@tsed/ioredis 7.2.3 → 7.3.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.
|
@@ -3,90 +3,92 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ioRedisStore = exports.IORedisStore = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const ioredis_1 = tslib_1.__importDefault(require("ioredis"));
|
|
6
|
-
|
|
7
|
-
try {
|
|
8
|
-
let result = await next();
|
|
9
|
-
if (parse) {
|
|
10
|
-
result = result && JSON.parse(result);
|
|
11
|
-
}
|
|
12
|
-
if (!cb) {
|
|
13
|
-
return result;
|
|
14
|
-
}
|
|
15
|
-
cb(null, result);
|
|
16
|
-
}
|
|
17
|
-
catch (er) {
|
|
18
|
-
if (!cb) {
|
|
19
|
-
throw er;
|
|
20
|
-
}
|
|
21
|
-
cb(er, null);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
6
|
+
const getVal = (value) => JSON.stringify(value) || '"undefined"';
|
|
24
7
|
class IORedisStore {
|
|
25
8
|
name = "redis";
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated
|
|
11
|
+
*/
|
|
26
12
|
isCacheableValue;
|
|
13
|
+
isCacheable;
|
|
27
14
|
redisCache;
|
|
28
15
|
storeArgs;
|
|
29
|
-
constructor(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
else if (args.length > 0 && args[0].clusterConfig) {
|
|
34
|
-
const { nodes, options } = args[0].clusterConfig;
|
|
35
|
-
this.redisCache = new ioredis_1.default.Cluster(nodes, options || {});
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
this.redisCache = new ioredis_1.default(...args);
|
|
39
|
-
}
|
|
16
|
+
constructor(options) {
|
|
17
|
+
options ||= {};
|
|
18
|
+
this.redisCache =
|
|
19
|
+
"clusterConfig" in options ? new ioredis_1.default.Cluster(options.clusterConfig.nodes, options.clusterConfig.options) : new ioredis_1.default(options);
|
|
40
20
|
this.storeArgs = this.redisCache.options;
|
|
41
|
-
this.
|
|
21
|
+
this.isCacheable = this.isCacheableValue =
|
|
22
|
+
this.storeArgs.isCacheable || this.storeArgs.isCacheableValue || ((value) => value !== undefined && value !== null);
|
|
23
|
+
}
|
|
24
|
+
get client() {
|
|
25
|
+
return this.redisCache;
|
|
42
26
|
}
|
|
43
27
|
getClient() {
|
|
44
28
|
return this.redisCache;
|
|
45
29
|
}
|
|
46
|
-
set(key, value,
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
30
|
+
async set(key, value, ttl) {
|
|
31
|
+
this.assertCacheable(value);
|
|
32
|
+
ttl = this.getTtl(ttl);
|
|
33
|
+
const val = getVal(value);
|
|
34
|
+
if (ttl) {
|
|
35
|
+
await this.redisCache.setex(key, ttl, val);
|
|
36
|
+
return;
|
|
50
37
|
}
|
|
51
|
-
|
|
52
|
-
return handle(async () => {
|
|
53
|
-
if (!this.isCacheableValue(value)) {
|
|
54
|
-
throw new Error(`"${value}" is not a cacheable value`);
|
|
55
|
-
}
|
|
56
|
-
const ttl = options.ttl || options.ttl === 0 ? options.ttl : this.storeArgs.ttl;
|
|
57
|
-
const val = JSON.stringify(value) || '"undefined"';
|
|
58
|
-
if (ttl) {
|
|
59
|
-
return this.redisCache.setex(key, ttl, val);
|
|
60
|
-
}
|
|
61
|
-
return this.redisCache.set(key, val);
|
|
62
|
-
}, false, cb);
|
|
38
|
+
await this.redisCache.set(key, val);
|
|
63
39
|
}
|
|
64
|
-
get(key, options
|
|
65
|
-
|
|
66
|
-
|
|
40
|
+
async get(key, options) {
|
|
41
|
+
const val = await this.redisCache.get(key);
|
|
42
|
+
if (val === undefined || val === null) {
|
|
43
|
+
return undefined;
|
|
67
44
|
}
|
|
68
|
-
return
|
|
45
|
+
return JSON.parse(val);
|
|
46
|
+
}
|
|
47
|
+
async del(key) {
|
|
48
|
+
await this.redisCache.del(key);
|
|
69
49
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
50
|
+
async mset(args, ttl) {
|
|
51
|
+
ttl = this.getTtl(ttl);
|
|
52
|
+
if (ttl) {
|
|
53
|
+
const multi = this.redisCache.multi();
|
|
54
|
+
for (const [key, value] of args) {
|
|
55
|
+
this.assertCacheable(value);
|
|
56
|
+
multi.setex(key, ttl / 1000, getVal(value));
|
|
57
|
+
}
|
|
58
|
+
await multi.exec();
|
|
73
59
|
}
|
|
74
|
-
|
|
60
|
+
else
|
|
61
|
+
await this.redisCache.mset(args.flatMap(([key, value]) => {
|
|
62
|
+
this.assertCacheable(value);
|
|
63
|
+
return [key, getVal(value)];
|
|
64
|
+
}));
|
|
75
65
|
}
|
|
76
|
-
|
|
77
|
-
|
|
66
|
+
async mget(...args) {
|
|
67
|
+
const x = await this.redisCache.mget(args);
|
|
68
|
+
return x.map((x) => (x === null || x === undefined ? undefined : JSON.parse(x)));
|
|
78
69
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
cb = pattern;
|
|
82
|
-
pattern = "*";
|
|
83
|
-
}
|
|
84
|
-
return handle(() => this.redisCache.keys(pattern), false, cb);
|
|
70
|
+
async mdel(...args) {
|
|
71
|
+
await this.redisCache.del(args);
|
|
85
72
|
}
|
|
86
|
-
|
|
87
|
-
|
|
73
|
+
async reset() {
|
|
74
|
+
await this.redisCache.flushall();
|
|
75
|
+
}
|
|
76
|
+
keys(pattern = "*") {
|
|
77
|
+
return this.redisCache.keys(pattern);
|
|
78
|
+
}
|
|
79
|
+
ttl(key) {
|
|
80
|
+
return this.redisCache.ttl(key);
|
|
81
|
+
}
|
|
82
|
+
getTtl(ttl) {
|
|
83
|
+
return ttl === undefined ? this.storeArgs.ttl : ttl;
|
|
84
|
+
}
|
|
85
|
+
assertCacheable(value) {
|
|
86
|
+
if (!this.isCacheable(value)) {
|
|
87
|
+
throw new Error(`"${getVal(value)}" is not a cacheable value`);
|
|
88
|
+
}
|
|
88
89
|
}
|
|
89
90
|
}
|
|
90
91
|
exports.IORedisStore = IORedisStore;
|
|
91
|
-
|
|
92
|
+
const ioRedisStore = (config) => new IORedisStore(config);
|
|
93
|
+
exports.ioRedisStore = ioRedisStore;
|
|
92
94
|
//# sourceMappingURL=IORedisStore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IORedisStore.js","sourceRoot":"","sources":["../../../src/domain/IORedisStore.ts"],"names":[],"mappings":";;;;AACA,
|
|
1
|
+
{"version":3,"file":"IORedisStore.js","sourceRoot":"","sources":["../../../src/domain/IORedisStore.ts"],"names":[],"mappings":";;;;AACA,8DAAkF;AAOlF,MAAM,MAAM,GAAG,CAAC,KAAc,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,aAAa,CAAC;AAE1E,MAAa,YAAY;IAChB,IAAI,GAAG,OAAO,CAAC;IACtB;;OAEG;IACI,gBAAgB,CAAC;IACjB,WAAW,CAAC;IACX,UAAU,CAAkB;IAC5B,SAAS,CAAM;IAEvB,YAAY,OAAuE;QACjF,OAAO,KAAK,EAAE,CAAC;QAEf,IAAI,CAAC,UAAU;YACb,eAAe,IAAI,OAAO,CAAC,CAAC,CAAC,IAAI,iBAAK,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC,KAAK,EAAE,OAAO,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,iBAAK,CAAC,OAAO,CAAC,CAAC;QAElI,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC;QACzC,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,gBAAgB;YACtC,IAAI,CAAC,SAAS,CAAC,WAAW,IAAI,IAAI,CAAC,SAAS,CAAC,gBAAgB,IAAI,CAAC,CAAC,KAAU,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,CAAC,CAAC;IAC7H,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAA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AEO,MAAM,CAAC,GAAY;QACzB,OAAO,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;IACtD,CAAC;IAEO,eAAe,CAAC,KAAU;QAChC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE;YAC5B,MAAM,IAAI,KAAK,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAC;SAChE;IACH,CAAC;CACF;AAhHD,oCAgHC;AAEM,MAAM,YAAY,GAAG,CAAC,MAA2B,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,MAAM,CAAC,CAAC;AAAzE,QAAA,YAAY,gBAA6D","sourcesContent":["import type {Config, FactoryConfig, Store} from \"cache-manager\";\nimport Redis, {Cluster, ClusterNode, ClusterOptions, RedisOptions} from \"ioredis\";\n\nexport interface RedisClusterConfig {\n nodes: ClusterNode[];\n options?: ClusterOptions;\n}\n\nconst getVal = (value: unknown) => JSON.stringify(value) || '\"undefined\"';\n\nexport class IORedisStore implements Store {\n public name = \"redis\";\n /**\n * @deprecated\n */\n public isCacheableValue;\n public isCacheable;\n private redisCache: Redis | Cluster;\n private storeArgs: any;\n\n constructor(options?: (RedisOptions | {clusterConfig: RedisClusterConfig}) & Config) {\n options ||= {};\n\n this.redisCache =\n \"clusterConfig\" in options ? new Redis.Cluster(options.clusterConfig.nodes, options.clusterConfig.options) : new Redis(options);\n\n this.storeArgs = this.redisCache.options;\n this.isCacheable = this.isCacheableValue =\n this.storeArgs.isCacheable || this.storeArgs.isCacheableValue || ((value: any) => value !== undefined && value !== null);\n }\n\n get client() {\n return this.redisCache;\n }\n\n getClient() {\n return this.redisCache;\n }\n\n async set(key: string, value: any, ttl?: number) {\n this.assertCacheable(value);\n\n ttl = this.getTtl(ttl);\n\n const val = getVal(value);\n\n if (ttl) {\n await this.redisCache.setex(key, ttl, val);\n return;\n }\n\n await this.redisCache.set(key, val);\n }\n\n async get<T = any>(key: string, options?: any) {\n const val = await this.redisCache.get(key);\n\n if (val === undefined || val === null) {\n return undefined;\n }\n\n return JSON.parse(val) as T;\n }\n\n async del(key: string) {\n await this.redisCache.del(key);\n }\n\n async mset(args: [string, unknown][], ttl?: number) {\n ttl = this.getTtl(ttl);\n\n if (ttl) {\n const multi = this.redisCache.multi();\n\n for (const [key, value] of args) {\n this.assertCacheable(value);\n\n multi.setex(key, ttl / 1000, getVal(value));\n }\n\n await multi.exec();\n } else\n await this.redisCache.mset(\n args.flatMap(([key, value]) => {\n this.assertCacheable(value);\n\n return [key, getVal(value)] as [string, string];\n })\n );\n }\n\n async mget(...args: string[]) {\n const x = await this.redisCache.mget(args);\n\n return x.map((x) => (x === null || x === undefined ? undefined : (JSON.parse(x) as unknown)));\n }\n\n async mdel(...args: string[]) {\n await this.redisCache.del(args);\n }\n\n async reset() {\n await this.redisCache.flushall();\n }\n\n keys(pattern: string = \"*\") {\n return this.redisCache.keys(pattern);\n }\n\n ttl(key: string) {\n return this.redisCache.ttl(key);\n }\n\n private getTtl(ttl?: number) {\n return ttl === undefined ? this.storeArgs.ttl : ttl;\n }\n\n private assertCacheable(value: any) {\n if (!this.isCacheable(value)) {\n throw new Error(`\"${getVal(value)}\" is not a cacheable value`);\n }\n }\n}\n\nexport const ioRedisStore = (config?: FactoryConfig<any>) => new IORedisStore(config);\n"]}
|
|
@@ -1,87 +1,88 @@
|
|
|
1
1
|
import Redis from "ioredis";
|
|
2
|
-
|
|
3
|
-
try {
|
|
4
|
-
let result = await next();
|
|
5
|
-
if (parse) {
|
|
6
|
-
result = result && JSON.parse(result);
|
|
7
|
-
}
|
|
8
|
-
if (!cb) {
|
|
9
|
-
return result;
|
|
10
|
-
}
|
|
11
|
-
cb(null, result);
|
|
12
|
-
}
|
|
13
|
-
catch (er) {
|
|
14
|
-
if (!cb) {
|
|
15
|
-
throw er;
|
|
16
|
-
}
|
|
17
|
-
cb(er, null);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
2
|
+
const getVal = (value) => JSON.stringify(value) || '"undefined"';
|
|
20
3
|
export class IORedisStore {
|
|
21
4
|
name = "redis";
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated
|
|
7
|
+
*/
|
|
22
8
|
isCacheableValue;
|
|
9
|
+
isCacheable;
|
|
23
10
|
redisCache;
|
|
24
11
|
storeArgs;
|
|
25
|
-
constructor(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
else if (args.length > 0 && args[0].clusterConfig) {
|
|
30
|
-
const { nodes, options } = args[0].clusterConfig;
|
|
31
|
-
this.redisCache = new Redis.Cluster(nodes, options || {});
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
this.redisCache = new Redis(...args);
|
|
35
|
-
}
|
|
12
|
+
constructor(options) {
|
|
13
|
+
options ||= {};
|
|
14
|
+
this.redisCache =
|
|
15
|
+
"clusterConfig" in options ? new Redis.Cluster(options.clusterConfig.nodes, options.clusterConfig.options) : new Redis(options);
|
|
36
16
|
this.storeArgs = this.redisCache.options;
|
|
37
|
-
this.
|
|
17
|
+
this.isCacheable = this.isCacheableValue =
|
|
18
|
+
this.storeArgs.isCacheable || this.storeArgs.isCacheableValue || ((value) => value !== undefined && value !== null);
|
|
19
|
+
}
|
|
20
|
+
get client() {
|
|
21
|
+
return this.redisCache;
|
|
38
22
|
}
|
|
39
23
|
getClient() {
|
|
40
24
|
return this.redisCache;
|
|
41
25
|
}
|
|
42
|
-
set(key, value,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
26
|
+
async set(key, value, ttl) {
|
|
27
|
+
this.assertCacheable(value);
|
|
28
|
+
ttl = this.getTtl(ttl);
|
|
29
|
+
const val = getVal(value);
|
|
30
|
+
if (ttl) {
|
|
31
|
+
await this.redisCache.setex(key, ttl, val);
|
|
32
|
+
return;
|
|
46
33
|
}
|
|
47
|
-
|
|
48
|
-
return handle(async () => {
|
|
49
|
-
if (!this.isCacheableValue(value)) {
|
|
50
|
-
throw new Error(`"${value}" is not a cacheable value`);
|
|
51
|
-
}
|
|
52
|
-
const ttl = options.ttl || options.ttl === 0 ? options.ttl : this.storeArgs.ttl;
|
|
53
|
-
const val = JSON.stringify(value) || '"undefined"';
|
|
54
|
-
if (ttl) {
|
|
55
|
-
return this.redisCache.setex(key, ttl, val);
|
|
56
|
-
}
|
|
57
|
-
return this.redisCache.set(key, val);
|
|
58
|
-
}, false, cb);
|
|
34
|
+
await this.redisCache.set(key, val);
|
|
59
35
|
}
|
|
60
|
-
get(key, options
|
|
61
|
-
|
|
62
|
-
|
|
36
|
+
async get(key, options) {
|
|
37
|
+
const val = await this.redisCache.get(key);
|
|
38
|
+
if (val === undefined || val === null) {
|
|
39
|
+
return undefined;
|
|
63
40
|
}
|
|
64
|
-
return
|
|
41
|
+
return JSON.parse(val);
|
|
42
|
+
}
|
|
43
|
+
async del(key) {
|
|
44
|
+
await this.redisCache.del(key);
|
|
65
45
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
46
|
+
async mset(args, ttl) {
|
|
47
|
+
ttl = this.getTtl(ttl);
|
|
48
|
+
if (ttl) {
|
|
49
|
+
const multi = this.redisCache.multi();
|
|
50
|
+
for (const [key, value] of args) {
|
|
51
|
+
this.assertCacheable(value);
|
|
52
|
+
multi.setex(key, ttl / 1000, getVal(value));
|
|
53
|
+
}
|
|
54
|
+
await multi.exec();
|
|
69
55
|
}
|
|
70
|
-
|
|
56
|
+
else
|
|
57
|
+
await this.redisCache.mset(args.flatMap(([key, value]) => {
|
|
58
|
+
this.assertCacheable(value);
|
|
59
|
+
return [key, getVal(value)];
|
|
60
|
+
}));
|
|
71
61
|
}
|
|
72
|
-
|
|
73
|
-
|
|
62
|
+
async mget(...args) {
|
|
63
|
+
const x = await this.redisCache.mget(args);
|
|
64
|
+
return x.map((x) => (x === null || x === undefined ? undefined : JSON.parse(x)));
|
|
74
65
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
cb = pattern;
|
|
78
|
-
pattern = "*";
|
|
79
|
-
}
|
|
80
|
-
return handle(() => this.redisCache.keys(pattern), false, cb);
|
|
66
|
+
async mdel(...args) {
|
|
67
|
+
await this.redisCache.del(args);
|
|
81
68
|
}
|
|
82
|
-
|
|
83
|
-
|
|
69
|
+
async reset() {
|
|
70
|
+
await this.redisCache.flushall();
|
|
71
|
+
}
|
|
72
|
+
keys(pattern = "*") {
|
|
73
|
+
return this.redisCache.keys(pattern);
|
|
74
|
+
}
|
|
75
|
+
ttl(key) {
|
|
76
|
+
return this.redisCache.ttl(key);
|
|
77
|
+
}
|
|
78
|
+
getTtl(ttl) {
|
|
79
|
+
return ttl === undefined ? this.storeArgs.ttl : ttl;
|
|
80
|
+
}
|
|
81
|
+
assertCacheable(value) {
|
|
82
|
+
if (!this.isCacheable(value)) {
|
|
83
|
+
throw new Error(`"${getVal(value)}" is not a cacheable value`);
|
|
84
|
+
}
|
|
84
85
|
}
|
|
85
86
|
}
|
|
86
|
-
export const ioRedisStore =
|
|
87
|
+
export const ioRedisStore = (config) => new IORedisStore(config);
|
|
87
88
|
//# sourceMappingURL=IORedisStore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IORedisStore.js","sourceRoot":"","sources":["../../../src/domain/IORedisStore.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"IORedisStore.js","sourceRoot":"","sources":["../../../src/domain/IORedisStore.ts"],"names":[],"mappings":"AACA,OAAO,KAA2D,MAAM,SAAS,CAAC;AAOlF,MAAM,MAAM,GAAG,CAAC,KAAc,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,aAAa,CAAC;AAE1E,MAAM,OAAO,YAAY;IAChB,IAAI,GAAG,OAAO,CAAC;IACtB;;OAEG;IACI,gBAAgB,CAAC;IACjB,WAAW,CAAC;IACX,UAAU,CAAkB;IAC5B,SAAS,CAAM;IAEvB,YAAY,OAAuE;QACj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AAC,CAAC,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAa,CAAC,CAAC,CAAC;IAChG,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,GAAG,IAAc;QAC1B,MAAM,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAClC,CAAC;IAED,KAAK,CAAC,KAAK;QACT,MAAM,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC;IACnC,CAAC;IAED,IAAI,CAAC,UAAkB,GAAG;QACxB,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACvC,CAAC;IAED,GAAG,CAAC,GAAW;QACb,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAClC,CAAC;IAEO,MAAM,CAAC,GAAY;QACzB,OAAO,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;IACtD,CAAC;IAEO,eAAe,CAAC,KAAU;QAChC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE;YAC5B,MAAM,IAAI,KAAK,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAC;SAChE;IACH,CAAC;CACF;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,MAA2B,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,MAAM,CAAC,CAAC","sourcesContent":["import type {Config, FactoryConfig, Store} from \"cache-manager\";\nimport Redis, {Cluster, ClusterNode, ClusterOptions, RedisOptions} from \"ioredis\";\n\nexport interface RedisClusterConfig {\n nodes: ClusterNode[];\n options?: ClusterOptions;\n}\n\nconst getVal = (value: unknown) => JSON.stringify(value) || '\"undefined\"';\n\nexport class IORedisStore implements Store {\n public name = \"redis\";\n /**\n * @deprecated\n */\n public isCacheableValue;\n public isCacheable;\n private redisCache: Redis | Cluster;\n private storeArgs: any;\n\n constructor(options?: (RedisOptions | {clusterConfig: RedisClusterConfig}) & Config) {\n options ||= {};\n\n this.redisCache =\n \"clusterConfig\" in options ? new Redis.Cluster(options.clusterConfig.nodes, options.clusterConfig.options) : new Redis(options);\n\n this.storeArgs = this.redisCache.options;\n this.isCacheable = this.isCacheableValue =\n this.storeArgs.isCacheable || this.storeArgs.isCacheableValue || ((value: any) => value !== undefined && value !== null);\n }\n\n get client() {\n return this.redisCache;\n }\n\n getClient() {\n return this.redisCache;\n }\n\n async set(key: string, value: any, ttl?: number) {\n this.assertCacheable(value);\n\n ttl = this.getTtl(ttl);\n\n const val = getVal(value);\n\n if (ttl) {\n await this.redisCache.setex(key, ttl, val);\n return;\n }\n\n await this.redisCache.set(key, val);\n }\n\n async get<T = any>(key: string, options?: any) {\n const val = await this.redisCache.get(key);\n\n if (val === undefined || val === null) {\n return undefined;\n }\n\n return JSON.parse(val) as T;\n }\n\n async del(key: string) {\n await this.redisCache.del(key);\n }\n\n async mset(args: [string, unknown][], ttl?: number) {\n ttl = this.getTtl(ttl);\n\n if (ttl) {\n const multi = this.redisCache.multi();\n\n for (const [key, value] of args) {\n this.assertCacheable(value);\n\n multi.setex(key, ttl / 1000, getVal(value));\n }\n\n await multi.exec();\n } else\n await this.redisCache.mset(\n args.flatMap(([key, value]) => {\n this.assertCacheable(value);\n\n return [key, getVal(value)] as [string, string];\n })\n );\n }\n\n async mget(...args: string[]) {\n const x = await this.redisCache.mget(args);\n\n return x.map((x) => (x === null || x === undefined ? undefined : (JSON.parse(x) as unknown)));\n }\n\n async mdel(...args: string[]) {\n await this.redisCache.del(args);\n }\n\n async reset() {\n await this.redisCache.flushall();\n }\n\n keys(pattern: string = \"*\") {\n return this.redisCache.keys(pattern);\n }\n\n ttl(key: string) {\n return this.redisCache.ttl(key);\n }\n\n private getTtl(ttl?: number) {\n return ttl === undefined ? this.storeArgs.ttl : ttl;\n }\n\n private assertCacheable(value: any) {\n if (!this.isCacheable(value)) {\n throw new Error(`\"${getVal(value)}\" is not a cacheable value`);\n }\n }\n}\n\nexport const ioRedisStore = (config?: FactoryConfig<any>) => new IORedisStore(config);\n"]}
|
|
@@ -1,20 +1,33 @@
|
|
|
1
|
-
import { Store } from "cache-manager";
|
|
2
|
-
import Redis, { Cluster } from "ioredis";
|
|
3
|
-
|
|
1
|
+
import type { Config, FactoryConfig, Store } from "cache-manager";
|
|
2
|
+
import Redis, { Cluster, ClusterNode, ClusterOptions, RedisOptions } from "ioredis";
|
|
3
|
+
export interface RedisClusterConfig {
|
|
4
|
+
nodes: ClusterNode[];
|
|
5
|
+
options?: ClusterOptions;
|
|
6
|
+
}
|
|
4
7
|
export declare class IORedisStore implements Store {
|
|
5
8
|
name: string;
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated
|
|
11
|
+
*/
|
|
6
12
|
isCacheableValue: any;
|
|
13
|
+
isCacheable: any;
|
|
7
14
|
private redisCache;
|
|
8
15
|
private storeArgs;
|
|
9
|
-
constructor(
|
|
16
|
+
constructor(options?: (RedisOptions | {
|
|
17
|
+
clusterConfig: RedisClusterConfig;
|
|
18
|
+
}) & Config);
|
|
19
|
+
get client(): Redis | Cluster;
|
|
10
20
|
getClient(): Redis | Cluster;
|
|
11
|
-
set(key: string, value: any,
|
|
12
|
-
get(key: string, options?: any
|
|
13
|
-
del(key: string
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
21
|
+
set(key: string, value: any, ttl?: number): Promise<void>;
|
|
22
|
+
get<T = any>(key: string, options?: any): Promise<T | undefined>;
|
|
23
|
+
del(key: string): Promise<void>;
|
|
24
|
+
mset(args: [string, unknown][], ttl?: number): Promise<void>;
|
|
25
|
+
mget(...args: string[]): Promise<unknown[]>;
|
|
26
|
+
mdel(...args: string[]): Promise<void>;
|
|
27
|
+
reset(): Promise<void>;
|
|
28
|
+
keys(pattern?: string): Promise<string[]>;
|
|
29
|
+
ttl(key: string): Promise<number>;
|
|
30
|
+
private getTtl;
|
|
31
|
+
private assertCacheable;
|
|
17
32
|
}
|
|
18
|
-
export declare const ioRedisStore:
|
|
19
|
-
create: (...args: any[]) => IORedisStore;
|
|
20
|
-
};
|
|
33
|
+
export declare const ioRedisStore: (config?: FactoryConfig<any>) => IORedisStore;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/ioredis",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.3.0",
|
|
4
4
|
"description": "IO Redis connection for Ts.ED",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -24,15 +24,15 @@
|
|
|
24
24
|
"tslib": "2.4.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@tsed/core": "7.
|
|
28
|
-
"@tsed/di": "7.
|
|
27
|
+
"@tsed/core": "7.3.0",
|
|
28
|
+
"@tsed/di": "7.3.0",
|
|
29
29
|
"@types/ioredis-mock": "^5.6.0",
|
|
30
30
|
"ioredis": "5.2.3",
|
|
31
31
|
"ioredis-mock": "8.2.2"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@tsed/core": "^7.
|
|
35
|
-
"@tsed/di": "^7.
|
|
34
|
+
"@tsed/core": "^7.3.0",
|
|
35
|
+
"@tsed/di": "^7.3.0",
|
|
36
36
|
"ioredis": ">=5.2.3",
|
|
37
37
|
"ioredis-mock": ">=8.2.2"
|
|
38
38
|
},
|