@adtrackify/at-service-common 3.8.9 → 3.9.0
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 +14 -13
- package/dist/cjs/clients/generic/redis-client.js +72 -30
- package/dist/cjs/clients/generic/redis-client.js.map +1 -1
- package/dist/esm/clients/generic/redis-client.d.ts +14 -13
- package/dist/esm/clients/generic/redis-client.js +72 -30
- package/dist/esm/clients/generic/redis-client.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,26 +1,27 @@
|
|
|
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
|
-
|
|
10
|
+
connect(client: Redis): Promise<void>;
|
|
11
|
+
ensureConnected(): Promise<void>;
|
|
12
|
+
disconnect(): Promise<void>;
|
|
19
13
|
safeSet(key: string, value: string, ttlInSeconds?: number): Promise<void>;
|
|
20
14
|
unsafeSet(key: string, value: string, ttlInSeconds?: number): Promise<void>;
|
|
21
15
|
unsafeGet(key: string): Promise<string | null>;
|
|
22
16
|
safeGet(key: string): Promise<string | null>;
|
|
23
17
|
safeBatchGet(keys: string[]): Promise<(string | null)[] | null>;
|
|
24
18
|
del(key: string): Promise<number>;
|
|
25
|
-
|
|
19
|
+
safeHmget(key: string, ...fields: string[]): Promise<(string | null)[] | null>;
|
|
20
|
+
safeLrange(key: string, start: number, stop: number): Promise<string[] | null>;
|
|
21
|
+
safeZcount(key: string, min: number | string, max: number | string): Promise<number>;
|
|
22
|
+
safeZrevrangebyscore(key: string, max: number | string, min: number | string, offsetCount?: {
|
|
23
|
+
offset: number;
|
|
24
|
+
count: number;
|
|
25
|
+
}): Promise<string[]>;
|
|
26
|
+
createPipeline(): ChainableCommander;
|
|
26
27
|
}
|
|
@@ -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) {
|
|
@@ -69,10 +59,26 @@ class RedisClient {
|
|
|
69
59
|
}
|
|
70
60
|
async ensureConnected() {
|
|
71
61
|
const client = this.getClient();
|
|
72
|
-
if (client.status
|
|
62
|
+
if (client.status === 'ready')
|
|
63
|
+
return;
|
|
64
|
+
try {
|
|
73
65
|
await client.connect();
|
|
66
|
+
helpers_1.Logger.debug('Connected to Redis');
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
helpers_1.Logger.error('Error connecting to Redis:', { err });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async disconnect() {
|
|
73
|
+
try {
|
|
74
|
+
const client = this.getClient();
|
|
75
|
+
await client.quit();
|
|
76
|
+
helpers_1.Logger.debug('Disconnected from Redis');
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
helpers_1.Logger.error('Error disconnecting from Redis:', { err });
|
|
80
|
+
return;
|
|
74
81
|
}
|
|
75
|
-
return client;
|
|
76
82
|
}
|
|
77
83
|
async safeSet(key, value, ttlInSeconds) {
|
|
78
84
|
try {
|
|
@@ -135,17 +141,53 @@ class RedisClient {
|
|
|
135
141
|
return 0;
|
|
136
142
|
}
|
|
137
143
|
}
|
|
138
|
-
async
|
|
144
|
+
async safeHmget(key, ...fields) {
|
|
139
145
|
try {
|
|
140
146
|
const client = this.getClient();
|
|
141
|
-
await client.
|
|
142
|
-
helpers_1.Logger.debug('Disconnected from Redis');
|
|
147
|
+
return await client.hmget(key, ...fields);
|
|
143
148
|
}
|
|
144
149
|
catch (err) {
|
|
145
|
-
helpers_1.Logger.error('Error
|
|
146
|
-
return;
|
|
150
|
+
helpers_1.Logger.error('Error in hmget:', { err, key, fields });
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
async safeLrange(key, start, stop) {
|
|
155
|
+
try {
|
|
156
|
+
const client = this.getClient();
|
|
157
|
+
return await client.lrange(key, start, stop);
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
helpers_1.Logger.error('Error in lrange:', { err, key, start, stop });
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
async safeZcount(key, min, max) {
|
|
165
|
+
try {
|
|
166
|
+
const client = this.getClient();
|
|
167
|
+
return await client.zcount(key, min, max);
|
|
168
|
+
}
|
|
169
|
+
catch (err) {
|
|
170
|
+
helpers_1.Logger.error('Error in zcount:', { err, key, min, max });
|
|
171
|
+
return 0;
|
|
147
172
|
}
|
|
148
173
|
}
|
|
174
|
+
async safeZrevrangebyscore(key, max, min, offsetCount) {
|
|
175
|
+
try {
|
|
176
|
+
const client = this.getClient();
|
|
177
|
+
if (offsetCount) {
|
|
178
|
+
return await client.zrevrangebyscore(key, max, min, 'LIMIT', offsetCount.offset, offsetCount.count);
|
|
179
|
+
}
|
|
180
|
+
return await client.zrevrangebyscore(key, max, min);
|
|
181
|
+
}
|
|
182
|
+
catch (err) {
|
|
183
|
+
helpers_1.Logger.error('Error in zrevrangebyscore:', { err, key, max, min, offsetCount });
|
|
184
|
+
return [];
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
createPipeline() {
|
|
188
|
+
const client = this.getClient();
|
|
189
|
+
return client.pipeline();
|
|
190
|
+
}
|
|
149
191
|
}
|
|
150
192
|
exports.RedisClient = RedisClient;
|
|
151
193
|
//# 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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
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
|
-
|
|
10
|
+
connect(client: Redis): Promise<void>;
|
|
11
|
+
ensureConnected(): Promise<void>;
|
|
12
|
+
disconnect(): Promise<void>;
|
|
19
13
|
safeSet(key: string, value: string, ttlInSeconds?: number): Promise<void>;
|
|
20
14
|
unsafeSet(key: string, value: string, ttlInSeconds?: number): Promise<void>;
|
|
21
15
|
unsafeGet(key: string): Promise<string | null>;
|
|
22
16
|
safeGet(key: string): Promise<string | null>;
|
|
23
17
|
safeBatchGet(keys: string[]): Promise<(string | null)[] | null>;
|
|
24
18
|
del(key: string): Promise<number>;
|
|
25
|
-
|
|
19
|
+
safeHmget(key: string, ...fields: string[]): Promise<(string | null)[] | null>;
|
|
20
|
+
safeLrange(key: string, start: number, stop: number): Promise<string[] | null>;
|
|
21
|
+
safeZcount(key: string, min: number | string, max: number | string): Promise<number>;
|
|
22
|
+
safeZrevrangebyscore(key: string, max: number | string, min: number | string, offsetCount?: {
|
|
23
|
+
offset: number;
|
|
24
|
+
count: number;
|
|
25
|
+
}): Promise<string[]>;
|
|
26
|
+
createPipeline(): ChainableCommander;
|
|
26
27
|
}
|
|
@@ -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) {
|
|
@@ -63,10 +53,26 @@ export class RedisClient {
|
|
|
63
53
|
}
|
|
64
54
|
async ensureConnected() {
|
|
65
55
|
const client = this.getClient();
|
|
66
|
-
if (client.status
|
|
56
|
+
if (client.status === 'ready')
|
|
57
|
+
return;
|
|
58
|
+
try {
|
|
67
59
|
await client.connect();
|
|
60
|
+
Logger.debug('Connected to Redis');
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
Logger.error('Error connecting to Redis:', { err });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async disconnect() {
|
|
67
|
+
try {
|
|
68
|
+
const client = this.getClient();
|
|
69
|
+
await client.quit();
|
|
70
|
+
Logger.debug('Disconnected from Redis');
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
Logger.error('Error disconnecting from Redis:', { err });
|
|
74
|
+
return;
|
|
68
75
|
}
|
|
69
|
-
return client;
|
|
70
76
|
}
|
|
71
77
|
async safeSet(key, value, ttlInSeconds) {
|
|
72
78
|
try {
|
|
@@ -129,16 +135,52 @@ export class RedisClient {
|
|
|
129
135
|
return 0;
|
|
130
136
|
}
|
|
131
137
|
}
|
|
132
|
-
async
|
|
138
|
+
async safeHmget(key, ...fields) {
|
|
133
139
|
try {
|
|
134
140
|
const client = this.getClient();
|
|
135
|
-
await client.
|
|
136
|
-
Logger.debug('Disconnected from Redis');
|
|
141
|
+
return await client.hmget(key, ...fields);
|
|
137
142
|
}
|
|
138
143
|
catch (err) {
|
|
139
|
-
Logger.error('Error
|
|
140
|
-
return;
|
|
144
|
+
Logger.error('Error in hmget:', { err, key, fields });
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
async safeLrange(key, start, stop) {
|
|
149
|
+
try {
|
|
150
|
+
const client = this.getClient();
|
|
151
|
+
return await client.lrange(key, start, stop);
|
|
152
|
+
}
|
|
153
|
+
catch (err) {
|
|
154
|
+
Logger.error('Error in lrange:', { err, key, start, stop });
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
async safeZcount(key, min, max) {
|
|
159
|
+
try {
|
|
160
|
+
const client = this.getClient();
|
|
161
|
+
return await client.zcount(key, min, max);
|
|
162
|
+
}
|
|
163
|
+
catch (err) {
|
|
164
|
+
Logger.error('Error in zcount:', { err, key, min, max });
|
|
165
|
+
return 0;
|
|
141
166
|
}
|
|
142
167
|
}
|
|
168
|
+
async safeZrevrangebyscore(key, max, min, offsetCount) {
|
|
169
|
+
try {
|
|
170
|
+
const client = this.getClient();
|
|
171
|
+
if (offsetCount) {
|
|
172
|
+
return await client.zrevrangebyscore(key, max, min, 'LIMIT', offsetCount.offset, offsetCount.count);
|
|
173
|
+
}
|
|
174
|
+
return await client.zrevrangebyscore(key, max, min);
|
|
175
|
+
}
|
|
176
|
+
catch (err) {
|
|
177
|
+
Logger.error('Error in zrevrangebyscore:', { err, key, max, min, offsetCount });
|
|
178
|
+
return [];
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
createPipeline() {
|
|
182
|
+
const client = this.getClient();
|
|
183
|
+
return client.pipeline();
|
|
184
|
+
}
|
|
143
185
|
}
|
|
144
186
|
//# sourceMappingURL=redis-client.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redis-client.js","sourceRoot":"","sources":["../../../../src/clients/generic/redis-client.ts"],"names":[],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