koatty_store 1.4.8 → 1.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +2 -1
- package/.vscode/launch.json +25 -0
- package/CHANGELOG.md +3 -7
- package/LICENSE +1 -1
- package/babel.config.js +0 -5
- package/dist/index.d.ts +20 -28
- package/dist/index.js +14 -20
- package/dist/index.js.map +1 -1
- package/dist/{memory.d.ts → store/memory.d.ts} +8 -41
- package/dist/{memory.js → store/memory.js} +12 -56
- package/dist/store/memory.js.map +1 -0
- package/dist/store/memory_cache.d.ts +553 -0
- package/dist/store/memory_cache.js +1149 -0
- package/dist/store/memory_cache.js.map +1 -0
- package/dist/store/redis.d.ts +73 -0
- package/dist/store/redis.js +228 -0
- package/dist/store/redis.js.map +1 -0
- package/dist/{redis.d.ts → store.d.ts} +36 -105
- package/dist/store.js +383 -0
- package/dist/store.js.map +1 -0
- package/jest.config.js +19 -1
- package/jest_html_reporters.html +1 -1
- package/package.json +18 -20
- package/tsconfig.json +4 -4
- package/dist/memory.js.map +0 -1
- package/dist/redis.js +0 -545
- package/dist/redis.js.map +0 -1
- package/tslint.json +0 -85
- package/yarn-error.log +0 -6080
|
@@ -0,0 +1,1149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MemoryCache = exports.messages = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
/*
|
|
6
|
+
* @Description:
|
|
7
|
+
* @Usage:
|
|
8
|
+
* @Author: richen
|
|
9
|
+
* @Date: 2021-12-02 11:03:20
|
|
10
|
+
* @LastEditTime: 2021-12-02 14:36:11
|
|
11
|
+
*/
|
|
12
|
+
const lodash_1 = require("lodash");
|
|
13
|
+
const Helper = (0, tslib_1.__importStar)(require("koatty_lib"));
|
|
14
|
+
const events_1 = require("events");
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
*
|
|
18
|
+
* @enum {number}
|
|
19
|
+
*/
|
|
20
|
+
var messages;
|
|
21
|
+
(function (messages) {
|
|
22
|
+
messages["ok"] = "OK";
|
|
23
|
+
messages["queued"] = "QUEUED";
|
|
24
|
+
messages["pong"] = "PONG";
|
|
25
|
+
messages["noint"] = "ERR value is not an integer or out of range";
|
|
26
|
+
messages["nofloat"] = "ERR value is not an float or out of range";
|
|
27
|
+
messages["nokey"] = "ERR no such key";
|
|
28
|
+
messages["nomultiinmulti"] = "ERR MULTI calls can not be nested";
|
|
29
|
+
messages["nomultiexec"] = "ERR EXEC without MULTI";
|
|
30
|
+
messages["nomultidiscard"] = "ERR DISCARD without MULTI";
|
|
31
|
+
messages["busykey"] = "ERR target key name is busy";
|
|
32
|
+
messages["syntax"] = "ERR syntax error";
|
|
33
|
+
messages["unsupported"] = "MemoryCache does not support that operation";
|
|
34
|
+
messages["wrongTypeOp"] = "WRONGTYPE Operation against a key holding the wrong kind of value";
|
|
35
|
+
messages["wrongPayload"] = "DUMP payload version or checksum are wrong";
|
|
36
|
+
messages["wrongArgCount"] = "ERR wrong number of arguments for '%0' command";
|
|
37
|
+
messages["bitopnotWrongCount"] = "ERR BITOP NOT must be called with a single source key";
|
|
38
|
+
messages["indexOutOfRange"] = "ERR index out of range";
|
|
39
|
+
messages["invalidLexRange"] = "ERR min or max not valid string range item";
|
|
40
|
+
messages["invalidDBIndex"] = "ERR invalid DB index";
|
|
41
|
+
messages["invalidDBIndexNX"] = "ERR invalid DB index, '%0' does not exist";
|
|
42
|
+
messages["mutuallyExclusiveNXXX"] = "ERR XX and NX options at the same time are not compatible";
|
|
43
|
+
})(messages = exports.messages || (exports.messages = {}));
|
|
44
|
+
class MemoryCache extends events_1.EventEmitter {
|
|
45
|
+
/**
|
|
46
|
+
* Creates an instance of MemoryCache.
|
|
47
|
+
* @param {*} options
|
|
48
|
+
* @memberof MemoryCache
|
|
49
|
+
*/
|
|
50
|
+
constructor(options) {
|
|
51
|
+
super();
|
|
52
|
+
this.databases = Object.create({});
|
|
53
|
+
this.options = { ...{ database: "0" }, ...options };
|
|
54
|
+
this.currentDBIndex = 0;
|
|
55
|
+
this.connected = false;
|
|
56
|
+
this.lastSave = Date.now();
|
|
57
|
+
this.multiMode = false;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
*
|
|
61
|
+
*
|
|
62
|
+
* @returns {*}
|
|
63
|
+
* @memberof MemoryCache
|
|
64
|
+
*/
|
|
65
|
+
createClient() {
|
|
66
|
+
this.databases[this.options.database] = Object.create({});
|
|
67
|
+
this.cache = this.databases[this.options.database];
|
|
68
|
+
this.connected = true;
|
|
69
|
+
// exit multi mode if we are in it
|
|
70
|
+
this.discard(null, true);
|
|
71
|
+
this.emit('connect');
|
|
72
|
+
this.emit('ready');
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
*
|
|
77
|
+
*
|
|
78
|
+
* @returns {*}
|
|
79
|
+
* @memberof MemoryCache
|
|
80
|
+
*/
|
|
81
|
+
quit() {
|
|
82
|
+
this.connected = false;
|
|
83
|
+
// exit multi mode if we are in it
|
|
84
|
+
this.discard(null, true);
|
|
85
|
+
this.emit('end');
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
*
|
|
90
|
+
*
|
|
91
|
+
* @returns {*}
|
|
92
|
+
* @memberof MemoryCache
|
|
93
|
+
*/
|
|
94
|
+
end() {
|
|
95
|
+
return this.quit();
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
*
|
|
99
|
+
*
|
|
100
|
+
* @param {string} message
|
|
101
|
+
* @param {Function} [callback]
|
|
102
|
+
* @returns {*}
|
|
103
|
+
* @memberof MemoryCache
|
|
104
|
+
*/
|
|
105
|
+
echo(message, callback) {
|
|
106
|
+
return this._handleCallback(callback, message);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
*
|
|
110
|
+
*
|
|
111
|
+
* @param {string} message
|
|
112
|
+
* @param {Function} [callback]
|
|
113
|
+
* @returns {*}
|
|
114
|
+
* @memberof MemoryCache
|
|
115
|
+
*/
|
|
116
|
+
ping(message, callback) {
|
|
117
|
+
message = message || messages.pong;
|
|
118
|
+
return this._handleCallback(callback, message);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
*
|
|
122
|
+
*
|
|
123
|
+
* @param {string} password
|
|
124
|
+
* @param {Function} [callback]
|
|
125
|
+
* @returns {*}
|
|
126
|
+
* @memberof MemoryCache
|
|
127
|
+
*/
|
|
128
|
+
auth(password, callback) {
|
|
129
|
+
return this._handleCallback(callback, messages.ok);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
*
|
|
133
|
+
*
|
|
134
|
+
* @param {number} dbIndex
|
|
135
|
+
* @param {Function} [callback]
|
|
136
|
+
* @returns {*}
|
|
137
|
+
* @memberof MemoryCache
|
|
138
|
+
*/
|
|
139
|
+
select(dbIndex, callback) {
|
|
140
|
+
if (!Helper.isNumber(dbIndex)) {
|
|
141
|
+
return this._handleCallback(callback, null, messages.invalidDBIndex);
|
|
142
|
+
}
|
|
143
|
+
if (!this.databases.hasOwnProperty(dbIndex)) {
|
|
144
|
+
this.databases[dbIndex] = Object.create({});
|
|
145
|
+
}
|
|
146
|
+
this.multiMode = false;
|
|
147
|
+
this.currentDBIndex = dbIndex;
|
|
148
|
+
this.cache = this.databases[dbIndex];
|
|
149
|
+
return this._handleCallback(callback, messages.ok);
|
|
150
|
+
}
|
|
151
|
+
// ---------------------------------------
|
|
152
|
+
// Keys
|
|
153
|
+
// ---------------------------------------
|
|
154
|
+
get(key, callback) {
|
|
155
|
+
let retVal = null;
|
|
156
|
+
if (this._hasKey(key)) {
|
|
157
|
+
this._testType(key, 'string', true, callback);
|
|
158
|
+
retVal = this._getKey(key);
|
|
159
|
+
}
|
|
160
|
+
return this._handleCallback(callback, retVal);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* set(key, value, ttl, pttl, notexist, onlyexist, callback)
|
|
164
|
+
*
|
|
165
|
+
* @param {string} key
|
|
166
|
+
* @param {(string | number)} value
|
|
167
|
+
* @param {...any[]} params
|
|
168
|
+
* @returns {*}
|
|
169
|
+
* @memberof MemoryCache
|
|
170
|
+
*/
|
|
171
|
+
set(key, value, ...params) {
|
|
172
|
+
const retVal = null;
|
|
173
|
+
params = (0, lodash_1.flatten)(params);
|
|
174
|
+
const callback = this._retrieveCallback(params);
|
|
175
|
+
let ttl, pttl, notexist, onlyexist;
|
|
176
|
+
// parse parameters
|
|
177
|
+
while (params.length > 0) {
|
|
178
|
+
const param = params.shift();
|
|
179
|
+
switch (param.toString().toLowerCase()) {
|
|
180
|
+
case 'nx':
|
|
181
|
+
notexist = true;
|
|
182
|
+
break;
|
|
183
|
+
case 'xx':
|
|
184
|
+
onlyexist = true;
|
|
185
|
+
break;
|
|
186
|
+
case 'ex':
|
|
187
|
+
if (params.length === 0) {
|
|
188
|
+
return this._handleCallback(callback, null, messages.syntax);
|
|
189
|
+
}
|
|
190
|
+
ttl = parseInt(params.shift());
|
|
191
|
+
if (isNaN(ttl)) {
|
|
192
|
+
return this._handleCallback(callback, null, messages.noint);
|
|
193
|
+
}
|
|
194
|
+
break;
|
|
195
|
+
case 'px':
|
|
196
|
+
if (params.length === 0) {
|
|
197
|
+
return this._handleCallback(callback, null, messages.syntax);
|
|
198
|
+
}
|
|
199
|
+
pttl = parseInt(params.shift());
|
|
200
|
+
if (isNaN(pttl)) {
|
|
201
|
+
return this._handleCallback(callback, null, messages.noint);
|
|
202
|
+
}
|
|
203
|
+
break;
|
|
204
|
+
default:
|
|
205
|
+
return this._handleCallback(callback, null, messages.syntax);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (!(0, lodash_1.isNil)(ttl) && !(0, lodash_1.isNil)(pttl)) {
|
|
209
|
+
return this._handleCallback(callback, null, messages.syntax);
|
|
210
|
+
}
|
|
211
|
+
if (notexist && onlyexist) {
|
|
212
|
+
return this._handleCallback(callback, null, messages.syntax);
|
|
213
|
+
}
|
|
214
|
+
pttl = pttl || ttl * 1000 || null;
|
|
215
|
+
if (!(0, lodash_1.isNil)(pttl)) {
|
|
216
|
+
pttl = Date.now() + pttl;
|
|
217
|
+
}
|
|
218
|
+
if (this._hasKey(key)) {
|
|
219
|
+
this._testType(key, 'string', true, callback);
|
|
220
|
+
if (notexist) {
|
|
221
|
+
return this._handleCallback(callback, retVal);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else if (onlyexist) {
|
|
225
|
+
return this._handleCallback(callback, retVal);
|
|
226
|
+
}
|
|
227
|
+
this.cache[key] = this._makeKey(value.toString(), 'string', pttl);
|
|
228
|
+
return this._handleCallback(callback, messages.ok);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
*
|
|
232
|
+
*
|
|
233
|
+
* @param {string} key
|
|
234
|
+
* @param {Function} [callback]
|
|
235
|
+
* @returns {*}
|
|
236
|
+
* @memberof MemoryCache
|
|
237
|
+
*/
|
|
238
|
+
ttl(key, callback) {
|
|
239
|
+
let retVal = this.pttl(key);
|
|
240
|
+
if (retVal >= 0 || retVal <= -3) {
|
|
241
|
+
retVal = Math.floor(retVal / 1000);
|
|
242
|
+
}
|
|
243
|
+
return this._handleCallback(callback, retVal);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
*
|
|
247
|
+
*
|
|
248
|
+
* @param {string} key
|
|
249
|
+
* @param {number} seconds
|
|
250
|
+
* @param {Function} [callback]
|
|
251
|
+
* @returns {*}
|
|
252
|
+
* @memberof MemoryCache
|
|
253
|
+
*/
|
|
254
|
+
expire(key, seconds, callback) {
|
|
255
|
+
let retVal = 0;
|
|
256
|
+
if (this._hasKey(key)) {
|
|
257
|
+
this.cache[key].timeout = Date.now() + seconds * 1000;
|
|
258
|
+
retVal = 1;
|
|
259
|
+
}
|
|
260
|
+
return this._handleCallback(callback, retVal);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
*
|
|
264
|
+
*
|
|
265
|
+
* @param {...any[]} keys
|
|
266
|
+
* @returns {*}
|
|
267
|
+
* @memberof MemoryCache
|
|
268
|
+
*/
|
|
269
|
+
del(...keys) {
|
|
270
|
+
let retVal = 0;
|
|
271
|
+
const callback = this._retrieveCallback(keys);
|
|
272
|
+
// Flatten the array in case an array was passed
|
|
273
|
+
keys = (0, lodash_1.flatten)(keys);
|
|
274
|
+
for (let itr = 0; itr < keys.length; itr++) {
|
|
275
|
+
const key = keys[itr];
|
|
276
|
+
if (this._hasKey(key)) {
|
|
277
|
+
delete this.cache[key];
|
|
278
|
+
retVal++;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return this._handleCallback(callback, retVal);
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
*
|
|
285
|
+
*
|
|
286
|
+
* @param {...any[]} keys
|
|
287
|
+
* @returns {*}
|
|
288
|
+
* @memberof MemoryCache
|
|
289
|
+
*/
|
|
290
|
+
exists(...keys) {
|
|
291
|
+
let retVal = 0;
|
|
292
|
+
const callback = this._retrieveCallback(keys);
|
|
293
|
+
for (let itr = 0; itr < keys.length; itr++) {
|
|
294
|
+
const key = keys[itr];
|
|
295
|
+
if (this._hasKey(key)) {
|
|
296
|
+
retVal++;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return this._handleCallback(callback, retVal);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
*
|
|
303
|
+
*
|
|
304
|
+
* @param {string} key
|
|
305
|
+
* @param {Function} [callback]
|
|
306
|
+
* @returns {*}
|
|
307
|
+
* @memberof MemoryCache
|
|
308
|
+
*/
|
|
309
|
+
incr(key, callback) {
|
|
310
|
+
let retVal = null;
|
|
311
|
+
try {
|
|
312
|
+
retVal = this._addToKey(key, 1);
|
|
313
|
+
}
|
|
314
|
+
catch (err) {
|
|
315
|
+
return this._handleCallback(callback, null, err);
|
|
316
|
+
}
|
|
317
|
+
return this._handleCallback(callback, retVal);
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
*
|
|
321
|
+
*
|
|
322
|
+
* @param {string} key
|
|
323
|
+
* @param {number} amount
|
|
324
|
+
* @param {Function} [callback]
|
|
325
|
+
* @returns {*}
|
|
326
|
+
* @memberof MemoryCache
|
|
327
|
+
*/
|
|
328
|
+
incrby(key, amount, callback) {
|
|
329
|
+
let retVal = null;
|
|
330
|
+
try {
|
|
331
|
+
retVal = this._addToKey(key, amount);
|
|
332
|
+
}
|
|
333
|
+
catch (err) {
|
|
334
|
+
return this._handleCallback(callback, null, err);
|
|
335
|
+
}
|
|
336
|
+
return this._handleCallback(callback, retVal);
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
*
|
|
340
|
+
*
|
|
341
|
+
* @param {string} key
|
|
342
|
+
* @param {Function} [callback]
|
|
343
|
+
* @returns {*}
|
|
344
|
+
* @memberof MemoryCache
|
|
345
|
+
*/
|
|
346
|
+
decr(key, callback) {
|
|
347
|
+
let retVal = null;
|
|
348
|
+
try {
|
|
349
|
+
retVal = this._addToKey(key, -1);
|
|
350
|
+
}
|
|
351
|
+
catch (err) {
|
|
352
|
+
return this._handleCallback(callback, null, err);
|
|
353
|
+
}
|
|
354
|
+
return this._handleCallback(callback, retVal);
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
*
|
|
358
|
+
*
|
|
359
|
+
* @param {string} key
|
|
360
|
+
* @param {number} amount
|
|
361
|
+
* @param {Function} [callback]
|
|
362
|
+
* @returns {*}
|
|
363
|
+
* @memberof MemoryCache
|
|
364
|
+
*/
|
|
365
|
+
decrby(key, amount, callback) {
|
|
366
|
+
let retVal = null;
|
|
367
|
+
try {
|
|
368
|
+
retVal = this._addToKey(key, -amount);
|
|
369
|
+
}
|
|
370
|
+
catch (err) {
|
|
371
|
+
return this._handleCallback(callback, null, err);
|
|
372
|
+
}
|
|
373
|
+
return this._handleCallback(callback, retVal);
|
|
374
|
+
}
|
|
375
|
+
// ---------------------------------------
|
|
376
|
+
// ## Hash ##
|
|
377
|
+
// ---------------------------------------
|
|
378
|
+
hset(key, field, value, callback) {
|
|
379
|
+
let retVal = 0;
|
|
380
|
+
if (this._hasKey(key)) {
|
|
381
|
+
this._testType(key, 'hash', true, callback);
|
|
382
|
+
}
|
|
383
|
+
else {
|
|
384
|
+
this.cache[key] = this._makeKey({}, 'hash');
|
|
385
|
+
}
|
|
386
|
+
if (!this._hasField(key, field)) {
|
|
387
|
+
retVal = 1;
|
|
388
|
+
}
|
|
389
|
+
this._setField(key, field, value.toString());
|
|
390
|
+
this.persist(key);
|
|
391
|
+
return this._handleCallback(callback, retVal);
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
*
|
|
395
|
+
*
|
|
396
|
+
* @param {string} key
|
|
397
|
+
* @param {string} field
|
|
398
|
+
* @param {Function} [callback]
|
|
399
|
+
* @returns {*}
|
|
400
|
+
* @memberof MemoryCache
|
|
401
|
+
*/
|
|
402
|
+
hget(key, field, callback) {
|
|
403
|
+
let retVal = null;
|
|
404
|
+
if (this._hasKey(key)) {
|
|
405
|
+
this._testType(key, 'hash', true, callback);
|
|
406
|
+
if (this._hasField(key, field)) {
|
|
407
|
+
retVal = this._getKey(key)[field];
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return this._handleCallback(callback, retVal);
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
*
|
|
414
|
+
*
|
|
415
|
+
* @param {string} key
|
|
416
|
+
* @param {string} field
|
|
417
|
+
* @param {Function} [callback]
|
|
418
|
+
* @returns {*}
|
|
419
|
+
* @memberof MemoryCache
|
|
420
|
+
*/
|
|
421
|
+
hexists(key, field, callback) {
|
|
422
|
+
let retVal = 0;
|
|
423
|
+
if (this._hasKey(key)) {
|
|
424
|
+
this._testType(key, 'hash', true, callback);
|
|
425
|
+
if (this._hasField(key, field)) {
|
|
426
|
+
retVal = 1;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return this._handleCallback(callback, retVal);
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
*
|
|
433
|
+
*
|
|
434
|
+
* @param {string} key
|
|
435
|
+
* @param {...any[]} fields
|
|
436
|
+
* @returns {*}
|
|
437
|
+
* @memberof MemoryCache
|
|
438
|
+
*/
|
|
439
|
+
hdel(key, ...fields) {
|
|
440
|
+
let retVal = 0;
|
|
441
|
+
const callback = this._retrieveCallback(fields);
|
|
442
|
+
if (this._hasKey(key)) {
|
|
443
|
+
this._testType(key, 'hash', true, callback);
|
|
444
|
+
for (let itr = 0; itr < fields.length; itr++) {
|
|
445
|
+
const field = fields[itr];
|
|
446
|
+
if (this._hasField(key, field)) {
|
|
447
|
+
delete this.cache[key].value[field];
|
|
448
|
+
retVal++;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return this._handleCallback(callback, retVal);
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
*
|
|
456
|
+
*
|
|
457
|
+
* @param {string} key
|
|
458
|
+
* @param {Function} [callback]
|
|
459
|
+
* @returns {*}
|
|
460
|
+
* @memberof MemoryCache
|
|
461
|
+
*/
|
|
462
|
+
hlen(key, callback) {
|
|
463
|
+
const retVal = this.hkeys(key).length;
|
|
464
|
+
return this._handleCallback(callback, retVal);
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
*
|
|
468
|
+
*
|
|
469
|
+
* @param {string} key
|
|
470
|
+
* @param {string} field
|
|
471
|
+
* @param {*} value
|
|
472
|
+
* @param {Function} [callback]
|
|
473
|
+
* @returns {*}
|
|
474
|
+
* @memberof MemoryCache
|
|
475
|
+
*/
|
|
476
|
+
hincrby(key, field, value, callback) {
|
|
477
|
+
let retVal;
|
|
478
|
+
try {
|
|
479
|
+
retVal = this._addToField(key, field, value, false);
|
|
480
|
+
}
|
|
481
|
+
catch (err) {
|
|
482
|
+
return this._handleCallback(callback, null, err);
|
|
483
|
+
}
|
|
484
|
+
return this._handleCallback(callback, retVal);
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
*
|
|
488
|
+
*
|
|
489
|
+
* @param {string} key
|
|
490
|
+
* @param {Function} [callback]
|
|
491
|
+
* @returns {*}
|
|
492
|
+
* @memberof MemoryCache
|
|
493
|
+
*/
|
|
494
|
+
hgetall(key, callback) {
|
|
495
|
+
let retVals = {};
|
|
496
|
+
if (this._hasKey(key)) {
|
|
497
|
+
this._testType(key, 'hash', true, callback);
|
|
498
|
+
retVals = this._getKey(key);
|
|
499
|
+
}
|
|
500
|
+
return this._handleCallback(callback, retVals);
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
*
|
|
504
|
+
*
|
|
505
|
+
* @param {string} key
|
|
506
|
+
* @param {Function} [callback]
|
|
507
|
+
* @returns {*}
|
|
508
|
+
* @memberof MemoryCache
|
|
509
|
+
*/
|
|
510
|
+
hkeys(key, callback) {
|
|
511
|
+
let retVals = [];
|
|
512
|
+
if (this._hasKey(key)) {
|
|
513
|
+
this._testType(key, 'hash', true, callback);
|
|
514
|
+
retVals = Object.keys(this._getKey(key));
|
|
515
|
+
}
|
|
516
|
+
return this._handleCallback(callback, retVals);
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
*
|
|
520
|
+
*
|
|
521
|
+
* @param {string} key
|
|
522
|
+
* @param {Function} [callback]
|
|
523
|
+
* @returns {*}
|
|
524
|
+
* @memberof MemoryCache
|
|
525
|
+
*/
|
|
526
|
+
hvals(key, callback) {
|
|
527
|
+
let retVals = [];
|
|
528
|
+
if (this._hasKey(key)) {
|
|
529
|
+
this._testType(key, 'hash', true, callback);
|
|
530
|
+
retVals = Object.values(this._getKey(key));
|
|
531
|
+
}
|
|
532
|
+
return this._handleCallback(callback, retVals);
|
|
533
|
+
}
|
|
534
|
+
// ---------------------------------------
|
|
535
|
+
// Lists (Array / Queue / Stack)
|
|
536
|
+
// ---------------------------------------
|
|
537
|
+
/**
|
|
538
|
+
*
|
|
539
|
+
*
|
|
540
|
+
* @param {string} key
|
|
541
|
+
* @param {Function} [callback]
|
|
542
|
+
* @returns {*}
|
|
543
|
+
* @memberof MemoryCache
|
|
544
|
+
*/
|
|
545
|
+
llen(key, callback) {
|
|
546
|
+
let retVal = 0;
|
|
547
|
+
if (this._hasKey(key)) {
|
|
548
|
+
this._testType(key, 'list', true, callback);
|
|
549
|
+
retVal = this._getKey(key).length || 0;
|
|
550
|
+
}
|
|
551
|
+
return this._handleCallback(callback, retVal);
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
*
|
|
555
|
+
*
|
|
556
|
+
* @param {string} key
|
|
557
|
+
* @param {(string | number)} value
|
|
558
|
+
* @param {Function} [callback]
|
|
559
|
+
* @returns {*}
|
|
560
|
+
* @memberof MemoryCache
|
|
561
|
+
*/
|
|
562
|
+
rpush(key, value, callback) {
|
|
563
|
+
let retVal = 0;
|
|
564
|
+
if (this._hasKey(key)) {
|
|
565
|
+
this._testType(key, 'list', true, callback);
|
|
566
|
+
}
|
|
567
|
+
else {
|
|
568
|
+
this.cache[key] = this._makeKey([], 'list');
|
|
569
|
+
}
|
|
570
|
+
const val = this._getKey(key);
|
|
571
|
+
val.push(value);
|
|
572
|
+
this._setKey(key, val);
|
|
573
|
+
retVal = val.length;
|
|
574
|
+
return this._handleCallback(callback, retVal);
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
*
|
|
578
|
+
*
|
|
579
|
+
* @param {string} key
|
|
580
|
+
* @param {(string | number)} value
|
|
581
|
+
* @param {Function} [callback]
|
|
582
|
+
* @returns {*}
|
|
583
|
+
* @memberof MemoryCache
|
|
584
|
+
*/
|
|
585
|
+
lpush(key, value, callback) {
|
|
586
|
+
let retVal = 0;
|
|
587
|
+
if (this._hasKey(key)) {
|
|
588
|
+
this._testType(key, 'list', true, callback);
|
|
589
|
+
}
|
|
590
|
+
else {
|
|
591
|
+
this.cache[key] = this._makeKey([], 'list');
|
|
592
|
+
}
|
|
593
|
+
const val = this._getKey(key);
|
|
594
|
+
val.splice(0, 0, value);
|
|
595
|
+
this._setKey(key, val);
|
|
596
|
+
retVal = val.length;
|
|
597
|
+
return this._handleCallback(callback, retVal);
|
|
598
|
+
}
|
|
599
|
+
/**
|
|
600
|
+
*
|
|
601
|
+
*
|
|
602
|
+
* @param {string} key
|
|
603
|
+
* @param {Function} [callback]
|
|
604
|
+
* @returns {*}
|
|
605
|
+
* @memberof MemoryCache
|
|
606
|
+
*/
|
|
607
|
+
lpop(key, callback) {
|
|
608
|
+
let retVal = null;
|
|
609
|
+
if (this._hasKey(key)) {
|
|
610
|
+
this._testType(key, 'list', true, callback);
|
|
611
|
+
const val = this._getKey(key);
|
|
612
|
+
retVal = val.shift();
|
|
613
|
+
this._setKey(key, val);
|
|
614
|
+
}
|
|
615
|
+
return this._handleCallback(callback, retVal);
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
*
|
|
619
|
+
*
|
|
620
|
+
* @param {string} key
|
|
621
|
+
* @param {Function} [callback]
|
|
622
|
+
* @returns {*}
|
|
623
|
+
* @memberof MemoryCache
|
|
624
|
+
*/
|
|
625
|
+
rpop(key, callback) {
|
|
626
|
+
let retVal = null;
|
|
627
|
+
if (this._hasKey(key)) {
|
|
628
|
+
this._testType(key, 'list', true, callback);
|
|
629
|
+
const val = this._getKey(key);
|
|
630
|
+
retVal = val.pop();
|
|
631
|
+
this._setKey(key, val);
|
|
632
|
+
}
|
|
633
|
+
return this._handleCallback(callback, retVal);
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
*
|
|
637
|
+
*
|
|
638
|
+
* @param {string} key
|
|
639
|
+
* @param {number} start
|
|
640
|
+
* @param {number} stop
|
|
641
|
+
* @param {Function} [callback]
|
|
642
|
+
* @returns {*}
|
|
643
|
+
* @memberof MemoryCache
|
|
644
|
+
*/
|
|
645
|
+
lrange(key, start, stop, callback) {
|
|
646
|
+
const retVal = [];
|
|
647
|
+
if (this._hasKey(key)) {
|
|
648
|
+
this._testType(key, 'list', true, callback);
|
|
649
|
+
const val = this._getKey(key);
|
|
650
|
+
const length = val.length;
|
|
651
|
+
if (stop < 0) {
|
|
652
|
+
stop = length + stop;
|
|
653
|
+
}
|
|
654
|
+
if (start < 0) {
|
|
655
|
+
start = length + start;
|
|
656
|
+
}
|
|
657
|
+
if (start < 0) {
|
|
658
|
+
start = 0;
|
|
659
|
+
}
|
|
660
|
+
if (stop >= length) {
|
|
661
|
+
stop = length - 1;
|
|
662
|
+
}
|
|
663
|
+
if (stop >= 0 && stop >= start) {
|
|
664
|
+
const size = stop - start + 1;
|
|
665
|
+
for (let itr = start; itr < size; itr++) {
|
|
666
|
+
retVal.push(val[itr]);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
return this._handleCallback(callback, retVal);
|
|
671
|
+
}
|
|
672
|
+
// ---------------------------------------
|
|
673
|
+
// ## Sets (Unique Lists)##
|
|
674
|
+
// ---------------------------------------
|
|
675
|
+
/**
|
|
676
|
+
*
|
|
677
|
+
*
|
|
678
|
+
* @param {string} key
|
|
679
|
+
* @param {...any[]} members
|
|
680
|
+
* @returns {*}
|
|
681
|
+
* @memberof MemoryCache
|
|
682
|
+
*/
|
|
683
|
+
sadd(key, ...members) {
|
|
684
|
+
let retVal = 0;
|
|
685
|
+
const callback = this._retrieveCallback(members);
|
|
686
|
+
if (this._hasKey(key)) {
|
|
687
|
+
this._testType(key, 'set', true, callback);
|
|
688
|
+
}
|
|
689
|
+
else {
|
|
690
|
+
this.cache[key] = this._makeKey([], 'set');
|
|
691
|
+
}
|
|
692
|
+
const val = this._getKey(key);
|
|
693
|
+
const length = val.length;
|
|
694
|
+
const nval = (0, lodash_1.union)(val, members);
|
|
695
|
+
const newlength = nval.length;
|
|
696
|
+
retVal = newlength - length;
|
|
697
|
+
this._setKey(key, nval);
|
|
698
|
+
return this._handleCallback(callback, retVal);
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
*
|
|
702
|
+
*
|
|
703
|
+
* @param {string} key
|
|
704
|
+
* @param {Function} [callback]
|
|
705
|
+
* @returns {*}
|
|
706
|
+
* @memberof MemoryCache
|
|
707
|
+
*/
|
|
708
|
+
scard(key, callback) {
|
|
709
|
+
let retVal = 0;
|
|
710
|
+
if (this._hasKey(key)) {
|
|
711
|
+
this._testType(key, 'set', true, callback);
|
|
712
|
+
retVal = this._getKey(key).length;
|
|
713
|
+
}
|
|
714
|
+
return this._handleCallback(callback, retVal);
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
*
|
|
718
|
+
*
|
|
719
|
+
* @param {string} key
|
|
720
|
+
* @param {string} member
|
|
721
|
+
* @param {Function} [callback]
|
|
722
|
+
* @returns {*}
|
|
723
|
+
* @memberof MemoryCache
|
|
724
|
+
*/
|
|
725
|
+
sismember(key, member, callback) {
|
|
726
|
+
let retVal = 0;
|
|
727
|
+
if (this._hasKey(key)) {
|
|
728
|
+
this._testType(key, 'set', true, callback);
|
|
729
|
+
const val = this._getKey(key);
|
|
730
|
+
if (val.includes(member)) {
|
|
731
|
+
retVal = 1;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
return this._handleCallback(callback, retVal);
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
*
|
|
738
|
+
*
|
|
739
|
+
* @param {string} key
|
|
740
|
+
* @param {Function} [callback]
|
|
741
|
+
* @returns {*}
|
|
742
|
+
* @memberof MemoryCache
|
|
743
|
+
*/
|
|
744
|
+
smembers(key, callback) {
|
|
745
|
+
let retVal = [];
|
|
746
|
+
if (this._hasKey(key)) {
|
|
747
|
+
this._testType(key, 'set', true, callback);
|
|
748
|
+
retVal = this._getKey(key);
|
|
749
|
+
}
|
|
750
|
+
return this._handleCallback(callback, retVal);
|
|
751
|
+
}
|
|
752
|
+
/**
|
|
753
|
+
*
|
|
754
|
+
*
|
|
755
|
+
* @param {string} key
|
|
756
|
+
* @param {number} [count]
|
|
757
|
+
* @param {Function} [callback]
|
|
758
|
+
* @returns {*}
|
|
759
|
+
* @memberof MemoryCache
|
|
760
|
+
*/
|
|
761
|
+
spop(key, count, callback) {
|
|
762
|
+
let retVal = null;
|
|
763
|
+
count = count || 1;
|
|
764
|
+
if (isNaN(count)) {
|
|
765
|
+
return this._handleCallback(callback, null, messages.noint);
|
|
766
|
+
}
|
|
767
|
+
if (this._hasKey(key)) {
|
|
768
|
+
retVal = [];
|
|
769
|
+
this._testType(key, 'set', true, callback);
|
|
770
|
+
const val = this._getKey(key);
|
|
771
|
+
const length = val.length;
|
|
772
|
+
count = count > length ? length : count;
|
|
773
|
+
for (let itr = 0; itr < count; itr++) {
|
|
774
|
+
retVal.push(val.pop());
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
return this._handleCallback(callback, retVal);
|
|
778
|
+
}
|
|
779
|
+
/**
|
|
780
|
+
*
|
|
781
|
+
*
|
|
782
|
+
* @param {string} key
|
|
783
|
+
* @param {...any[]} members
|
|
784
|
+
* @returns {*}
|
|
785
|
+
* @memberof MemoryCache
|
|
786
|
+
*/
|
|
787
|
+
srem(key, ...members) {
|
|
788
|
+
let retVal = 0;
|
|
789
|
+
const callback = this._retrieveCallback(members);
|
|
790
|
+
if (this._hasKey(key)) {
|
|
791
|
+
this._testType(key, 'set', true, callback);
|
|
792
|
+
const val = this._getKey(key);
|
|
793
|
+
for (const index in members) {
|
|
794
|
+
if (members.hasOwnProperty(index)) {
|
|
795
|
+
const member = members[index];
|
|
796
|
+
const idx = val.indexOf(member);
|
|
797
|
+
if (idx !== -1) {
|
|
798
|
+
val.splice(idx, 1);
|
|
799
|
+
retVal++;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
this._setKey(key, val);
|
|
804
|
+
}
|
|
805
|
+
return this._handleCallback(callback, retVal);
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
*
|
|
809
|
+
*
|
|
810
|
+
* @param {string} sourcekey
|
|
811
|
+
* @param {string} destkey
|
|
812
|
+
* @param {string} member
|
|
813
|
+
* @param {Function} [callback]
|
|
814
|
+
* @returns {*}
|
|
815
|
+
* @memberof MemoryCache
|
|
816
|
+
*/
|
|
817
|
+
smove(sourcekey, destkey, member, callback) {
|
|
818
|
+
let retVal = 0;
|
|
819
|
+
if (this._hasKey(sourcekey)) {
|
|
820
|
+
this._testType(sourcekey, 'set', true, callback);
|
|
821
|
+
const val = this._getKey(sourcekey);
|
|
822
|
+
const idx = val.indexOf(member);
|
|
823
|
+
if (idx !== -1) {
|
|
824
|
+
this.sadd(destkey, member);
|
|
825
|
+
val.splice(idx, 1);
|
|
826
|
+
retVal = 1;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
return this._handleCallback(callback, retVal);
|
|
830
|
+
}
|
|
831
|
+
// ---------------------------------------
|
|
832
|
+
// ## Transactions (Atomic) ##
|
|
833
|
+
// ---------------------------------------
|
|
834
|
+
// TODO: Transaction Queues watch and unwatch
|
|
835
|
+
// https://redis.io/topics/transactions
|
|
836
|
+
// This can be accomplished by temporarily swapping this.cache to a temporary copy of the current statement
|
|
837
|
+
// holding and then using __.merge on actual this.cache with the temp storage.
|
|
838
|
+
discard(callback, silent) {
|
|
839
|
+
// Clear the queue mode, drain the queue, empty the watch list
|
|
840
|
+
if (this.multiMode) {
|
|
841
|
+
this.cache = this.databases[this.currentDBIndex];
|
|
842
|
+
this.tempCache = {};
|
|
843
|
+
this.multiMode = false;
|
|
844
|
+
this.responseMessages = [];
|
|
845
|
+
}
|
|
846
|
+
else if (!silent) {
|
|
847
|
+
return this._handleCallback(callback, null, messages.nomultidiscard);
|
|
848
|
+
}
|
|
849
|
+
return this._handleCallback(callback, messages.ok);
|
|
850
|
+
}
|
|
851
|
+
// ---------------------------------------
|
|
852
|
+
// ## Internal - Key ##
|
|
853
|
+
// ---------------------------------------
|
|
854
|
+
/**
|
|
855
|
+
*
|
|
856
|
+
*
|
|
857
|
+
* @param {string} key
|
|
858
|
+
* @param {Function} [callback]
|
|
859
|
+
* @returns {*}
|
|
860
|
+
* @memberof MemoryCache
|
|
861
|
+
*/
|
|
862
|
+
pttl(key, callback) {
|
|
863
|
+
let retVal = -2;
|
|
864
|
+
if (this._hasKey(key)) {
|
|
865
|
+
if (!(0, lodash_1.isNil)(this.cache[key].timeout)) {
|
|
866
|
+
retVal = this.cache[key].timeout - Date.now();
|
|
867
|
+
// Prevent unexpected errors if the actual ttl just happens to be -2 or -1
|
|
868
|
+
if (retVal < 0 && retVal > -3) {
|
|
869
|
+
retVal = -3;
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
else {
|
|
873
|
+
retVal = -1;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
return this._handleCallback(callback, retVal);
|
|
877
|
+
}
|
|
878
|
+
/**
|
|
879
|
+
*
|
|
880
|
+
*
|
|
881
|
+
* @private
|
|
882
|
+
* @param {string} key
|
|
883
|
+
* @param {Function} [callback]
|
|
884
|
+
* @returns {*}
|
|
885
|
+
* @memberof MemoryCache
|
|
886
|
+
*/
|
|
887
|
+
persist(key, callback) {
|
|
888
|
+
let retVal = 0;
|
|
889
|
+
if (this._hasKey(key)) {
|
|
890
|
+
if (!(0, lodash_1.isNil)(this._key(key).timeout)) {
|
|
891
|
+
this._key(key).timeout = null;
|
|
892
|
+
retVal = 1;
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
return this._handleCallback(callback, retVal);
|
|
896
|
+
}
|
|
897
|
+
/**
|
|
898
|
+
*
|
|
899
|
+
*
|
|
900
|
+
* @private
|
|
901
|
+
* @param {string} key
|
|
902
|
+
* @returns {*} {boolean}
|
|
903
|
+
* @memberof MemoryCache
|
|
904
|
+
*/
|
|
905
|
+
_hasKey(key) {
|
|
906
|
+
return this.cache.hasOwnProperty(key);
|
|
907
|
+
}
|
|
908
|
+
/**
|
|
909
|
+
*
|
|
910
|
+
*
|
|
911
|
+
* @private
|
|
912
|
+
* @param {*} value
|
|
913
|
+
* @param {string} type
|
|
914
|
+
* @param {number} timeout
|
|
915
|
+
* @returns {*}
|
|
916
|
+
* @memberof MemoryCache
|
|
917
|
+
*/
|
|
918
|
+
_makeKey(value, type, timeout) {
|
|
919
|
+
return { value: value, type: type, timeout: timeout || null, lastAccess: Date.now() };
|
|
920
|
+
}
|
|
921
|
+
/**
|
|
922
|
+
*
|
|
923
|
+
*
|
|
924
|
+
* @private
|
|
925
|
+
* @param {string} key
|
|
926
|
+
* @returns {*}
|
|
927
|
+
* @memberof MemoryCache
|
|
928
|
+
*/
|
|
929
|
+
_key(key) {
|
|
930
|
+
this.cache[key].lastAccess = Date.now();
|
|
931
|
+
return this.cache[key];
|
|
932
|
+
}
|
|
933
|
+
/**
|
|
934
|
+
*
|
|
935
|
+
*
|
|
936
|
+
* @private
|
|
937
|
+
* @param {string} key
|
|
938
|
+
* @param {number} amount
|
|
939
|
+
* @param {Function} [callback]
|
|
940
|
+
* @returns {*}
|
|
941
|
+
* @memberof MemoryCache
|
|
942
|
+
*/
|
|
943
|
+
_addToKey(key, amount, callback) {
|
|
944
|
+
let keyValue = 0;
|
|
945
|
+
if (isNaN(amount) || (0, lodash_1.isNil)(amount)) {
|
|
946
|
+
return this._handleCallback(callback, null, messages.noint);
|
|
947
|
+
}
|
|
948
|
+
if (this._hasKey(key)) {
|
|
949
|
+
this._testType(key, 'string', true, callback);
|
|
950
|
+
keyValue = parseInt(this._getKey(key));
|
|
951
|
+
if (isNaN(keyValue) || (0, lodash_1.isNil)(keyValue)) {
|
|
952
|
+
return this._handleCallback(callback, null, messages.noint);
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
else {
|
|
956
|
+
this.cache[key] = this._makeKey('0', 'string');
|
|
957
|
+
}
|
|
958
|
+
const val = keyValue + amount;
|
|
959
|
+
this._setKey(key, val.toString());
|
|
960
|
+
return val;
|
|
961
|
+
}
|
|
962
|
+
/**
|
|
963
|
+
*
|
|
964
|
+
*
|
|
965
|
+
* @private
|
|
966
|
+
* @param {string} key
|
|
967
|
+
* @param {string} type
|
|
968
|
+
* @param {boolean} [throwError]
|
|
969
|
+
* @param {Function} [callback]
|
|
970
|
+
* @returns {*}
|
|
971
|
+
* @memberof MemoryCache
|
|
972
|
+
*/
|
|
973
|
+
_testType(key, type, throwError, callback) {
|
|
974
|
+
throwError = !!throwError;
|
|
975
|
+
const keyType = this._key(key).type;
|
|
976
|
+
if (keyType !== type) {
|
|
977
|
+
if (throwError) {
|
|
978
|
+
return this._handleCallback(callback, null, messages.wrongTypeOp);
|
|
979
|
+
}
|
|
980
|
+
return false;
|
|
981
|
+
}
|
|
982
|
+
return true;
|
|
983
|
+
}
|
|
984
|
+
/**
|
|
985
|
+
*
|
|
986
|
+
*
|
|
987
|
+
* @private
|
|
988
|
+
* @param {string} key
|
|
989
|
+
* @returns {*}
|
|
990
|
+
* @memberof MemoryCache
|
|
991
|
+
*/
|
|
992
|
+
_getKey(key) {
|
|
993
|
+
const _key = this._key(key) || {};
|
|
994
|
+
if (_key.timeout && _key.timeout <= Date.now()) {
|
|
995
|
+
this.del(key);
|
|
996
|
+
return null;
|
|
997
|
+
}
|
|
998
|
+
return _key.value;
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
1001
|
+
*
|
|
1002
|
+
*
|
|
1003
|
+
* @private
|
|
1004
|
+
* @param {string} key
|
|
1005
|
+
* @param {(number | string)} value
|
|
1006
|
+
* @memberof MemoryCache
|
|
1007
|
+
*/
|
|
1008
|
+
_setKey(key, value) {
|
|
1009
|
+
this.cache[key].value = value;
|
|
1010
|
+
this.cache[key].lastAccess = Date.now();
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
*
|
|
1014
|
+
*
|
|
1015
|
+
* @private
|
|
1016
|
+
* @param {string} key
|
|
1017
|
+
* @param {string} field
|
|
1018
|
+
* @param {number} [amount]
|
|
1019
|
+
* @param {boolean} [useFloat]
|
|
1020
|
+
* @param {Function} [callback]
|
|
1021
|
+
* @returns {*}
|
|
1022
|
+
* @memberof MemoryCache
|
|
1023
|
+
*/
|
|
1024
|
+
_addToField(key, field, amount, useFloat, callback) {
|
|
1025
|
+
useFloat = useFloat || false;
|
|
1026
|
+
let fieldValue = useFloat ? 0.0 : 0;
|
|
1027
|
+
let value = 0;
|
|
1028
|
+
if (isNaN(amount) || (0, lodash_1.isNil)(amount)) {
|
|
1029
|
+
return this._handleCallback(callback, null, useFloat ? messages.nofloat : messages.noint);
|
|
1030
|
+
}
|
|
1031
|
+
if (this._hasKey(key)) {
|
|
1032
|
+
this._testType(key, 'hash', true, callback);
|
|
1033
|
+
if (this._hasField(key, field)) {
|
|
1034
|
+
value = this._getField(key, field);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
else {
|
|
1038
|
+
this.cache[key] = this._makeKey({}, 'hash');
|
|
1039
|
+
}
|
|
1040
|
+
fieldValue = useFloat ? parseFloat(`${value}`) : parseInt(`${value}`);
|
|
1041
|
+
amount = useFloat ? parseFloat(`${amount}`) : parseInt(`${amount}`);
|
|
1042
|
+
if (isNaN(fieldValue) || (0, lodash_1.isNil)(fieldValue)) {
|
|
1043
|
+
return this._handleCallback(callback, null, useFloat ? messages.nofloat : messages.noint);
|
|
1044
|
+
}
|
|
1045
|
+
fieldValue += amount;
|
|
1046
|
+
this._setField(key, field, fieldValue.toString());
|
|
1047
|
+
return fieldValue;
|
|
1048
|
+
}
|
|
1049
|
+
/**
|
|
1050
|
+
*
|
|
1051
|
+
*
|
|
1052
|
+
* @private
|
|
1053
|
+
* @param {string} key
|
|
1054
|
+
* @param {string} field
|
|
1055
|
+
* @returns {*}
|
|
1056
|
+
* @memberof MemoryCache
|
|
1057
|
+
*/
|
|
1058
|
+
_getField(key, field) {
|
|
1059
|
+
return this._getKey(key)[field];
|
|
1060
|
+
}
|
|
1061
|
+
/**
|
|
1062
|
+
*
|
|
1063
|
+
*
|
|
1064
|
+
* @private
|
|
1065
|
+
* @param {string} key
|
|
1066
|
+
* @param {string} field
|
|
1067
|
+
* @returns {*} {boolean}
|
|
1068
|
+
* @memberof MemoryCache
|
|
1069
|
+
*/
|
|
1070
|
+
_hasField(key, field) {
|
|
1071
|
+
let retVal = false;
|
|
1072
|
+
if (key && field) {
|
|
1073
|
+
const ky = this._getKey(key);
|
|
1074
|
+
if (ky) {
|
|
1075
|
+
retVal = ky.hasOwnProperty(field);
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
return retVal;
|
|
1079
|
+
}
|
|
1080
|
+
/**
|
|
1081
|
+
*
|
|
1082
|
+
*
|
|
1083
|
+
* @param {string} key
|
|
1084
|
+
* @param {string} field
|
|
1085
|
+
* @param {*} value
|
|
1086
|
+
* @memberof MemoryCache
|
|
1087
|
+
*/
|
|
1088
|
+
_setField(key, field, value) {
|
|
1089
|
+
this._getKey(key)[field] = value;
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
*
|
|
1093
|
+
*
|
|
1094
|
+
* @private
|
|
1095
|
+
* @param {Function} [callback]
|
|
1096
|
+
* @param {(any)} [message]
|
|
1097
|
+
* @param {*} [error]
|
|
1098
|
+
* @param {boolean} [nolog]
|
|
1099
|
+
* @returns {*}
|
|
1100
|
+
* @memberof MemoryCache
|
|
1101
|
+
*/
|
|
1102
|
+
_handleCallback(callback, message, error, nolog) {
|
|
1103
|
+
let err = error;
|
|
1104
|
+
let msg = message;
|
|
1105
|
+
nolog = (0, lodash_1.isNil)(nolog) ? true : nolog;
|
|
1106
|
+
if (nolog) {
|
|
1107
|
+
err = this._logReturn(error);
|
|
1108
|
+
msg = this._logReturn(message);
|
|
1109
|
+
}
|
|
1110
|
+
if (typeof callback === 'function') {
|
|
1111
|
+
callback(err, msg);
|
|
1112
|
+
return;
|
|
1113
|
+
}
|
|
1114
|
+
if (err) {
|
|
1115
|
+
throw new Error(err);
|
|
1116
|
+
}
|
|
1117
|
+
return msg;
|
|
1118
|
+
}
|
|
1119
|
+
_logReturn(message) {
|
|
1120
|
+
if (!(0, lodash_1.isUndefined)(message)) {
|
|
1121
|
+
if (this.multiMode) {
|
|
1122
|
+
if (!(0, lodash_1.isNil)(this.responseMessages)) {
|
|
1123
|
+
this.responseMessages.push(message);
|
|
1124
|
+
if (message === messages.ok) {
|
|
1125
|
+
message = messages.queued;
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
return message;
|
|
1130
|
+
}
|
|
1131
|
+
return;
|
|
1132
|
+
}
|
|
1133
|
+
/**
|
|
1134
|
+
*
|
|
1135
|
+
*
|
|
1136
|
+
* @private
|
|
1137
|
+
* @param {any[]} [params]
|
|
1138
|
+
* @returns {*}
|
|
1139
|
+
* @memberof MemoryCache
|
|
1140
|
+
*/
|
|
1141
|
+
_retrieveCallback(params) {
|
|
1142
|
+
if (Array.isArray(params) && params.length > 0 && typeof params[params.length - 1] === 'function') {
|
|
1143
|
+
return params.pop();
|
|
1144
|
+
}
|
|
1145
|
+
return;
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
exports.MemoryCache = MemoryCache;
|
|
1149
|
+
//# sourceMappingURL=memory_cache.js.map
|