-
Notifications
You must be signed in to change notification settings - Fork 10
/
TdCache.js
169 lines (158 loc) · 4.33 KB
/
TdCache.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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) => {
this.client = redis.createClient(
{
host: this.redis_host,
port: this.redis_port,
password: this.redis_password
});
this.client.on('error', err => {
console.error('Redis connection error', err);
reject(err);
if (callback) {
callback(err);
}
});
// this.client.on('connect', function() {
// console.log('Redis Connected!');
// });
this.client.on('ready',function() {
resolve();
if (callback) {
callback();
}
});
});
}
async set(key, value, options) {
return new Promise( async (resolve, reject) => {
if (options && options.EX) {
try {
await this.client.set(
key,
value,
'EX', options.EX);
}
catch(error) {
reject(error)
}
}
else {
try {
//console.log("setting here...key", key, value)
await this.client.set(
key,
value);
}
catch(error) {
console.error("Error", error);
reject(error)
}
}
if (options && options.callback) {
options.callback();
}
//console.log("resolving...", key);
return resolve();
});
}
async hset(dict_key, key, value, options) {
//console.log("hsetting dict_key key value", dict_key, key, value)
return new Promise( async (resolve, reject) => {
if (options && options.EX) {
//console.log("expires:", options.EX)
try {
await this.client.hset(
dict_key,
key,
value,
'EX', options.EX);
}
catch(error) {
reject(error)
}
}
else {
try {
//console.log("setting here...key", key, value)
await this.client.hset(
dict_key,
key,
value);
}
catch(error) {
console.error("Error", error);
reject(error)
}
}
if (options && options.callback) {
options.callback();
}
return resolve();
});
}
async setJSON(key, value, options) {
const _string = JSON.stringify(value);
return await this.set(key, _string, options);
}
async get(key, callback) {
console.log("getting key:", key)
return new Promise( async (resolve, reject) => {
this.client.get(key, (err, value) => {
console.log("Got something with redis", key, value, err);
if (err) {
console.error("Error on redis get()", err);
reject(err);
}
else {
if (callback) {
callback(value);
}
return resolve(value);
}
});
});
}
async hgetall(dict_key, callback) {
//console.log("hgetting dics", dict_key);
return new Promise( async (resolve, reject) => {
this.client.hgetall(dict_key, (err, value) => {
if (err) {
reject(err);
if (callback) {
callback(err, null);
}
}
else {
if (callback) {
callback(null, value);
}
resolve(value);
}
});
});
}
async getJSON(key, callback) {
const value = await this.get(key);
return JSON.parse(value);
}
async del(key, callback) {
return new Promise( async (resolve, reject) => {
await this.client.del(key);
if (callback) {
callback();
}
return resolve();
})
}
}
module.exports = { TdCache };