-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
66 lines (55 loc) · 1.37 KB
/
index.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
import * as redis from "redis"
export class Handler {
private client: redis.RedisClient = null;
constructor(public config: any = null) {
this.client = redis.createClient(config);
}
get(key: string, callback: Function) {
if(this.config.prefix) {
key = this.config.prefix + "_" + key;
}
this.client.HGETALL(key, (err, data) => {
if (err !== null)
return callback(err, null);
if (data !== null) {
data.header = JSON.parse(data.header);
return callback(null, data);
}
return callback(null, null);
});
}
set(key: string, value: Buffer, header: any, rawUrl: string, expire: number = null, callback?: Function) {
if(this.config.prefix) {
key = this.config.prefix + "_" + key;
}
this.client.HMSET(key, [
"content", value.toString(),
"header", JSON.stringify(header),
"url", rawUrl
], () => {
if (expire !== null) {
this.client.EXPIRE(key, expire);
}
else if (this.config.default_expire) {
this.client.EXPIRE(key, this.config.default_expire);
}
callback && callback(key);
});
}
del(key: string) {
if(this.config.prefix) {
key = this.config.prefix + "_" + key;
}
this.client.DEL(key);
}
delGroup(group: string) {
if(this.config.prefix) {
group = this.config.prefix + "_" + group;
}
this.client.KEYS(group + "_*", function(err, rows) {
if(!err) {
this.client.DEL(rows);
}
});
}
}