@tiledesk/tiledesk-server 2.4.99 → 2.4.101

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,253 @@
1
+ const redis = require('redis');
2
+
3
+ class TdCache {
4
+
5
+ constructor(config) {
6
+ this.redis_host = config.host;
7
+ this.redis_port = config.port;
8
+ this.redis_password = config.password;
9
+ this.client = null;
10
+ }
11
+
12
+ async connect(callback) {
13
+ // client = redis.createClient();
14
+ return new Promise( async (resolve, reject) => {
15
+ this.client = redis.createClient(
16
+ {
17
+ host: this.redis_host,
18
+ port: this.redis_port,
19
+ password: this.redis_password
20
+ });
21
+ this.client.on('error', err => {
22
+ reject(err);
23
+ if (callback) {
24
+ callback(err);
25
+ }
26
+ });
27
+ // this.client.on('connect', function() {
28
+ // console.log('Redis Connected!');
29
+ // });
30
+ this.client.on('ready',function() {
31
+ resolve();
32
+ if (callback) {
33
+ callback();
34
+ }
35
+ //console.log("Redis is ready.");
36
+ });
37
+ });
38
+ }
39
+
40
+ async set(key, value, options) {
41
+ //console.log("setting key value", key, value)
42
+ if (!options) {
43
+ options = {EX: 86400}
44
+ }
45
+ return new Promise( async (resolve, reject) => {
46
+ if (options && options.EX) {
47
+ //console.log("expires:", options.EX)
48
+ try {
49
+ await this.client.set(
50
+ key,
51
+ value,
52
+ 'EX', options.EX);
53
+ }
54
+ catch(error) {
55
+ reject(error)
56
+ }
57
+ }
58
+ else {
59
+ try {
60
+ //console.log("setting here...key", key, value)
61
+ await this.client.set(
62
+ key,
63
+ value);
64
+ }
65
+ catch(error) {
66
+ console.error("Error", error);
67
+ reject(error)
68
+ }
69
+ }
70
+ if (options && options.callback) {
71
+ options.callback();
72
+ }
73
+ //console.log("resolving...", key);
74
+ return resolve();
75
+ });
76
+ }
77
+
78
+ async incr(key) {
79
+ // console.log("incr key:", key)
80
+ return new Promise( async (resolve, reject) => {
81
+ try {
82
+ // console.log("incr here...key", key)
83
+ await this.client.incr(key);
84
+ }
85
+ catch(error) {
86
+ console.error("Error on incr:", error);
87
+ reject(error)
88
+ }
89
+ return resolve();
90
+ });
91
+ }
92
+
93
+ async incrby(key, increment) {
94
+ return new Promise( async (resolve, reject) => {
95
+ try {
96
+ await this.client.incrby(key, increment);
97
+ }
98
+ catch(error) {
99
+ console.error("Error on incrby:", error);
100
+ reject(error)
101
+ }
102
+ return resolve()
103
+ })
104
+ }
105
+
106
+ async hset(dict_key, key, value, options) {
107
+ //console.log("hsetting dict_key key value", dict_key, key, value)
108
+ return new Promise( async (resolve, reject) => {
109
+ if (options && options.EX) {
110
+ //console.log("expires:", options.EX)
111
+ try {
112
+ await this.client.hset(
113
+ dict_key,
114
+ key,
115
+ value,
116
+ 'EX', options.EX);
117
+ }
118
+ catch(error) {
119
+ reject(error)
120
+ }
121
+ }
122
+ else {
123
+ try {
124
+ //console.log("setting here...key", key, value)
125
+ await this.client.hset(
126
+ dict_key,
127
+ key,
128
+ value);
129
+ }
130
+ catch(error) {
131
+ console.error("Error", error);
132
+ reject(error)
133
+ }
134
+ }
135
+ if (options && options.callback) {
136
+ options.callback();
137
+ }
138
+ return resolve();
139
+ });
140
+ }
141
+
142
+ async hdel(dict_key, key, options) {
143
+ //console.log("hsetting dict_key key value", dict_key, key, value)
144
+ return new Promise( async (resolve, reject) => {
145
+ if (options && options.EX) {
146
+ //console.log("expires:", options.EX)
147
+ try {
148
+ await this.client.hdel(
149
+ dict_key,
150
+ key,
151
+ 'EX', options.EX);
152
+ }
153
+ catch(error) {
154
+ reject(error)
155
+ }
156
+ }
157
+ else {
158
+ try {
159
+ //console.log("setting here...key", key, value)
160
+ await this.client.hdel(
161
+ dict_key,
162
+ key);
163
+ }
164
+ catch(error) {
165
+ console.error("Error", error);
166
+ reject(error);
167
+ }
168
+ }
169
+ if (options && options.callback) {
170
+ options.callback();
171
+ }
172
+ return resolve();
173
+ });
174
+ }
175
+
176
+ async setJSON(key, value, options) {
177
+ const _string = JSON.stringify(value);
178
+ return await this.set(key, _string, options);
179
+ }
180
+
181
+ async get(key, callback) {
182
+ return new Promise( async (resolve, reject) => {
183
+ this.client.get(key, (err, value) => {
184
+ if (err) {
185
+ reject(err);
186
+ }
187
+ else {
188
+ if (callback) {
189
+ callback(value);
190
+ }
191
+ return resolve(value);
192
+ }
193
+ });
194
+ });
195
+ }
196
+
197
+ async hgetall(dict_key, callback) {
198
+ //console.log("hgetting dics", dict_key);
199
+ return new Promise( async (resolve, reject) => {
200
+ this.client.hgetall(dict_key, (err, value) => {
201
+ if (err) {
202
+ reject(err);
203
+ if (callback) {
204
+ callback(err, null);
205
+ }
206
+ }
207
+ else {
208
+ if (callback) {
209
+ callback(null, value);
210
+ }
211
+ resolve(value);
212
+ }
213
+ });
214
+ });
215
+ }
216
+
217
+ async hget(dict_key, key, callback) {
218
+ //console.log("hgetting dics", dict_key);
219
+ return new Promise( async (resolve, reject) => {
220
+ this.client.hget(dict_key, key, (err, value) => {
221
+ if (err) {
222
+ reject(err);
223
+ if (callback) {
224
+ callback(err, null);
225
+ }
226
+ }
227
+ else {
228
+ if (callback) {
229
+ callback(null, value);
230
+ }
231
+ resolve(value);
232
+ }
233
+ });
234
+ });
235
+ }
236
+
237
+ async getJSON(key, callback) {
238
+ const value = await this.get(key);
239
+ return JSON.parse(value);
240
+ }
241
+
242
+ async del(key, callback) {
243
+ return new Promise( async (resolve, reject) => {
244
+ await this.client.del(key);
245
+ if (callback) {
246
+ callback();
247
+ }
248
+ return resolve();
249
+ })
250
+ }
251
+ }
252
+
253
+ module.exports = { TdCache };