-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
IORedisStore.ts
128 lines (95 loc) · 3.08 KB
/
IORedisStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import type {Config, FactoryConfig, Store} from "cache-manager";
import Redis, {Cluster, ClusterNode, ClusterOptions, RedisOptions} from "ioredis";
export interface RedisClusterConfig {
nodes: ClusterNode[];
options?: ClusterOptions;
}
const getVal = (value: unknown) => JSON.stringify(value) || '"undefined"';
export type IORedisStoreCtrOptions = (RedisOptions | {clusterConfig: RedisClusterConfig}) & Config & {redisInstance?: Redis | Cluster};
export class IORedisStore implements Store {
public name = "redis";
/**
* @deprecated
*/
public isCacheableValue;
public isCacheable;
private redisCache: Redis | Cluster;
private storeArgs: any;
constructor(options?: IORedisStoreCtrOptions) {
options = options || {};
this.redisCache =
options.redisInstance ||
("clusterConfig" in options ? new Redis.Cluster(options.clusterConfig.nodes, options.clusterConfig.options) : new Redis(options));
this.storeArgs = this.redisCache.options;
this.isCacheable = this.isCacheableValue =
this.storeArgs.isCacheable || this.storeArgs.isCacheableValue || ((value: any) => value !== undefined && value !== null);
}
get client() {
return this.redisCache;
}
getClient() {
return this.redisCache;
}
async set(key: string, value: any, ttl?: number) {
this.assertCacheable(value);
ttl = this.getTtl(ttl);
const val = getVal(value);
if (ttl) {
await this.redisCache.setex(key, ttl, val);
return;
}
await this.redisCache.set(key, val);
}
async get<T = any>(key: string, options?: any) {
const val = await this.redisCache.get(key);
if (val === undefined || val === null) {
return undefined;
}
return JSON.parse(val) as T;
}
async del(key: string) {
await this.redisCache.del(key);
}
async mset(args: [string, unknown][], ttl?: number) {
ttl = this.getTtl(ttl);
if (ttl) {
const multi = this.redisCache.multi();
for (const [key, value] of args) {
this.assertCacheable(value);
multi.setex(key, ttl / 1000, getVal(value));
}
await multi.exec();
} else
await this.redisCache.mset(
args.flatMap(([key, value]) => {
this.assertCacheable(value);
return [key, getVal(value)] as [string, string];
})
);
}
async mget(...args: string[]) {
const x = await this.redisCache.mget(args);
return x.map((x) => (x === null || x === undefined ? undefined : (JSON.parse(x) as unknown)));
}
async mdel(...args: string[]) {
await this.redisCache.del(args);
}
async reset() {
await this.redisCache.flushall();
}
keys(pattern: string = "*") {
return this.redisCache.keys(pattern);
}
ttl(key: string) {
return this.redisCache.ttl(key);
}
private getTtl(ttl?: number) {
return ttl === undefined ? this.storeArgs.ttl : ttl;
}
private assertCacheable(value: any) {
if (!this.isCacheable(value)) {
throw new Error(`"${getVal(value)}" is not a cacheable value`);
}
}
}
export const ioRedisStore = (config?: FactoryConfig<any>) => new IORedisStore(config);