cache-manager 4.1.0 → 5.0.1
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/README.md +25 -450
- package/dist/caching.d.ts +33 -0
- package/dist/caching.js +103 -0
- package/dist/caching.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/multi-caching.d.ts +6 -0
- package/dist/multi-caching.js +155 -0
- package/dist/multi-caching.js.map +1 -0
- package/dist/stores/index.d.ts +1 -0
- package/dist/stores/index.js +18 -0
- package/dist/stores/index.js.map +1 -0
- package/dist/stores/memory.d.ts +19 -0
- package/dist/stores/memory.js +232 -0
- package/dist/stores/memory.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/package.json +50 -21
- package/History.md +0 -262
- package/examples/example.js +0 -133
- package/examples/redis_example/example.js +0 -147
- package/examples/redis_example/redis_store.js +0 -124
- package/index.js +0 -1
- package/lib/caching.js +0 -384
- package/lib/callback_filler.js +0 -28
- package/lib/index.js +0 -7
- package/lib/multi_caching.js +0 -657
- package/lib/stores/memory.js +0 -244
- package/lib/stores/none.js +0 -143
- package/lib/utils.js +0 -42
package/lib/multi_caching.js
DELETED
|
@@ -1,657 +0,0 @@
|
|
|
1
|
-
/** @module cacheManager/multiCaching */
|
|
2
|
-
var async = require('async');
|
|
3
|
-
var CallbackFiller = require('./callback_filler');
|
|
4
|
-
var utils = require('./utils');
|
|
5
|
-
var isObject = utils.isObject;
|
|
6
|
-
var parseWrapArguments = utils.parseWrapArguments;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Module that lets you specify a hierarchy of caches.
|
|
10
|
-
*
|
|
11
|
-
* @param {array} caches - Array of caching objects.
|
|
12
|
-
* @param {object} [options]
|
|
13
|
-
* @param {function} [options.isCacheableValue] - A callback function which is called
|
|
14
|
-
* with every value returned from cache or from a wrapped function. This lets you specify
|
|
15
|
-
* which values should and should not be cached. If the function returns true, it will be
|
|
16
|
-
* stored in cache. By default it caches everything except undefined.
|
|
17
|
-
*
|
|
18
|
-
* If an underlying cache specifies its own isCacheableValue function, that function will
|
|
19
|
-
* be used instead of the multiCaching's _isCacheableValue function.
|
|
20
|
-
*/
|
|
21
|
-
var multiCaching = function(caches, options) {
|
|
22
|
-
var self = {};
|
|
23
|
-
options = options || {};
|
|
24
|
-
|
|
25
|
-
var Promise = options.promiseDependency || global.Promise;
|
|
26
|
-
|
|
27
|
-
if (!Array.isArray(caches)) {
|
|
28
|
-
throw new Error('multiCaching requires an array of caches');
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
var callbackFiller = new CallbackFiller();
|
|
32
|
-
var backgroundQueue = new Set();
|
|
33
|
-
|
|
34
|
-
if (typeof options.isCacheableValue === 'function') {
|
|
35
|
-
self._isCacheableValue = options.isCacheableValue;
|
|
36
|
-
} else {
|
|
37
|
-
self._isCacheableValue = function(value) {
|
|
38
|
-
return value !== undefined;
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* If the underlying cache specifies its own isCacheableValue function (such
|
|
44
|
-
* as how node-cache-manager-redis does), use that function, otherwise use
|
|
45
|
-
* self._isCacheableValue function.
|
|
46
|
-
*/
|
|
47
|
-
function getIsCacheableValueFunction(cache) {
|
|
48
|
-
if (cache.store && typeof cache.store.isCacheableValue === 'function') {
|
|
49
|
-
return cache.store.isCacheableValue.bind(cache.store);
|
|
50
|
-
} else {
|
|
51
|
-
return self._isCacheableValue;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function getFromHighestPriorityCachePromise() {
|
|
56
|
-
var args = Array.prototype.slice.apply(arguments).filter(function(v) {
|
|
57
|
-
return typeof v !== 'undefined';
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
return new Promise(function(resolve, reject) {
|
|
61
|
-
var cb = function(err, result) {
|
|
62
|
-
if (err) {
|
|
63
|
-
return reject(err);
|
|
64
|
-
}
|
|
65
|
-
resolve(result);
|
|
66
|
-
};
|
|
67
|
-
args.push(cb);
|
|
68
|
-
getFromHighestPriorityCache.apply(null, args);
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function getFromHighestPriorityCache() {
|
|
73
|
-
var args = Array.prototype.slice.apply(arguments).filter(function(v) {
|
|
74
|
-
return typeof v !== 'undefined';
|
|
75
|
-
});
|
|
76
|
-
var cb;
|
|
77
|
-
var options = {};
|
|
78
|
-
|
|
79
|
-
if (typeof args[args.length - 1] === 'function') {
|
|
80
|
-
cb = args.pop();
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (!cb) {
|
|
84
|
-
return getFromHighestPriorityCachePromise.apply(this, args);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (isObject(args[args.length - 1])) {
|
|
88
|
-
options = args.pop();
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Keep a copy of the keys to retrieve
|
|
93
|
-
*/
|
|
94
|
-
var keys = Array.prototype.slice.apply(args);
|
|
95
|
-
var multi = keys.length > 1;
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Then put back the options in the args Array
|
|
99
|
-
*/
|
|
100
|
-
args.push(options);
|
|
101
|
-
|
|
102
|
-
if (multi) {
|
|
103
|
-
/**
|
|
104
|
-
* Keep track of the keys left to fetch accross the caches
|
|
105
|
-
*/
|
|
106
|
-
var keysToFetch = Array.prototype.slice.apply(keys);
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Hash to save our multi keys result
|
|
110
|
-
*/
|
|
111
|
-
var mapResult = {};
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
var i = 0;
|
|
115
|
-
async.eachSeries(caches, function(cache, next) {
|
|
116
|
-
var callback = function(err, result) {
|
|
117
|
-
if (err) {
|
|
118
|
-
return next(err);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
var _isCacheableValue = getIsCacheableValueFunction(cache);
|
|
122
|
-
|
|
123
|
-
if (multi) {
|
|
124
|
-
addResultToMap(result, _isCacheableValue);
|
|
125
|
-
|
|
126
|
-
if (keysToFetch.length === 0 || i === caches.length - 1) {
|
|
127
|
-
// Return an Array with the values merged from all the caches
|
|
128
|
-
return cb(null, keys.map(function(k) {
|
|
129
|
-
return mapResult[k] || undefined;
|
|
130
|
-
}), i);
|
|
131
|
-
}
|
|
132
|
-
} else if (_isCacheableValue(result)) {
|
|
133
|
-
// break out of async loop.
|
|
134
|
-
return cb(err, result, i);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
i += 1;
|
|
138
|
-
next();
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
if (multi) {
|
|
142
|
-
if (typeof cache.store.mget !== 'function') {
|
|
143
|
-
/**
|
|
144
|
-
* Silently fail for store that don't support mget()
|
|
145
|
-
*/
|
|
146
|
-
return callback(null, []);
|
|
147
|
-
}
|
|
148
|
-
var _args = Array.prototype.slice.apply(keysToFetch);
|
|
149
|
-
_args.push(options);
|
|
150
|
-
_args.push(callback);
|
|
151
|
-
cache.store.mget.apply(cache.store, _args);
|
|
152
|
-
} else {
|
|
153
|
-
try {
|
|
154
|
-
cache.store.get(args[0], options, callback);
|
|
155
|
-
} catch (err) {
|
|
156
|
-
callback(err);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}, function(err, result) {
|
|
160
|
-
return cb(err, result);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
function addResultToMap(result, isCacheable) {
|
|
164
|
-
var key;
|
|
165
|
-
var diff = 0;
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* We loop through the result and if the value
|
|
169
|
-
* is cacheable we add it to the mapResult hash
|
|
170
|
-
* and remove the key to fetch from the "keysToFetch" array
|
|
171
|
-
*/
|
|
172
|
-
result.forEach(function(res, i) {
|
|
173
|
-
if (isCacheable(res)) {
|
|
174
|
-
key = keysToFetch[i - diff];
|
|
175
|
-
|
|
176
|
-
// Add the result to our map
|
|
177
|
-
mapResult[key] = res;
|
|
178
|
-
|
|
179
|
-
// delete key from our keysToFetch array
|
|
180
|
-
keysToFetch.splice(i - diff, 1);
|
|
181
|
-
diff += 1;
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function setInMultipleCachesPromise() {
|
|
188
|
-
var args = Array.prototype.slice.apply(arguments);
|
|
189
|
-
|
|
190
|
-
return new Promise(function(resolve, reject) {
|
|
191
|
-
var cb = function(err, result) {
|
|
192
|
-
if (err) {
|
|
193
|
-
return reject(err);
|
|
194
|
-
}
|
|
195
|
-
resolve(result);
|
|
196
|
-
};
|
|
197
|
-
args.push(cb);
|
|
198
|
-
setInMultipleCaches.apply(null, args);
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
function setInMultipleCaches() {
|
|
203
|
-
var args = Array.prototype.slice.apply(arguments);
|
|
204
|
-
var _caches = Array.isArray(args[0]) ? args.shift() : caches;
|
|
205
|
-
|
|
206
|
-
var cb;
|
|
207
|
-
var options = {};
|
|
208
|
-
|
|
209
|
-
if (typeof args[args.length - 1] === 'function') {
|
|
210
|
-
cb = args.pop();
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
if (!cb) {
|
|
214
|
-
return setInMultipleCachesPromise.apply(this, args);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (args.length % 2 > 0 && isObject(args[args.length - 1])) {
|
|
218
|
-
options = args.pop();
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
var length = args.length;
|
|
222
|
-
var multi = length > 2;
|
|
223
|
-
var i;
|
|
224
|
-
|
|
225
|
-
async.each(_caches, function(cache, next) {
|
|
226
|
-
var _isCacheableValue = getIsCacheableValueFunction(cache);
|
|
227
|
-
var keysValues = Array.prototype.slice.apply(args);
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* We filter out the keys *not* cacheable
|
|
231
|
-
*/
|
|
232
|
-
for (i = 0; i < length; i += 2) {
|
|
233
|
-
if (!_isCacheableValue(keysValues[i + 1])) {
|
|
234
|
-
keysValues.splice(i, 2);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
if (keysValues.length === 0) {
|
|
239
|
-
return next();
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
var cacheOptions = options;
|
|
243
|
-
if (typeof options.ttl === 'function') {
|
|
244
|
-
/**
|
|
245
|
-
* Dynamically set the ttl by context depending of the store
|
|
246
|
-
*/
|
|
247
|
-
cacheOptions = {};
|
|
248
|
-
cacheOptions.ttl = options.ttl(keysValues, cache.store.name);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
if (multi) {
|
|
252
|
-
if (typeof cache.store.mset !== 'function') {
|
|
253
|
-
/**
|
|
254
|
-
* Silently fail for store that don't support mset()
|
|
255
|
-
*/
|
|
256
|
-
return next();
|
|
257
|
-
}
|
|
258
|
-
keysValues.push(cacheOptions);
|
|
259
|
-
keysValues.push(next);
|
|
260
|
-
|
|
261
|
-
cache.store.mset.apply(cache.store, keysValues);
|
|
262
|
-
} else {
|
|
263
|
-
cache.store.set(keysValues[0], keysValues[1], cacheOptions, next);
|
|
264
|
-
}
|
|
265
|
-
}, function(err, result) {
|
|
266
|
-
cb(err, result);
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
function getAndPassUpPromise(key) {
|
|
271
|
-
return new Promise(function(resolve, reject) {
|
|
272
|
-
self.getAndPassUp(key, function(err, result) {
|
|
273
|
-
if (err) {
|
|
274
|
-
return reject(err);
|
|
275
|
-
}
|
|
276
|
-
resolve(result);
|
|
277
|
-
});
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Looks for an item in cache tiers.
|
|
283
|
-
* When a key is found in a lower cache, all higher levels are updated.
|
|
284
|
-
*
|
|
285
|
-
* @param {string} key
|
|
286
|
-
* @param {function} cb
|
|
287
|
-
*/
|
|
288
|
-
self.getAndPassUp = function(key, cb) {
|
|
289
|
-
if (!cb) {
|
|
290
|
-
return getAndPassUpPromise(key);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
getFromHighestPriorityCache(key, function(err, result, index) {
|
|
294
|
-
if (err) {
|
|
295
|
-
return cb(err);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
if (index) {
|
|
299
|
-
var cachesToUpdate = caches.slice(0, index);
|
|
300
|
-
async.each(cachesToUpdate, function(cache, next) {
|
|
301
|
-
var _isCacheableValue = getIsCacheableValueFunction(cache);
|
|
302
|
-
if (_isCacheableValue(result)) {
|
|
303
|
-
// We rely on the cache module's default TTL
|
|
304
|
-
cache.set(key, result, next);
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
return cb(err, result);
|
|
310
|
-
});
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
function wrapPromise(key, promise, options) {
|
|
314
|
-
return new Promise(function(resolve, reject) {
|
|
315
|
-
self.wrap(key, function(cb) {
|
|
316
|
-
Promise.resolve()
|
|
317
|
-
.then(promise)
|
|
318
|
-
.then(function(result) {
|
|
319
|
-
cb(null, result);
|
|
320
|
-
})
|
|
321
|
-
.catch(cb);
|
|
322
|
-
}, options, function(err, result) {
|
|
323
|
-
if (err) {
|
|
324
|
-
return reject(err);
|
|
325
|
-
}
|
|
326
|
-
resolve(result);
|
|
327
|
-
});
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
function handleBackgroundRefresh(caches, index, key, work, options) {
|
|
332
|
-
if (caches[index].refreshThreshold && !backgroundQueue.has(key)) {
|
|
333
|
-
backgroundQueue.add(key);
|
|
334
|
-
caches[index].checkRefreshThreshold(key, function(err, isExpiring) {
|
|
335
|
-
if (err) {
|
|
336
|
-
backgroundQueue.delete(key);
|
|
337
|
-
return;
|
|
338
|
-
}
|
|
339
|
-
if (isExpiring) {
|
|
340
|
-
work(function(workErr, workData) {
|
|
341
|
-
if (workErr || !self._isCacheableValue(workData)) {
|
|
342
|
-
backgroundQueue.delete(key);
|
|
343
|
-
return;
|
|
344
|
-
}
|
|
345
|
-
var args = [caches, key, workData, options, function() {
|
|
346
|
-
backgroundQueue.delete(key);
|
|
347
|
-
}];
|
|
348
|
-
setInMultipleCaches.apply(null, args);
|
|
349
|
-
});
|
|
350
|
-
} else {
|
|
351
|
-
backgroundQueue.delete(key);
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
/**
|
|
358
|
-
* Wraps a function in one or more caches.
|
|
359
|
-
* Has same API as regular caching module.
|
|
360
|
-
*
|
|
361
|
-
* If a key doesn't exist in any cache, it gets set in all caches.
|
|
362
|
-
* If a key exists in a high-priority (e.g., first) cache, it gets returned immediately
|
|
363
|
-
* without getting set in other lower-priority caches.
|
|
364
|
-
* If a key doesn't exist in a higher-priority cache but exists in a lower-priority
|
|
365
|
-
* cache, it gets set in all higher-priority caches.
|
|
366
|
-
* You can pass any number of keys as long as the wrapped function returns
|
|
367
|
-
* an array with the same number of values and in the same order.
|
|
368
|
-
*
|
|
369
|
-
* @function
|
|
370
|
-
* @name wrap
|
|
371
|
-
*
|
|
372
|
-
* @param {string} key - The cache key to use in cache operations. Can be one or many.
|
|
373
|
-
* @param {function} work - The function to wrap
|
|
374
|
-
* @param {object} [options] - options passed to `set` function
|
|
375
|
-
* @param {function} cb
|
|
376
|
-
*/
|
|
377
|
-
self.wrap = function() {
|
|
378
|
-
var parsedArgs = parseWrapArguments(Array.prototype.slice.apply(arguments));
|
|
379
|
-
var keys = parsedArgs.keys;
|
|
380
|
-
var work = parsedArgs.work;
|
|
381
|
-
var options = parsedArgs.options;
|
|
382
|
-
var cb = parsedArgs.cb;
|
|
383
|
-
|
|
384
|
-
if (!cb) {
|
|
385
|
-
keys.push(work);
|
|
386
|
-
keys.push(options);
|
|
387
|
-
return wrapPromise.apply(this, keys);
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
if (keys.length > 1) {
|
|
391
|
-
/**
|
|
392
|
-
* Handle more than 1 key
|
|
393
|
-
*/
|
|
394
|
-
return wrapMultiple(keys, work, options, cb);
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
var key = keys[0];
|
|
398
|
-
|
|
399
|
-
var hasKey = callbackFiller.has(key);
|
|
400
|
-
callbackFiller.add(key, {cb: cb});
|
|
401
|
-
if (hasKey) { return; }
|
|
402
|
-
|
|
403
|
-
getFromHighestPriorityCache(key, function(err, result, index) {
|
|
404
|
-
if (err) {
|
|
405
|
-
return callbackFiller.fill(key, err);
|
|
406
|
-
} else if (self._isCacheableValue(result)) {
|
|
407
|
-
handleBackgroundRefresh(caches, index, key, work, options);
|
|
408
|
-
var cachesToUpdate = caches.slice(0, index);
|
|
409
|
-
var args = [cachesToUpdate, key, result, options, function(err) {
|
|
410
|
-
callbackFiller.fill(key, err, result);
|
|
411
|
-
}];
|
|
412
|
-
|
|
413
|
-
setInMultipleCaches.apply(null, args);
|
|
414
|
-
} else {
|
|
415
|
-
work(function(err, data) {
|
|
416
|
-
if (err) {
|
|
417
|
-
return callbackFiller.fill(key, err);
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
if (!self._isCacheableValue(data)) {
|
|
421
|
-
return callbackFiller.fill(key, err, data);
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
var args = [caches, key, data, options, function(err) {
|
|
425
|
-
callbackFiller.fill(key, err, data);
|
|
426
|
-
}];
|
|
427
|
-
|
|
428
|
-
setInMultipleCaches.apply(null, args);
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
});
|
|
432
|
-
};
|
|
433
|
-
|
|
434
|
-
function wrapMultiple(keys, work, options, cb) {
|
|
435
|
-
/**
|
|
436
|
-
* We create a unique key for the multiple keys
|
|
437
|
-
* by concatenating them
|
|
438
|
-
*/
|
|
439
|
-
var combinedKey = keys.reduce(function(acc, k) {
|
|
440
|
-
return acc + k;
|
|
441
|
-
}, '');
|
|
442
|
-
|
|
443
|
-
var hasKey = callbackFiller.has(combinedKey);
|
|
444
|
-
callbackFiller.add(combinedKey, {cb: cb});
|
|
445
|
-
if (hasKey) { return; }
|
|
446
|
-
|
|
447
|
-
keys.push(options);
|
|
448
|
-
keys.push(onResult);
|
|
449
|
-
|
|
450
|
-
/**
|
|
451
|
-
* Get from all the caches. If multiple keys have been passed,
|
|
452
|
-
* we'll go through all the caches and merge the result
|
|
453
|
-
*/
|
|
454
|
-
getFromHighestPriorityCache.apply(this, keys);
|
|
455
|
-
|
|
456
|
-
function onResult(err, result, index) {
|
|
457
|
-
if (err) {
|
|
458
|
-
return done(err);
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
* If all the values returned are cacheable we don't need
|
|
463
|
-
* to call our "work" method and the values returned by the cache
|
|
464
|
-
* are valid. If one or more of the values is not cacheable
|
|
465
|
-
* the cache result is not valid.
|
|
466
|
-
*/
|
|
467
|
-
var cacheOK = result.filter(function(_result) {
|
|
468
|
-
return self._isCacheableValue(_result);
|
|
469
|
-
}).length === result.length;
|
|
470
|
-
|
|
471
|
-
if (!cacheOK) {
|
|
472
|
-
/**
|
|
473
|
-
* We need to fetch the data first
|
|
474
|
-
*/
|
|
475
|
-
return work(workCallback);
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
var cachesToUpdate = caches.slice(0, index);
|
|
479
|
-
|
|
480
|
-
/**
|
|
481
|
-
* Prepare arguments to set the values in
|
|
482
|
-
* higher priority caches
|
|
483
|
-
*/
|
|
484
|
-
var _args = [cachesToUpdate];
|
|
485
|
-
|
|
486
|
-
/**
|
|
487
|
-
* Add the {key, value} pair
|
|
488
|
-
*/
|
|
489
|
-
result.forEach(function(value, i) {
|
|
490
|
-
_args.push(keys[i]);
|
|
491
|
-
_args.push(value);
|
|
492
|
-
});
|
|
493
|
-
|
|
494
|
-
/**
|
|
495
|
-
* Add options and final callback
|
|
496
|
-
*/
|
|
497
|
-
_args.push(options);
|
|
498
|
-
_args.push(function(err) {
|
|
499
|
-
done(err, result);
|
|
500
|
-
});
|
|
501
|
-
|
|
502
|
-
return setInMultipleCaches.apply(null, _args);
|
|
503
|
-
|
|
504
|
-
/**
|
|
505
|
-
* Wrapped function callback
|
|
506
|
-
*/
|
|
507
|
-
function workCallback(err, data) {
|
|
508
|
-
if (err) {
|
|
509
|
-
return done(err);
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
/**
|
|
513
|
-
* Prepare arguments for "setInMultipleCaches"
|
|
514
|
-
*/
|
|
515
|
-
var _args;
|
|
516
|
-
|
|
517
|
-
_args = [];
|
|
518
|
-
data.forEach(function(value, i) {
|
|
519
|
-
/**
|
|
520
|
-
* Add the {key, value} pair to the args
|
|
521
|
-
* array that we will send to mset()
|
|
522
|
-
*/
|
|
523
|
-
if (self._isCacheableValue(value)) {
|
|
524
|
-
_args.push(keys[i]);
|
|
525
|
-
_args.push(value);
|
|
526
|
-
}
|
|
527
|
-
});
|
|
528
|
-
// If no key,value --> exit
|
|
529
|
-
if (_args.length === 0) {
|
|
530
|
-
return done(null);
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
/**
|
|
534
|
-
* Add options and final callback
|
|
535
|
-
*/
|
|
536
|
-
_args.push(options);
|
|
537
|
-
_args.push(function(err) {
|
|
538
|
-
done(err, data);
|
|
539
|
-
});
|
|
540
|
-
|
|
541
|
-
setInMultipleCaches.apply(null, _args);
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
/**
|
|
545
|
-
* Final callback
|
|
546
|
-
*/
|
|
547
|
-
function done(err, data) {
|
|
548
|
-
callbackFiller.fill(combinedKey, err, data);
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
/**
|
|
554
|
-
* Set value in all caches
|
|
555
|
-
*
|
|
556
|
-
* @function
|
|
557
|
-
* @name set
|
|
558
|
-
*
|
|
559
|
-
* @param {string} key
|
|
560
|
-
* @param {*} value
|
|
561
|
-
* @param {object} [options] to pass to underlying set function.
|
|
562
|
-
* @param {function} [cb]
|
|
563
|
-
*/
|
|
564
|
-
self.set = setInMultipleCaches;
|
|
565
|
-
|
|
566
|
-
/**
|
|
567
|
-
* Set multiple values in all caches
|
|
568
|
-
* Accepts an unlimited pair of {key, value}
|
|
569
|
-
*
|
|
570
|
-
* @function
|
|
571
|
-
* @name mset
|
|
572
|
-
*
|
|
573
|
-
* @param {string} key
|
|
574
|
-
* @param {*} value
|
|
575
|
-
* @param {string} [key2]
|
|
576
|
-
* @param {*} [value2]
|
|
577
|
-
* @param {object} [options] to pass to underlying set function.
|
|
578
|
-
* @param {function} [cb]
|
|
579
|
-
*/
|
|
580
|
-
self.mset = setInMultipleCaches;
|
|
581
|
-
|
|
582
|
-
/**
|
|
583
|
-
* Get value from highest level cache that has stored it.
|
|
584
|
-
*
|
|
585
|
-
* @function
|
|
586
|
-
* @name get
|
|
587
|
-
*
|
|
588
|
-
* @param {string} key
|
|
589
|
-
* @param {object} [options] to pass to underlying get function.
|
|
590
|
-
* @param {function} cb
|
|
591
|
-
*/
|
|
592
|
-
self.get = getFromHighestPriorityCache;
|
|
593
|
-
|
|
594
|
-
/**
|
|
595
|
-
* Get multiple value from highest level cache that has stored it.
|
|
596
|
-
* If some values are not found, the next highest cache is used
|
|
597
|
-
* until either all keys are found or all caches have been fetched.
|
|
598
|
-
* Accepts an unlimited number of keys.
|
|
599
|
-
*
|
|
600
|
-
* @function
|
|
601
|
-
* @name mget
|
|
602
|
-
*
|
|
603
|
-
* @param {string} key key to get (any number)
|
|
604
|
-
* @param {object} [options] to pass to underlying get function.
|
|
605
|
-
* @param {function} cb optional callback
|
|
606
|
-
*/
|
|
607
|
-
self.mget = getFromHighestPriorityCache;
|
|
608
|
-
|
|
609
|
-
/**
|
|
610
|
-
* Delete value from all caches.
|
|
611
|
-
*
|
|
612
|
-
* @function
|
|
613
|
-
* @name del
|
|
614
|
-
*
|
|
615
|
-
* @param {string} key
|
|
616
|
-
* @param {object} [options] to pass to underlying del function.
|
|
617
|
-
* @param {function} cb
|
|
618
|
-
*/
|
|
619
|
-
self.del = function() {
|
|
620
|
-
var args = Array.prototype.slice.apply(arguments);
|
|
621
|
-
var cb;
|
|
622
|
-
var options = {};
|
|
623
|
-
|
|
624
|
-
if (typeof args[args.length - 1] === 'function') {
|
|
625
|
-
cb = args.pop();
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
if (isObject(args[args.length - 1])) {
|
|
629
|
-
options = args.pop();
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
args.push(options);
|
|
633
|
-
async.each(caches, function(cache, next) {
|
|
634
|
-
var _args = Array.prototype.slice.apply(args);
|
|
635
|
-
_args.push(next);
|
|
636
|
-
cache.store.del.apply(cache.store, _args);
|
|
637
|
-
}, cb);
|
|
638
|
-
};
|
|
639
|
-
|
|
640
|
-
/**
|
|
641
|
-
* Reset all caches.
|
|
642
|
-
*
|
|
643
|
-
* @function
|
|
644
|
-
* @name reset
|
|
645
|
-
*
|
|
646
|
-
* @param {function} cb
|
|
647
|
-
*/
|
|
648
|
-
self.reset = function(cb) {
|
|
649
|
-
async.each(caches, function(cache, next) {
|
|
650
|
-
cache.store.reset(next);
|
|
651
|
-
}, cb);
|
|
652
|
-
};
|
|
653
|
-
|
|
654
|
-
return self;
|
|
655
|
-
};
|
|
656
|
-
|
|
657
|
-
module.exports = multiCaching;
|