-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathRedisClusterCache.ts
64 lines (55 loc) · 1.71 KB
/
RedisClusterCache.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
import { KeyValueCache, KeyValueCacheSetOptions } from 'apollo-server-caching';
import Redis, {
ClusterOptions,
ClusterNode,
Redis as RedisInstance,
} from 'ioredis';
import DataLoader from 'dataloader';
export class RedisClusterCache implements KeyValueCache {
readonly client: any;
readonly defaultSetOptions: KeyValueCacheSetOptions = {
ttl: 300,
};
private loader: DataLoader<string, string>;
constructor(nodes: ClusterNode[], options?: ClusterOptions) {
this.client = new Redis.Cluster(nodes, options);
this.loader = new DataLoader(
(keys = []) =>
Promise.all(keys.map(key => this.client.get(key).catch(() => null))),
{ cache: false },
);
}
async set(
key: string,
data: string,
options?: KeyValueCacheSetOptions,
): Promise<void> {
const { ttl } = Object.assign({}, this.defaultSetOptions, options);
if (typeof ttl === 'number') {
await this.client.set(key, data, 'EX', ttl);
} else {
// We'll leave out the EXpiration when no value is specified. Of course,
// it may be purged from the cache for other reasons as deemed necessary.
await this.client.set(key, data);
}
}
async get(key: string): Promise<string | undefined> {
const reply = await this.loader.load(key);
// reply is null if key is not found
if (reply !== null) {
return reply;
}
return;
}
async delete(key: string): Promise<boolean> {
return await this.client.del(key);
}
async flush(): Promise<void> {
const masters = this.client.nodes('master') || [];
await Promise.all(masters.map((node: RedisInstance) => node.flushdb()));
}
async close(): Promise<void> {
await this.client.quit();
return;
}
}