-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTdCache.js
More file actions
101 lines (89 loc) · 2.32 KB
/
TdCache.js
File metadata and controls
101 lines (89 loc) · 2.32 KB
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
const redis = require('redis');
class TdCache {
constructor(config) {
this.redis_host = config.host;
this.redis_port = config.port;
this.redis_password = config.password;
this.client = null;
}
async connect(callback) {
// client = redis.createClient();
return new Promise( async (resolve, reject) => {
/**
* Connect redis client
*/
this.client = redis.createClient(
{
url: `redis://${this.redis_host}:${this.redis_port}`,
password: this.redis_password
});
this.client.on('error', err => {
reject(err);
if (callback) {
callback(err);
}
});
this.client.on('ready',function() {
resolve();
if (callback) {
callback();
}
});
await this.client.connect();
});
}
async set(key, value, options) {
//console.log("setting key value", key, value)
if (!options) {
options = {EX: 86400}
}
await this.client.set(
key,
value,
options);
}
async hset(dict_key, key, value, options) {
if (!value) {
return;
}
if (!options) {
options = {EX: 86400}
}
await this.client.HSET(
dict_key,
key,
value,
options);
}
async setJSON(key, value, options) {
if (!value) {
return;
}
if (!options) {
options = {EX: 86400}
}
const _string = JSON.stringify(value);
return await this.set(key, _string, options);
}
async get(key) {
const value = await this.client.GET(key);
return value;
}
async hgetall(dict_key) {
const all = await this.client.HGETALL(dict_key);
return all;
}
async hget(dict_key, key) {
// console.log("hgetting dics", dict_key);
const value = await this.client.HGET(dict_key, key);
return value;
}
async getJSON(key, callback) {
const value = await this.get(key);
return JSON.parse(value);
}
async del(key) {
await this.client.del(key);
}
}
module.exports = { TdCache };