/
ioredis.ts
53 lines (44 loc) 路 964 Bytes
/
ioredis.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
class Redis {
private keyValue = {};
private timeouts = new Set<NodeJS.Timer>();
async del(key: string) {
delete this.keyValue[key];
return true;
}
async get(key: string) {
if (this.keyValue[key]) {
return this.keyValue[key].value;
}
}
async mget(...keys: string[]) {
return keys.map((key) => {
if (this.keyValue[key]) {
return this.keyValue[key].value;
}
});
}
async set(key, value, type, ttl) {
this.keyValue[key] = {
value,
ttl,
};
if (ttl) {
const timeout = setTimeout(() => {
this.timeouts.delete(timeout);
delete this.keyValue[key];
}, ttl * 1000);
this.timeouts.add(timeout);
}
return true;
}
nodes() {
return [];
}
async flushdb() {}
async quit() {
this.timeouts.forEach((t) => clearTimeout(t));
}
}
// Use the same mock as Redis.Cluster.
(Redis as any).Cluster = Redis;
export default Redis;