@adtrackify/at-service-common 3.8.9 → 3.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/clients/generic/redis-client.d.ts +13 -13
- package/dist/cjs/clients/generic/redis-client.js +63 -33
- package/dist/cjs/clients/generic/redis-client.js.map +1 -1
- package/dist/esm/clients/generic/redis-client.d.ts +13 -13
- package/dist/esm/clients/generic/redis-client.js +63 -33
- package/dist/esm/clients/generic/redis-client.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import Redis from 'ioredis';
|
|
2
|
-
export interface RedisClientOptions {
|
|
3
|
-
host: string;
|
|
4
|
-
port?: number;
|
|
5
|
-
poolSize?: number;
|
|
6
|
-
tls?: boolean;
|
|
7
|
-
}
|
|
1
|
+
import Redis, { ChainableCommander } from 'ioredis';
|
|
8
2
|
export declare class RedisClient {
|
|
3
|
+
private poolSize;
|
|
9
4
|
private pool;
|
|
10
5
|
host: string;
|
|
11
6
|
port: number;
|
|
12
|
-
|
|
13
|
-
constructor(hostOrOptions: string | RedisClientOptions, port?: number, poolSize?: number);
|
|
7
|
+
constructor(host: string, port?: number, poolSize?: number);
|
|
14
8
|
private createClient;
|
|
15
9
|
private getClient;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
ensureConnected(): Promise<Redis>;
|
|
10
|
+
connect(client: Redis): Promise<void>;
|
|
11
|
+
disconnect(): Promise<void>;
|
|
19
12
|
safeSet(key: string, value: string, ttlInSeconds?: number): Promise<void>;
|
|
20
13
|
unsafeSet(key: string, value: string, ttlInSeconds?: number): Promise<void>;
|
|
21
14
|
unsafeGet(key: string): Promise<string | null>;
|
|
22
15
|
safeGet(key: string): Promise<string | null>;
|
|
23
16
|
safeBatchGet(keys: string[]): Promise<(string | null)[] | null>;
|
|
24
17
|
del(key: string): Promise<number>;
|
|
25
|
-
|
|
18
|
+
safeHmget(key: string, ...fields: string[]): Promise<(string | null)[] | null>;
|
|
19
|
+
safeLrange(key: string, start: number, stop: number): Promise<string[] | null>;
|
|
20
|
+
safeZcount(key: string, min: number | string, max: number | string): Promise<number>;
|
|
21
|
+
safeZrevrangebyscore(key: string, max: number | string, min: number | string, offsetCount?: {
|
|
22
|
+
offset: number;
|
|
23
|
+
count: number;
|
|
24
|
+
}): Promise<string[]>;
|
|
25
|
+
createPipeline(): ChainableCommander;
|
|
26
26
|
}
|
|
@@ -6,27 +6,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.RedisClient = void 0;
|
|
7
7
|
const ioredis_1 = __importDefault(require("ioredis"));
|
|
8
8
|
const helpers_1 = require("../../helpers");
|
|
9
|
+
function shouldUseTls(host) {
|
|
10
|
+
return host.includes('clustercfg') || host.includes('cache.amazonaws.com');
|
|
11
|
+
}
|
|
9
12
|
class RedisClient {
|
|
13
|
+
poolSize;
|
|
10
14
|
pool;
|
|
11
15
|
host;
|
|
12
16
|
port;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
this.useTls = hostOrOptions.includes('clustercfg') || hostOrOptions.includes('cache.amazonaws.com');
|
|
19
|
-
this.pool = Array.from({ length: poolSize }, () => this.createClient(this.host, this.port));
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
this.host = hostOrOptions.host;
|
|
23
|
-
this.port = hostOrOptions.port ?? 6379;
|
|
24
|
-
this.useTls = hostOrOptions.tls ?? (this.host.includes('clustercfg') || this.host.includes('cache.amazonaws.com'));
|
|
25
|
-
this.pool = Array.from({ length: hostOrOptions.poolSize ?? 3 }, () => this.createClient(this.host, this.port));
|
|
26
|
-
}
|
|
17
|
+
constructor(host, port = 6379, poolSize = 3) {
|
|
18
|
+
this.poolSize = poolSize;
|
|
19
|
+
this.host = host;
|
|
20
|
+
this.port = port;
|
|
21
|
+
this.pool = Array.from({ length: poolSize }, () => this.createClient(host, port));
|
|
27
22
|
}
|
|
28
23
|
createClient(host, port) {
|
|
29
24
|
try {
|
|
25
|
+
const useTls = shouldUseTls(host);
|
|
30
26
|
const client = new ioredis_1.default({
|
|
31
27
|
host,
|
|
32
28
|
port,
|
|
@@ -34,7 +30,7 @@ class RedisClient {
|
|
|
34
30
|
commandTimeout: 3000,
|
|
35
31
|
lazyConnect: true,
|
|
36
32
|
keepAlive: 1000,
|
|
37
|
-
...(
|
|
33
|
+
...(useTls && { tls: {} }),
|
|
38
34
|
});
|
|
39
35
|
return client;
|
|
40
36
|
}
|
|
@@ -44,22 +40,16 @@ class RedisClient {
|
|
|
44
40
|
}
|
|
45
41
|
}
|
|
46
42
|
getClient() {
|
|
47
|
-
const randomIndex = Math.floor(Math.random() * this.
|
|
43
|
+
const randomIndex = Math.floor(Math.random() * this.poolSize);
|
|
48
44
|
let client = this.pool[randomIndex];
|
|
49
45
|
if (!client) {
|
|
50
46
|
client = this.createClient(this.host, this.port);
|
|
51
47
|
}
|
|
52
48
|
return client;
|
|
53
49
|
}
|
|
54
|
-
getRedisInstance() {
|
|
55
|
-
return this.getClient();
|
|
56
|
-
}
|
|
57
50
|
async connect(client) {
|
|
58
|
-
const target = client ?? this.getClient();
|
|
59
51
|
try {
|
|
60
|
-
|
|
61
|
-
return;
|
|
62
|
-
await target.connect();
|
|
52
|
+
await client.connect();
|
|
63
53
|
helpers_1.Logger.debug('Connected to Redis');
|
|
64
54
|
}
|
|
65
55
|
catch (err) {
|
|
@@ -67,12 +57,16 @@ class RedisClient {
|
|
|
67
57
|
return;
|
|
68
58
|
}
|
|
69
59
|
}
|
|
70
|
-
async
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
await client.
|
|
60
|
+
async disconnect() {
|
|
61
|
+
try {
|
|
62
|
+
const client = this.getClient();
|
|
63
|
+
await client.quit();
|
|
64
|
+
helpers_1.Logger.debug('Disconnected from Redis');
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
helpers_1.Logger.error('Error disconnecting from Redis:', { err });
|
|
68
|
+
return;
|
|
74
69
|
}
|
|
75
|
-
return client;
|
|
76
70
|
}
|
|
77
71
|
async safeSet(key, value, ttlInSeconds) {
|
|
78
72
|
try {
|
|
@@ -135,17 +129,53 @@ class RedisClient {
|
|
|
135
129
|
return 0;
|
|
136
130
|
}
|
|
137
131
|
}
|
|
138
|
-
async
|
|
132
|
+
async safeHmget(key, ...fields) {
|
|
139
133
|
try {
|
|
140
134
|
const client = this.getClient();
|
|
141
|
-
await client.
|
|
142
|
-
helpers_1.Logger.debug('Disconnected from Redis');
|
|
135
|
+
return await client.hmget(key, ...fields);
|
|
143
136
|
}
|
|
144
137
|
catch (err) {
|
|
145
|
-
helpers_1.Logger.error('Error
|
|
146
|
-
return;
|
|
138
|
+
helpers_1.Logger.error('Error in hmget:', { err, key, fields });
|
|
139
|
+
return null;
|
|
147
140
|
}
|
|
148
141
|
}
|
|
142
|
+
async safeLrange(key, start, stop) {
|
|
143
|
+
try {
|
|
144
|
+
const client = this.getClient();
|
|
145
|
+
return await client.lrange(key, start, stop);
|
|
146
|
+
}
|
|
147
|
+
catch (err) {
|
|
148
|
+
helpers_1.Logger.error('Error in lrange:', { err, key, start, stop });
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
async safeZcount(key, min, max) {
|
|
153
|
+
try {
|
|
154
|
+
const client = this.getClient();
|
|
155
|
+
return await client.zcount(key, min, max);
|
|
156
|
+
}
|
|
157
|
+
catch (err) {
|
|
158
|
+
helpers_1.Logger.error('Error in zcount:', { err, key, min, max });
|
|
159
|
+
return 0;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
async safeZrevrangebyscore(key, max, min, offsetCount) {
|
|
163
|
+
try {
|
|
164
|
+
const client = this.getClient();
|
|
165
|
+
if (offsetCount) {
|
|
166
|
+
return await client.zrevrangebyscore(key, max, min, 'LIMIT', offsetCount.offset, offsetCount.count);
|
|
167
|
+
}
|
|
168
|
+
return await client.zrevrangebyscore(key, max, min);
|
|
169
|
+
}
|
|
170
|
+
catch (err) {
|
|
171
|
+
helpers_1.Logger.error('Error in zrevrangebyscore:', { err, key, max, min, offsetCount });
|
|
172
|
+
return [];
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
createPipeline() {
|
|
176
|
+
const client = this.getClient();
|
|
177
|
+
return client.pipeline();
|
|
178
|
+
}
|
|
149
179
|
}
|
|
150
180
|
exports.RedisClient = RedisClient;
|
|
151
181
|
//# sourceMappingURL=redis-client.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redis-client.js","sourceRoot":"","sources":["../../../../src/clients/generic/redis-client.ts"],"names":[],"mappings":";;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"redis-client.js","sourceRoot":"","sources":["../../../../src/clients/generic/redis-client.ts"],"names":[],"mappings":";;;;;;AAAA,sDAAoD;AACpD,2CAAuC;AAEvC,SAAS,YAAY,CAAC,IAAY;IAChC,OAAO,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,qBAAqB,CAAC,CAAC;AAC7E,CAAC;AAED,MAAa,WAAW;IAKyB;IAJvC,IAAI,CAAmB;IAC/B,IAAI,CAAS;IACb,IAAI,CAAS;IAEb,YAAY,IAAY,EAAE,IAAI,GAAG,IAAI,EAAU,WAAmB,CAAC;QAApB,aAAQ,GAAR,QAAQ,CAAY;QACjE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;IACpF,CAAC;IAEO,YAAY,CAAC,IAAY,EAAE,IAAY;QAC7C,IAAI;YACF,MAAM,MAAM,GAAG,YAAY,CAAC,IAA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AAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,KAAK,CAAC,CAAC;aACrG;YACD,OAAO,MAAM,MAAM,CAAC,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;SACrD;QAAC,OAAO,GAAG,EAAE;YACZ,gBAAM,CAAC,KAAK,CAAC,4BAA4B,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,WAAW,EAAE,CAAC,CAAC;YAChF,OAAO,EAAE,CAAC;SACX;IACH,CAAC;IAIM,cAAc;QACnB,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC;QAChC,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC;IAC3B,CAAC;CACF;AA1LD,kCA0LC"}
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import Redis from 'ioredis';
|
|
2
|
-
export interface RedisClientOptions {
|
|
3
|
-
host: string;
|
|
4
|
-
port?: number;
|
|
5
|
-
poolSize?: number;
|
|
6
|
-
tls?: boolean;
|
|
7
|
-
}
|
|
1
|
+
import Redis, { ChainableCommander } from 'ioredis';
|
|
8
2
|
export declare class RedisClient {
|
|
3
|
+
private poolSize;
|
|
9
4
|
private pool;
|
|
10
5
|
host: string;
|
|
11
6
|
port: number;
|
|
12
|
-
|
|
13
|
-
constructor(hostOrOptions: string | RedisClientOptions, port?: number, poolSize?: number);
|
|
7
|
+
constructor(host: string, port?: number, poolSize?: number);
|
|
14
8
|
private createClient;
|
|
15
9
|
private getClient;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
ensureConnected(): Promise<Redis>;
|
|
10
|
+
connect(client: Redis): Promise<void>;
|
|
11
|
+
disconnect(): Promise<void>;
|
|
19
12
|
safeSet(key: string, value: string, ttlInSeconds?: number): Promise<void>;
|
|
20
13
|
unsafeSet(key: string, value: string, ttlInSeconds?: number): Promise<void>;
|
|
21
14
|
unsafeGet(key: string): Promise<string | null>;
|
|
22
15
|
safeGet(key: string): Promise<string | null>;
|
|
23
16
|
safeBatchGet(keys: string[]): Promise<(string | null)[] | null>;
|
|
24
17
|
del(key: string): Promise<number>;
|
|
25
|
-
|
|
18
|
+
safeHmget(key: string, ...fields: string[]): Promise<(string | null)[] | null>;
|
|
19
|
+
safeLrange(key: string, start: number, stop: number): Promise<string[] | null>;
|
|
20
|
+
safeZcount(key: string, min: number | string, max: number | string): Promise<number>;
|
|
21
|
+
safeZrevrangebyscore(key: string, max: number | string, min: number | string, offsetCount?: {
|
|
22
|
+
offset: number;
|
|
23
|
+
count: number;
|
|
24
|
+
}): Promise<string[]>;
|
|
25
|
+
createPipeline(): ChainableCommander;
|
|
26
26
|
}
|
|
@@ -1,26 +1,22 @@
|
|
|
1
1
|
import Redis from 'ioredis';
|
|
2
2
|
import { Logger } from '../../helpers';
|
|
3
|
+
function shouldUseTls(host) {
|
|
4
|
+
return host.includes('clustercfg') || host.includes('cache.amazonaws.com');
|
|
5
|
+
}
|
|
3
6
|
export class RedisClient {
|
|
7
|
+
poolSize;
|
|
4
8
|
pool;
|
|
5
9
|
host;
|
|
6
10
|
port;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
this.useTls = hostOrOptions.includes('clustercfg') || hostOrOptions.includes('cache.amazonaws.com');
|
|
13
|
-
this.pool = Array.from({ length: poolSize }, () => this.createClient(this.host, this.port));
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
this.host = hostOrOptions.host;
|
|
17
|
-
this.port = hostOrOptions.port ?? 6379;
|
|
18
|
-
this.useTls = hostOrOptions.tls ?? (this.host.includes('clustercfg') || this.host.includes('cache.amazonaws.com'));
|
|
19
|
-
this.pool = Array.from({ length: hostOrOptions.poolSize ?? 3 }, () => this.createClient(this.host, this.port));
|
|
20
|
-
}
|
|
11
|
+
constructor(host, port = 6379, poolSize = 3) {
|
|
12
|
+
this.poolSize = poolSize;
|
|
13
|
+
this.host = host;
|
|
14
|
+
this.port = port;
|
|
15
|
+
this.pool = Array.from({ length: poolSize }, () => this.createClient(host, port));
|
|
21
16
|
}
|
|
22
17
|
createClient(host, port) {
|
|
23
18
|
try {
|
|
19
|
+
const useTls = shouldUseTls(host);
|
|
24
20
|
const client = new Redis({
|
|
25
21
|
host,
|
|
26
22
|
port,
|
|
@@ -28,7 +24,7 @@ export class RedisClient {
|
|
|
28
24
|
commandTimeout: 3000,
|
|
29
25
|
lazyConnect: true,
|
|
30
26
|
keepAlive: 1000,
|
|
31
|
-
...(
|
|
27
|
+
...(useTls && { tls: {} }),
|
|
32
28
|
});
|
|
33
29
|
return client;
|
|
34
30
|
}
|
|
@@ -38,22 +34,16 @@ export class RedisClient {
|
|
|
38
34
|
}
|
|
39
35
|
}
|
|
40
36
|
getClient() {
|
|
41
|
-
const randomIndex = Math.floor(Math.random() * this.
|
|
37
|
+
const randomIndex = Math.floor(Math.random() * this.poolSize);
|
|
42
38
|
let client = this.pool[randomIndex];
|
|
43
39
|
if (!client) {
|
|
44
40
|
client = this.createClient(this.host, this.port);
|
|
45
41
|
}
|
|
46
42
|
return client;
|
|
47
43
|
}
|
|
48
|
-
getRedisInstance() {
|
|
49
|
-
return this.getClient();
|
|
50
|
-
}
|
|
51
44
|
async connect(client) {
|
|
52
|
-
const target = client ?? this.getClient();
|
|
53
45
|
try {
|
|
54
|
-
|
|
55
|
-
return;
|
|
56
|
-
await target.connect();
|
|
46
|
+
await client.connect();
|
|
57
47
|
Logger.debug('Connected to Redis');
|
|
58
48
|
}
|
|
59
49
|
catch (err) {
|
|
@@ -61,12 +51,16 @@ export class RedisClient {
|
|
|
61
51
|
return;
|
|
62
52
|
}
|
|
63
53
|
}
|
|
64
|
-
async
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
await client.
|
|
54
|
+
async disconnect() {
|
|
55
|
+
try {
|
|
56
|
+
const client = this.getClient();
|
|
57
|
+
await client.quit();
|
|
58
|
+
Logger.debug('Disconnected from Redis');
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
Logger.error('Error disconnecting from Redis:', { err });
|
|
62
|
+
return;
|
|
68
63
|
}
|
|
69
|
-
return client;
|
|
70
64
|
}
|
|
71
65
|
async safeSet(key, value, ttlInSeconds) {
|
|
72
66
|
try {
|
|
@@ -129,16 +123,52 @@ export class RedisClient {
|
|
|
129
123
|
return 0;
|
|
130
124
|
}
|
|
131
125
|
}
|
|
132
|
-
async
|
|
126
|
+
async safeHmget(key, ...fields) {
|
|
133
127
|
try {
|
|
134
128
|
const client = this.getClient();
|
|
135
|
-
await client.
|
|
136
|
-
Logger.debug('Disconnected from Redis');
|
|
129
|
+
return await client.hmget(key, ...fields);
|
|
137
130
|
}
|
|
138
131
|
catch (err) {
|
|
139
|
-
Logger.error('Error
|
|
140
|
-
return;
|
|
132
|
+
Logger.error('Error in hmget:', { err, key, fields });
|
|
133
|
+
return null;
|
|
141
134
|
}
|
|
142
135
|
}
|
|
136
|
+
async safeLrange(key, start, stop) {
|
|
137
|
+
try {
|
|
138
|
+
const client = this.getClient();
|
|
139
|
+
return await client.lrange(key, start, stop);
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
Logger.error('Error in lrange:', { err, key, start, stop });
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async safeZcount(key, min, max) {
|
|
147
|
+
try {
|
|
148
|
+
const client = this.getClient();
|
|
149
|
+
return await client.zcount(key, min, max);
|
|
150
|
+
}
|
|
151
|
+
catch (err) {
|
|
152
|
+
Logger.error('Error in zcount:', { err, key, min, max });
|
|
153
|
+
return 0;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async safeZrevrangebyscore(key, max, min, offsetCount) {
|
|
157
|
+
try {
|
|
158
|
+
const client = this.getClient();
|
|
159
|
+
if (offsetCount) {
|
|
160
|
+
return await client.zrevrangebyscore(key, max, min, 'LIMIT', offsetCount.offset, offsetCount.count);
|
|
161
|
+
}
|
|
162
|
+
return await client.zrevrangebyscore(key, max, min);
|
|
163
|
+
}
|
|
164
|
+
catch (err) {
|
|
165
|
+
Logger.error('Error in zrevrangebyscore:', { err, key, max, min, offsetCount });
|
|
166
|
+
return [];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
createPipeline() {
|
|
170
|
+
const client = this.getClient();
|
|
171
|
+
return client.pipeline();
|
|
172
|
+
}
|
|
143
173
|
}
|
|
144
174
|
//# sourceMappingURL=redis-client.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redis-client.js","sourceRoot":"","sources":["../../../../src/clients/generic/redis-client.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"redis-client.js","sourceRoot":"","sources":["../../../../src/clients/generic/redis-client.ts"],"names":[],"mappings":"AAAA,OAAO,KAA6B,MAAM,SAAS,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAEvC,SAAS,YAAY,CAAC,IAAY;IAChC,OAAO,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,qBAAqB,CAAC,CAAC;AAC7E,CAAC;AAED,MAAM,OAAO,WAAW;IAKyB;IAJvC,IAAI,CAAmB;IAC/B,IAAI,CAAS;IACb,IAAI,CAAS;IAEb,YAAY,IAAY,EAAE,IAAI,GAAG,IAAI,EAAU,WAAmB,CAAC;QAApB,aAAQ,GAAR,QAAQ,CAAY;QACjE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;IACpF,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