-
Notifications
You must be signed in to change notification settings - Fork 2k
/
RedisCache.ts
62 lines (53 loc) · 1.62 KB
/
RedisCache.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
import {
TestableKeyValueCache,
KeyValueCacheSetOptions,
} from 'apollo-server-caching';
import Redis, { RedisOptions } from 'ioredis';
import DataLoader from 'dataloader';
export class RedisCache implements TestableKeyValueCache<string> {
readonly client: any;
readonly defaultSetOptions: KeyValueCacheSetOptions = {
ttl: 300,
};
private loader: DataLoader<string, string>;
constructor(options?: RedisOptions) {
const client = new Redis(options);
this.client = client;
this.loader = new DataLoader(keys => this.client.mget(keys), {
cache: false,
});
}
async set(
key: string,
value: string,
options?: KeyValueCacheSetOptions,
): Promise<void> {
const { ttl } = Object.assign({}, this.defaultSetOptions, options);
if (typeof ttl === 'number') {
await this.client.set(key, value, '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, value);
}
}
async get(key: string): Promise<string | undefined> {
const reply = await this.loader.load(key);
if (reply !== null) {
return reply;
}
return;
}
async delete(key: string): Promise<boolean> {
return await this.client.del(key);
}
// Drops all data from Redis. This should only be used by test suites ---
// production code should never drop all data from an end user Redis cache!
async flush(): Promise<void> {
await this.client.flushdb();
}
async close(): Promise<void> {
await this.client.quit();
return;
}
}