cache-manager-memory-store-with-ttl 1.0.13 → 1.0.14
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/dist/memory-store.js +259 -0
- package/dist/utils.js +42 -0
- package/package.json +2 -2
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/* eslint no-unused-vars:0 */
|
|
2
|
+
const Lru = require('lru-cache');
|
|
3
|
+
const cloneDeep = require('lodash.clonedeep');
|
|
4
|
+
const utils = require('./utils');
|
|
5
|
+
|
|
6
|
+
const { isObject } = utils;
|
|
7
|
+
|
|
8
|
+
function clone(object) {
|
|
9
|
+
if (typeof object === 'object' && object !== null) {
|
|
10
|
+
return cloneDeep(object);
|
|
11
|
+
}
|
|
12
|
+
return object;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Wrapper for lru-cache.
|
|
17
|
+
* NOTE: If you want to use a memory store, you want to write your own to have full
|
|
18
|
+
* control over its behavior. E.g., you might need the extra Promise overhead or
|
|
19
|
+
* you may want to clone objects a certain way before storing.
|
|
20
|
+
*
|
|
21
|
+
* @param {object} args - Args passed to underlying lru-cache, plus additional optional args:
|
|
22
|
+
* @param {boolean} [args.shouldCloneBeforeSet=true] - Whether to clone the data being stored.
|
|
23
|
+
* Default: true
|
|
24
|
+
* @param {boolean} [args.usePromises=true] - Whether to enable the use of Promises. Default: true
|
|
25
|
+
*/
|
|
26
|
+
const memoryStore = function (args) {
|
|
27
|
+
args = args || {};
|
|
28
|
+
const self = {};
|
|
29
|
+
self.name = 'memory';
|
|
30
|
+
const Promise = args.promiseDependency || global.Promise;
|
|
31
|
+
self.usePromises = !(typeof Promise === 'undefined' || args.noPromises);
|
|
32
|
+
self.shouldCloneBeforeSet = args.shouldCloneBeforeSet !== false; // clone by default
|
|
33
|
+
|
|
34
|
+
const lruOpts = {
|
|
35
|
+
max: args.max || 500,
|
|
36
|
+
maxSize: args.maxSize,
|
|
37
|
+
ttl: args.ttl || args.ttl === 0 ? args.ttl * 1000 : undefined,
|
|
38
|
+
dispose: args.dispose,
|
|
39
|
+
sizeCalculation: args.sizeCalculation || args.length,
|
|
40
|
+
allowStale: args.allowStale || args.stale,
|
|
41
|
+
updateAgeOnGet: args.updateAgeOnGet,
|
|
42
|
+
updateAgeOnHas: args.updateAgeOnHas,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const lruCache = new Lru(lruOpts);
|
|
46
|
+
|
|
47
|
+
const setMultipleKeys = function setMultipleKeys(keysValues, ttl) {
|
|
48
|
+
const { length } = keysValues;
|
|
49
|
+
const values = [];
|
|
50
|
+
for (let i = 0; i < length; i += 2) {
|
|
51
|
+
lruCache.set(keysValues[i], keysValues[i + 1], { ttl });
|
|
52
|
+
values.push(keysValues[i + 1]);
|
|
53
|
+
}
|
|
54
|
+
return values;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
self.set = function (key, value, options, cb) {
|
|
58
|
+
if (self.shouldCloneBeforeSet) {
|
|
59
|
+
value = clone(value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (typeof options === 'function') {
|
|
63
|
+
cb = options;
|
|
64
|
+
options = {};
|
|
65
|
+
}
|
|
66
|
+
options = options || {};
|
|
67
|
+
|
|
68
|
+
if (options && typeof options === 'number') {
|
|
69
|
+
options = { ttl: options };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const ttl =
|
|
73
|
+
options.ttl || options.ttl === 0 ? options.ttl * 1000 : lruOpts.ttl;
|
|
74
|
+
|
|
75
|
+
lruCache.set(key, value, { ttl });
|
|
76
|
+
if (cb) {
|
|
77
|
+
process.nextTick(cb.bind(null, null));
|
|
78
|
+
} else if (self.usePromises) {
|
|
79
|
+
return Promise.resolve(value);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
self.mset = function () {
|
|
84
|
+
const args = Array.prototype.slice.apply(arguments);
|
|
85
|
+
let cb;
|
|
86
|
+
let options = {};
|
|
87
|
+
|
|
88
|
+
if (typeof args[args.length - 1] === 'function') {
|
|
89
|
+
cb = args.pop();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (args.length % 2 > 0 && isObject(args[args.length - 1])) {
|
|
93
|
+
options = args.pop();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const ttl =
|
|
97
|
+
options.ttl || options.ttl === 0 ? options.ttl * 1000 : lruOpts.ttl;
|
|
98
|
+
|
|
99
|
+
const values = setMultipleKeys(args, ttl);
|
|
100
|
+
|
|
101
|
+
if (cb) {
|
|
102
|
+
process.nextTick(cb.bind(null, null));
|
|
103
|
+
} else if (self.usePromises) {
|
|
104
|
+
return Promise.resolve(values);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
self.get = function (key, options, cb) {
|
|
109
|
+
if (typeof options === 'function') {
|
|
110
|
+
cb = options;
|
|
111
|
+
}
|
|
112
|
+
const value = lruCache.get(key);
|
|
113
|
+
|
|
114
|
+
if (cb) {
|
|
115
|
+
process.nextTick(cb.bind(null, null, value));
|
|
116
|
+
} else if (self.usePromises) {
|
|
117
|
+
return Promise.resolve(value);
|
|
118
|
+
} else {
|
|
119
|
+
return value;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
self.mget = function () {
|
|
124
|
+
const args = Array.prototype.slice.apply(arguments);
|
|
125
|
+
let cb;
|
|
126
|
+
let options = {};
|
|
127
|
+
|
|
128
|
+
if (typeof args[args.length - 1] === 'function') {
|
|
129
|
+
cb = args.pop();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (isObject(args[args.length - 1])) {
|
|
133
|
+
options = args.pop();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const values = args.map(function (key) {
|
|
137
|
+
return lruCache.get(key);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
if (cb) {
|
|
141
|
+
process.nextTick(cb.bind(null, null, values));
|
|
142
|
+
} else if (self.usePromises) {
|
|
143
|
+
return Promise.resolve(values);
|
|
144
|
+
} else {
|
|
145
|
+
return values;
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
self.ttl = function (key, cb) {
|
|
150
|
+
const value = lruCache.getRemainingTTL(key);
|
|
151
|
+
|
|
152
|
+
if (cb) {
|
|
153
|
+
process.nextTick(cb.bind(null, null, value));
|
|
154
|
+
} else if (self.usePromises) {
|
|
155
|
+
return Promise.resolve(value);
|
|
156
|
+
} else {
|
|
157
|
+
return value;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
self.del = function () {
|
|
162
|
+
let args = Array.prototype.slice.apply(arguments);
|
|
163
|
+
let cb;
|
|
164
|
+
let options = {};
|
|
165
|
+
|
|
166
|
+
if (typeof args[args.length - 1] === 'function') {
|
|
167
|
+
cb = args.pop();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (isObject(args[args.length - 1])) {
|
|
171
|
+
options = args.pop();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (Array.isArray(args[0])) {
|
|
175
|
+
args = args[0];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
args.forEach(function (key) {
|
|
179
|
+
lruCache.delete(key);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
if (cb) {
|
|
183
|
+
process.nextTick(cb.bind(null, null));
|
|
184
|
+
} else if (self.usePromises) {
|
|
185
|
+
return Promise.resolve();
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
self.reset = function (cb) {
|
|
190
|
+
lruCache.clear();
|
|
191
|
+
if (cb) {
|
|
192
|
+
process.nextTick(cb.bind(null, null));
|
|
193
|
+
} else if (self.usePromises) {
|
|
194
|
+
return Promise.resolve();
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
self.keys = function (cb) {
|
|
199
|
+
const keys = [...lruCache.keys()];
|
|
200
|
+
if (cb) {
|
|
201
|
+
process.nextTick(cb.bind(null, null, keys));
|
|
202
|
+
} else if (self.usePromises) {
|
|
203
|
+
return Promise.resolve(keys);
|
|
204
|
+
} else {
|
|
205
|
+
return keys;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* This method is not available in the caching modules.
|
|
211
|
+
*/
|
|
212
|
+
self.keyCount = function () {
|
|
213
|
+
return lruCache.size;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* This method is not available in the caching modules.
|
|
218
|
+
*/
|
|
219
|
+
self.dump = function () {
|
|
220
|
+
const args = Array.prototype.slice.apply(arguments);
|
|
221
|
+
let cb;
|
|
222
|
+
|
|
223
|
+
if (typeof args[args.length - 1] === 'function') {
|
|
224
|
+
cb = args.pop();
|
|
225
|
+
}
|
|
226
|
+
const value = lruCache.dump();
|
|
227
|
+
|
|
228
|
+
if (cb) {
|
|
229
|
+
process.nextTick(cb.bind(null, null, value));
|
|
230
|
+
} else if (self.usePromises) {
|
|
231
|
+
return Promise.resolve(value);
|
|
232
|
+
} else {
|
|
233
|
+
return value;
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* This method is not available in the caching modules.
|
|
239
|
+
*/
|
|
240
|
+
self.load = function (cacheEntries, cb) {
|
|
241
|
+
lruCache.load(cacheEntries);
|
|
242
|
+
|
|
243
|
+
if (typeof cb === 'function') {
|
|
244
|
+
process.nextTick(cb.bind(null, null));
|
|
245
|
+
} else if (self.usePromises) {
|
|
246
|
+
return Promise.resolve();
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
return self;
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const methods = {
|
|
254
|
+
create(args) {
|
|
255
|
+
return memoryStore(args);
|
|
256
|
+
},
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
module.exports = methods;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
var isObject = function isObject(value) {
|
|
2
|
+
return value instanceof Object && value.constructor === Object;
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
var parseWrapArguments = function parseWrapArguments(args) {
|
|
6
|
+
var length = args.length;
|
|
7
|
+
var work;
|
|
8
|
+
var options = {};
|
|
9
|
+
var cb;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* As we can receive an unlimited number of keys
|
|
13
|
+
* we find the index of the first function which is
|
|
14
|
+
* the "work" handler to fetch the keys.
|
|
15
|
+
*/
|
|
16
|
+
for (var i = 0; i < length; i += 1) {
|
|
17
|
+
if (typeof args[i] === 'function') {
|
|
18
|
+
if (typeof args[i + 2] === 'function') {
|
|
19
|
+
cb = args.pop();
|
|
20
|
+
} else if (typeof args[i + 1] === 'function') {
|
|
21
|
+
cb = args.pop();
|
|
22
|
+
}
|
|
23
|
+
if (isObject(args[i + 1])) {
|
|
24
|
+
options = args.pop();
|
|
25
|
+
}
|
|
26
|
+
work = args.pop();
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
keys: args,
|
|
33
|
+
work: work,
|
|
34
|
+
options: options,
|
|
35
|
+
cb: cb
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
module.exports = {
|
|
40
|
+
isObject: isObject,
|
|
41
|
+
parseWrapArguments: parseWrapArguments
|
|
42
|
+
};
|