cache-manager 4.1.0 → 5.0.0
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 +32 -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/caching.js
DELETED
|
@@ -1,384 +0,0 @@
|
|
|
1
|
-
/** @module cacheManager/caching */
|
|
2
|
-
/*jshint maxcomplexity:16*/
|
|
3
|
-
var CallbackFiller = require('./callback_filler');
|
|
4
|
-
var utils = require('./utils');
|
|
5
|
-
var memory = require('./stores/memory.js');
|
|
6
|
-
var none = require('./stores/none.js');
|
|
7
|
-
var parseWrapArguments = utils.parseWrapArguments;
|
|
8
|
-
|
|
9
|
-
var bundledStores = {memory, none};
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Generic caching interface that wraps any caching library with a compatible interface.
|
|
13
|
-
*
|
|
14
|
-
* @param {object} args
|
|
15
|
-
* @param {object|string} args.store - The store must at least have `set` and a `get` functions.
|
|
16
|
-
* @param {function} [args.isCacheableValue] - A callback function which is called
|
|
17
|
-
* with every value returned from cache or from a wrapped function. This lets you specify
|
|
18
|
-
* which values should and should not be cached. If the function returns true, it will be
|
|
19
|
-
* stored in cache. By default it caches everything except undefined.
|
|
20
|
-
*/
|
|
21
|
-
var caching = function(args) {
|
|
22
|
-
args = args || {};
|
|
23
|
-
var self = {};
|
|
24
|
-
if (typeof args.store === 'object') {
|
|
25
|
-
if (args.store.create) {
|
|
26
|
-
self.store = args.store.create(args);
|
|
27
|
-
} else {
|
|
28
|
-
self.store = args.store;
|
|
29
|
-
}
|
|
30
|
-
} else {
|
|
31
|
-
var storeName = args.store || 'memory';
|
|
32
|
-
self.store = bundledStores[storeName].create(args);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// do we handle a cache error the same as a cache miss?
|
|
36
|
-
self.ignoreCacheErrors = args.ignoreCacheErrors || false;
|
|
37
|
-
self.refreshThreshold = args.refreshThreshold || false;
|
|
38
|
-
|
|
39
|
-
var Promise = args.promiseDependency || global.Promise;
|
|
40
|
-
|
|
41
|
-
var callbackFiller = new CallbackFiller();
|
|
42
|
-
var backgroundQueue = new Set();
|
|
43
|
-
|
|
44
|
-
if (typeof args.isCacheableValue === 'function') {
|
|
45
|
-
self._isCacheableValue = args.isCacheableValue;
|
|
46
|
-
} else if (typeof self.store.isCacheableValue === 'function') {
|
|
47
|
-
self._isCacheableValue = self.store.isCacheableValue.bind(self.store);
|
|
48
|
-
} else {
|
|
49
|
-
self._isCacheableValue = function(value) {
|
|
50
|
-
return value !== undefined;
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function wrapPromise(key, promise, options) {
|
|
55
|
-
return new Promise(function(resolve, reject) {
|
|
56
|
-
self.wrap(key, function(cb) {
|
|
57
|
-
Promise.resolve()
|
|
58
|
-
.then(promise)
|
|
59
|
-
.then(function(result) {
|
|
60
|
-
cb(null, result);
|
|
61
|
-
return null;
|
|
62
|
-
})
|
|
63
|
-
.catch(cb);
|
|
64
|
-
}, options, function(err, result) {
|
|
65
|
-
if (err) {
|
|
66
|
-
return reject(err);
|
|
67
|
-
}
|
|
68
|
-
resolve(result);
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function handleBackgroundRefresh(key, work, options) {
|
|
74
|
-
if (self.refreshThreshold && !backgroundQueue.has(key)) {
|
|
75
|
-
backgroundQueue.add(key);
|
|
76
|
-
self.checkRefreshThreshold(key, function(err, isExpiring) {
|
|
77
|
-
if (err) {
|
|
78
|
-
backgroundQueue.delete(key);
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
if (isExpiring) {
|
|
82
|
-
work(function(err, data) {
|
|
83
|
-
if (err || !self._isCacheableValue(data)) {
|
|
84
|
-
backgroundQueue.delete(key);
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
if (options && typeof options.ttl === 'function') {
|
|
88
|
-
options.ttl = options.ttl(data);
|
|
89
|
-
}
|
|
90
|
-
self.store.set(key, data, options, function() {
|
|
91
|
-
backgroundQueue.delete(key);
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
} else {
|
|
95
|
-
backgroundQueue.delete(key);
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Checks if the current key is expiring. I.e., if a refreshThreshold is set for this cache
|
|
103
|
-
* and if the cache supports the ttl method, this method checks if the remaining ttl is
|
|
104
|
-
* less than the refreshThreshold.
|
|
105
|
-
* In all other cases this method's callback will contain "false" (ie. not expiring).
|
|
106
|
-
*
|
|
107
|
-
* @function
|
|
108
|
-
* @name checkRefreshThreshold
|
|
109
|
-
*
|
|
110
|
-
* @param {string} key - The cache key to check.
|
|
111
|
-
* @param {function} cb
|
|
112
|
-
*/
|
|
113
|
-
self.checkRefreshThreshold = function(key, cb) {
|
|
114
|
-
if (self.refreshThreshold && typeof self.store.ttl === 'function') {
|
|
115
|
-
return self.store.ttl(key, function(ttlErr, ttl) {
|
|
116
|
-
if (ttlErr || typeof ttl !== 'number') {
|
|
117
|
-
return cb(new Error('Invalid TTL response'));
|
|
118
|
-
}
|
|
119
|
-
if (self.refreshThreshold > ttl) {
|
|
120
|
-
return cb(null, true);
|
|
121
|
-
}
|
|
122
|
-
return cb(null, false);
|
|
123
|
-
});
|
|
124
|
-
} else {
|
|
125
|
-
return cb(new Error('Unhandled refresh'));
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Wraps a function in cache. I.e., the first time the function is run,
|
|
131
|
-
* its results are stored in cache so subsequent calls retrieve from cache
|
|
132
|
-
* instead of calling the function.
|
|
133
|
-
* You can pass any number of keys as long as the wrapped function returns
|
|
134
|
-
* an array with the same number of values and in the same order.
|
|
135
|
-
*
|
|
136
|
-
* @function
|
|
137
|
-
* @name wrap
|
|
138
|
-
*
|
|
139
|
-
* @param {string} key - The cache key to use in cache operations. Can be one or many.
|
|
140
|
-
* @param {function} work - The function to wrap
|
|
141
|
-
* @param {object} [options] - options passed to `set` function
|
|
142
|
-
* @param {function} cb
|
|
143
|
-
*
|
|
144
|
-
* @example
|
|
145
|
-
* var key = 'user_' + userId;
|
|
146
|
-
* cache.wrap(key, function(cb) {
|
|
147
|
-
* User.get(userId, cb);
|
|
148
|
-
* }, function(err, user) {
|
|
149
|
-
* console.log(user);
|
|
150
|
-
* });
|
|
151
|
-
*
|
|
152
|
-
* // Multiple keys
|
|
153
|
-
* var key = 'user_' + userId;
|
|
154
|
-
* var key2 = 'user_' + userId2;
|
|
155
|
-
* cache.wrap(key, key2, function(cb) {
|
|
156
|
-
* User.getMany([userId, userId2], cb);
|
|
157
|
-
* }, function(err, users) {
|
|
158
|
-
* console.log(users[0]);
|
|
159
|
-
* console.log(users[1]);
|
|
160
|
-
* });
|
|
161
|
-
*/
|
|
162
|
-
self.wrap = function() {
|
|
163
|
-
var parsedArgs = parseWrapArguments(Array.prototype.slice.apply(arguments));
|
|
164
|
-
var keys = parsedArgs.keys;
|
|
165
|
-
var work = parsedArgs.work;
|
|
166
|
-
var options = parsedArgs.options;
|
|
167
|
-
var cb = parsedArgs.cb;
|
|
168
|
-
|
|
169
|
-
if (!cb) {
|
|
170
|
-
keys.push(work);
|
|
171
|
-
keys.push(options);
|
|
172
|
-
return wrapPromise.apply(this, keys);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
if (keys.length > 1) {
|
|
176
|
-
/**
|
|
177
|
-
* Handle more than 1 key
|
|
178
|
-
*/
|
|
179
|
-
return wrapMultiple(keys, work, options, cb);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
var key = keys[0];
|
|
183
|
-
|
|
184
|
-
var hasKey = callbackFiller.has(key);
|
|
185
|
-
callbackFiller.add(key, {cb: cb});
|
|
186
|
-
if (hasKey) { return; }
|
|
187
|
-
|
|
188
|
-
self.store.get(key, options, function(err, result) {
|
|
189
|
-
if (err && (!self.ignoreCacheErrors)) {
|
|
190
|
-
callbackFiller.fill(key, err);
|
|
191
|
-
} else if (self._isCacheableValue(result)) {
|
|
192
|
-
handleBackgroundRefresh(key, work, options);
|
|
193
|
-
callbackFiller.fill(key, null, result);
|
|
194
|
-
} else {
|
|
195
|
-
work(function(err, data) {
|
|
196
|
-
if (err) {
|
|
197
|
-
callbackFiller.fill(key, err);
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if (!self._isCacheableValue(data)) {
|
|
202
|
-
callbackFiller.fill(key, null, data);
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
if (options && typeof options.ttl === 'function') {
|
|
207
|
-
options.ttl = options.ttl(data);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
self.store.set(key, data, options, function(err) {
|
|
211
|
-
if (err && (!self.ignoreCacheErrors)) {
|
|
212
|
-
callbackFiller.fill(key, err);
|
|
213
|
-
} else {
|
|
214
|
-
callbackFiller.fill(key, null, data);
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
};
|
|
221
|
-
|
|
222
|
-
function wrapMultiple(keys, work, options, cb) {
|
|
223
|
-
/**
|
|
224
|
-
* We create a unique key for the multiple keys
|
|
225
|
-
* by concatenating them
|
|
226
|
-
*/
|
|
227
|
-
var combinedKey = keys.reduce(function(acc, k) {
|
|
228
|
-
return acc + k;
|
|
229
|
-
}, '');
|
|
230
|
-
|
|
231
|
-
var hasKey = callbackFiller.has(combinedKey);
|
|
232
|
-
callbackFiller.add(combinedKey, {cb: cb});
|
|
233
|
-
if (hasKey) { return; }
|
|
234
|
-
|
|
235
|
-
keys.push(options);
|
|
236
|
-
keys.push(onResult);
|
|
237
|
-
|
|
238
|
-
self.store.mget.apply(self.store, keys);
|
|
239
|
-
|
|
240
|
-
function onResult(err, result) {
|
|
241
|
-
if (err && (!self.ignoreCacheErrors)) {
|
|
242
|
-
return callbackFiller.fill(combinedKey, err);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* If all the values returned are cacheable we don't need
|
|
247
|
-
* to call our "work" method and the values returned by the cache
|
|
248
|
-
* are valid. If one or more of the values is not cacheable
|
|
249
|
-
* the cache result is not valid.
|
|
250
|
-
*/
|
|
251
|
-
var cacheOk = Array.isArray(result) && result.filter(function(_result) {
|
|
252
|
-
return self._isCacheableValue(_result);
|
|
253
|
-
}).length === result.length;
|
|
254
|
-
|
|
255
|
-
if (cacheOk) {
|
|
256
|
-
return callbackFiller.fill(combinedKey, null, result);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
return work(function(err, data) {
|
|
260
|
-
if (err || !data) {
|
|
261
|
-
return done(err);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
var _args = [];
|
|
265
|
-
data.forEach(function(value, i) {
|
|
266
|
-
/**
|
|
267
|
-
* Add the {key, value} pair to the args
|
|
268
|
-
* array that we will send to mset()
|
|
269
|
-
*/
|
|
270
|
-
if (self._isCacheableValue(value)) {
|
|
271
|
-
_args.push(keys[i]);
|
|
272
|
-
_args.push(value);
|
|
273
|
-
}
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
// If no key|value, exit
|
|
277
|
-
if (_args.length === 0) {
|
|
278
|
-
return done(null);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
if (options && typeof options.ttl === 'function') {
|
|
282
|
-
options.ttl = options.ttl(data);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
_args.push(options);
|
|
286
|
-
_args.push(done);
|
|
287
|
-
|
|
288
|
-
self.store.mset.apply(self.store, _args);
|
|
289
|
-
|
|
290
|
-
function done(err) {
|
|
291
|
-
if (err && (!self.ignoreCacheErrors)) {
|
|
292
|
-
callbackFiller.fill(combinedKey, err);
|
|
293
|
-
} else {
|
|
294
|
-
callbackFiller.fill(combinedKey, null, data);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* Binds to the underlying store's `get` function.
|
|
303
|
-
* @function
|
|
304
|
-
* @name get
|
|
305
|
-
*/
|
|
306
|
-
self.get = self.store.get.bind(self.store);
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Get multiple keys at once.
|
|
310
|
-
* Binds to the underlying store's `mget` function.
|
|
311
|
-
* @function
|
|
312
|
-
* @name mget
|
|
313
|
-
*/
|
|
314
|
-
if (typeof self.store.mget === 'function') {
|
|
315
|
-
self.mget = self.store.mget.bind(self.store);
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* Binds to the underlying store's `set` function.
|
|
320
|
-
* @function
|
|
321
|
-
* @name set
|
|
322
|
-
*/
|
|
323
|
-
self.set = self.store.set.bind(self.store);
|
|
324
|
-
|
|
325
|
-
/**
|
|
326
|
-
* Set multiple keys at once.
|
|
327
|
-
* It accepts any number of {key, value} pair
|
|
328
|
-
* Binds to the underlying store's `mset` function.
|
|
329
|
-
* @function
|
|
330
|
-
* @name mset
|
|
331
|
-
*/
|
|
332
|
-
if (typeof self.store.mset === 'function') {
|
|
333
|
-
self.mset = self.store.mset.bind(self.store);
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* Binds to the underlying store's `del` function if it exists.
|
|
338
|
-
* @function
|
|
339
|
-
* @name del
|
|
340
|
-
*/
|
|
341
|
-
if (typeof self.store.del === 'function') {
|
|
342
|
-
self.del = self.store.del.bind(self.store);
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Binds to the underlying store's `setex` function if it exists.
|
|
347
|
-
* @function
|
|
348
|
-
* @name setex
|
|
349
|
-
*/
|
|
350
|
-
if (typeof self.store.setex === 'function') {
|
|
351
|
-
self.setex = self.store.setex.bind(self.store);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* Binds to the underlying store's `reset` function if it exists.
|
|
356
|
-
* @function
|
|
357
|
-
* @name reset
|
|
358
|
-
*/
|
|
359
|
-
if (typeof self.store.reset === 'function') {
|
|
360
|
-
self.reset = self.store.reset.bind(self.store);
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* Binds to the underlying store's `keys` function if it exists.
|
|
365
|
-
* @function
|
|
366
|
-
* @name keys
|
|
367
|
-
*/
|
|
368
|
-
if (typeof self.store.keys === 'function') {
|
|
369
|
-
self.keys = self.store.keys.bind(self.store);
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
/**
|
|
373
|
-
* Binds to the underlying store's `ttl` function if it exists.
|
|
374
|
-
* @function
|
|
375
|
-
* @name ttl
|
|
376
|
-
*/
|
|
377
|
-
if (typeof self.store.ttl === 'function') {
|
|
378
|
-
self.ttl = self.store.ttl.bind(self.store);
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
return self;
|
|
382
|
-
};
|
|
383
|
-
|
|
384
|
-
module.exports = caching;
|
package/lib/callback_filler.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
function CallbackFiller() {
|
|
2
|
-
this.queues = {};
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
CallbackFiller.prototype.fill = function(key, err, data) {
|
|
6
|
-
var waiting = this.queues[key];
|
|
7
|
-
delete this.queues[key];
|
|
8
|
-
|
|
9
|
-
if (waiting && waiting.length) {
|
|
10
|
-
waiting.forEach(function(task) {
|
|
11
|
-
(task.cb)(err, data);
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
CallbackFiller.prototype.has = function(key) {
|
|
17
|
-
return this.queues[key];
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
CallbackFiller.prototype.add = function(key, funcObj) {
|
|
21
|
-
if (this.queues[key]) {
|
|
22
|
-
this.queues[key].push(funcObj);
|
|
23
|
-
} else {
|
|
24
|
-
this.queues[key] = [funcObj];
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
module.exports = CallbackFiller;
|