cache-manager 3.1.0 → 3.4.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/History.md +12 -0
- package/README.md +27 -0
- package/lib/caching.js +59 -0
- package/lib/multi_caching.js +28 -0
- package/lib/stores/memory.js +1 -1
- package/package.json +11 -11
- package/test/caching.unit.js +315 -32
- package/test/multi_caching.unit.js +460 -0
- package/test/stores/memory.unit.js +2 -1
package/History.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
- 3.4.0 2020-04-30
|
|
2
|
+
- Update dependencies (#145). - @orgads
|
|
3
|
+
|
|
4
|
+
- 3.3.0 2020-04-30
|
|
5
|
+
- Use full lodash lib instead of lodash.clonedeep npm (#141)
|
|
6
|
+
|
|
7
|
+
- 3.2.1 2020-03-20
|
|
8
|
+
- Background refresh fixes (#139). -@marcoreni
|
|
9
|
+
|
|
10
|
+
- 3.2.0 2020-03-13
|
|
11
|
+
- Background refresh of expiring entries (#138). - @marcoreni
|
|
12
|
+
|
|
1
13
|
- 3.1.0 2020-02-24
|
|
2
14
|
- Pass updateAgeOnGet to lru-cache in memory-store (#136). - @BastianKubaile
|
|
3
15
|
|
package/README.md
CHANGED
|
@@ -426,6 +426,33 @@ var multiCache = cacheManager.multiCaching([memoryCache, someOtherCache], {
|
|
|
426
426
|
|
|
427
427
|
```
|
|
428
428
|
|
|
429
|
+
### Refresh cache keys in background
|
|
430
|
+
|
|
431
|
+
Both the `caching` and `multicaching` modules support a mechanism to refresh expiring cache keys in background when using `wrap` function.
|
|
432
|
+
This is done by adding a `refreshThreshold` attribute while creating the caching store.
|
|
433
|
+
|
|
434
|
+
If `refreshThreshold` is set and if the `ttl` method is available for the used store, after retrieving a value from cache TTL will be checked.
|
|
435
|
+
If the remaining TTL is less than `refreshThreshold`, the system will spawn a background worker to update the value, following same rules as standard fetching. In the meantime, the system will return the old value until expiration.
|
|
436
|
+
|
|
437
|
+
In case of multicaching, the store that will be used for refresh is the one where the key will be found first (highest priority). The value will then be set in all the stores.
|
|
438
|
+
|
|
439
|
+
NOTES:
|
|
440
|
+
|
|
441
|
+
* In case of multicaching, the store that will be checked for refresh is the one where the key will be found first (highest priority).
|
|
442
|
+
* If the threshold is low and the worker function is slow, the key may expire and you may encounter a racing condition with updating values.
|
|
443
|
+
* The background refresh mechanism currently does not support providing multiple keys to `wrap` function.
|
|
444
|
+
* The caching store needs to provide the `ttl` method.
|
|
445
|
+
|
|
446
|
+
For example, pass the refreshThreshold to `caching` like this:
|
|
447
|
+
|
|
448
|
+
```javascript
|
|
449
|
+
var redisStore = require('cache-manager-ioredis');
|
|
450
|
+
|
|
451
|
+
var redisCache = cacheManager.caching({store: redisStore, refreshThreshold: 3, isCacheableValue: isCacheableValue});
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
When a value will be retrieved from Redis with a remaining TTL < 3sec, the value will be updated in background.
|
|
455
|
+
|
|
429
456
|
### Development environment
|
|
430
457
|
You may disable real caching but still get all the callback functionality working by setting `none` store.
|
|
431
458
|
|
package/lib/caching.js
CHANGED
|
@@ -30,10 +30,12 @@ var caching = function(args) {
|
|
|
30
30
|
|
|
31
31
|
// do we handle a cache error the same as a cache miss?
|
|
32
32
|
self.ignoreCacheErrors = args.ignoreCacheErrors || false;
|
|
33
|
+
self.refreshThreshold = args.refreshThreshold || false;
|
|
33
34
|
|
|
34
35
|
var Promise = args.promiseDependency || global.Promise;
|
|
35
36
|
|
|
36
37
|
var callbackFiller = new CallbackFiller();
|
|
38
|
+
var backgroundQueue = new Set();
|
|
37
39
|
|
|
38
40
|
if (typeof args.isCacheableValue === 'function') {
|
|
39
41
|
self._isCacheableValue = args.isCacheableValue;
|
|
@@ -64,6 +66,62 @@ var caching = function(args) {
|
|
|
64
66
|
});
|
|
65
67
|
}
|
|
66
68
|
|
|
69
|
+
function handleBackgroundRefresh(key, work, options) {
|
|
70
|
+
if (self.refreshThreshold && !backgroundQueue.has(key)) {
|
|
71
|
+
backgroundQueue.add(key);
|
|
72
|
+
self.checkRefreshThreshold(key, function(err, isExpiring) {
|
|
73
|
+
if (err) {
|
|
74
|
+
backgroundQueue.delete(key);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (isExpiring) {
|
|
78
|
+
work(function(err, data) {
|
|
79
|
+
if (err || !self._isCacheableValue(data)) {
|
|
80
|
+
backgroundQueue.delete(key);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (options && typeof options.ttl === 'function') {
|
|
84
|
+
options.ttl = options.ttl(data);
|
|
85
|
+
}
|
|
86
|
+
self.store.set(key, data, options, function() {
|
|
87
|
+
backgroundQueue.delete(key);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
} else {
|
|
91
|
+
backgroundQueue.delete(key);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Checks if the current key is expiring. I.e., if a refreshThreshold is set for this cache
|
|
99
|
+
* and if the cache supports the ttl method, this method checks if the remaining ttl is
|
|
100
|
+
* less than the refreshThreshold.
|
|
101
|
+
* In all other cases this method's callback will contain "false" (ie. not expiring).
|
|
102
|
+
*
|
|
103
|
+
* @function
|
|
104
|
+
* @name checkRefreshThreshold
|
|
105
|
+
*
|
|
106
|
+
* @param {string} key - The cache key to check.
|
|
107
|
+
* @param {function} cb
|
|
108
|
+
*/
|
|
109
|
+
self.checkRefreshThreshold = function(key, cb) {
|
|
110
|
+
if (self.refreshThreshold && typeof self.store.ttl === 'function') {
|
|
111
|
+
return self.store.ttl(key, function(ttlErr, ttl) {
|
|
112
|
+
if (ttlErr || typeof ttl !== 'number') {
|
|
113
|
+
return cb(new Error('Invalid TTL response'));
|
|
114
|
+
}
|
|
115
|
+
if (self.refreshThreshold > ttl) {
|
|
116
|
+
return cb(null, true);
|
|
117
|
+
}
|
|
118
|
+
return cb(null, false);
|
|
119
|
+
});
|
|
120
|
+
} else {
|
|
121
|
+
return cb(new Error('Unhandled refresh'));
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
67
125
|
/**
|
|
68
126
|
* Wraps a function in cache. I.e., the first time the function is run,
|
|
69
127
|
* its results are stored in cache so subsequent calls retrieve from cache
|
|
@@ -127,6 +185,7 @@ var caching = function(args) {
|
|
|
127
185
|
if (err && (!self.ignoreCacheErrors)) {
|
|
128
186
|
callbackFiller.fill(key, err);
|
|
129
187
|
} else if (self._isCacheableValue(result)) {
|
|
188
|
+
handleBackgroundRefresh(key, work, options);
|
|
130
189
|
callbackFiller.fill(key, null, result);
|
|
131
190
|
} else {
|
|
132
191
|
work(function(err, data) {
|
package/lib/multi_caching.js
CHANGED
|
@@ -29,6 +29,7 @@ var multiCaching = function(caches, options) {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
var callbackFiller = new CallbackFiller();
|
|
32
|
+
var backgroundQueue = new Set();
|
|
32
33
|
|
|
33
34
|
if (typeof options.isCacheableValue === 'function') {
|
|
34
35
|
self._isCacheableValue = options.isCacheableValue;
|
|
@@ -323,6 +324,32 @@ var multiCaching = function(caches, options) {
|
|
|
323
324
|
});
|
|
324
325
|
}
|
|
325
326
|
|
|
327
|
+
function handleBackgroundRefresh(caches, index, key, work, options) {
|
|
328
|
+
if (caches[index].refreshThreshold && !backgroundQueue.has(key)) {
|
|
329
|
+
backgroundQueue.add(key);
|
|
330
|
+
caches[index].checkRefreshThreshold(key, function(err, isExpiring) {
|
|
331
|
+
if (err) {
|
|
332
|
+
backgroundQueue.delete(key);
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
if (isExpiring) {
|
|
336
|
+
work(function(workErr, workData) {
|
|
337
|
+
if (workErr || !self._isCacheableValue(workData)) {
|
|
338
|
+
backgroundQueue.delete(key);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
var args = [caches, key, workData, options, function() {
|
|
342
|
+
backgroundQueue.delete(key);
|
|
343
|
+
}];
|
|
344
|
+
setInMultipleCaches.apply(null, args);
|
|
345
|
+
});
|
|
346
|
+
} else {
|
|
347
|
+
backgroundQueue.delete(key);
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
326
353
|
/**
|
|
327
354
|
* Wraps a function in one or more caches.
|
|
328
355
|
* Has same API as regular caching module.
|
|
@@ -373,6 +400,7 @@ var multiCaching = function(caches, options) {
|
|
|
373
400
|
if (err) {
|
|
374
401
|
return callbackFiller.fill(key, err);
|
|
375
402
|
} else if (self._isCacheableValue(result)) {
|
|
403
|
+
handleBackgroundRefresh(caches, index, key, work, options);
|
|
376
404
|
var cachesToUpdate = caches.slice(0, index);
|
|
377
405
|
var args = [cachesToUpdate, key, result, options, function(err) {
|
|
378
406
|
callbackFiller.fill(key, err, result);
|
package/lib/stores/memory.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cache-manager",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"description": "Cache module for Node.js",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -20,18 +20,18 @@
|
|
|
20
20
|
"author": "Bryan Donovan",
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"lru-cache": "
|
|
23
|
+
"async": "^3.2.0",
|
|
24
|
+
"lodash": "^4.17.20",
|
|
25
|
+
"lru-cache": "6.0.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"coveralls": "3.0
|
|
29
|
-
"es6-promise": "^
|
|
30
|
-
"eslint": "
|
|
31
|
-
"jsdoc": "3.
|
|
32
|
-
"mocha": "
|
|
33
|
-
"nyc": "
|
|
28
|
+
"coveralls": "3.1.0",
|
|
29
|
+
"es6-promise": "^4.2.8",
|
|
30
|
+
"eslint": "7.7.0",
|
|
31
|
+
"jsdoc": "3.6.5",
|
|
32
|
+
"mocha": "8.1.1",
|
|
33
|
+
"nyc": "15.1.0",
|
|
34
34
|
"optimist": "0.6.1",
|
|
35
|
-
"sinon": "
|
|
35
|
+
"sinon": "9.0.3"
|
|
36
36
|
}
|
|
37
37
|
}
|
package/test/caching.unit.js
CHANGED
|
@@ -397,8 +397,8 @@ describe("caching", function() {
|
|
|
397
397
|
|
|
398
398
|
cache = caching({store: 'memory'});
|
|
399
399
|
|
|
400
|
-
function isDone() {
|
|
401
|
-
|
|
400
|
+
function isDone(cb) {
|
|
401
|
+
cb(null, processed === keyCount);
|
|
402
402
|
}
|
|
403
403
|
|
|
404
404
|
async.until(isDone, function(cb) {
|
|
@@ -437,8 +437,8 @@ describe("caching", function() {
|
|
|
437
437
|
|
|
438
438
|
cache = caching({store: memoryStore.create({noPromises: true})});
|
|
439
439
|
|
|
440
|
-
function isDone() {
|
|
441
|
-
|
|
440
|
+
function isDone(cb) {
|
|
441
|
+
cb(null, processed === keyCount);
|
|
442
442
|
}
|
|
443
443
|
|
|
444
444
|
async.until(isDone, function(cb) {
|
|
@@ -790,7 +790,7 @@ describe("caching", function() {
|
|
|
790
790
|
it("bubbles up that error", function(done) {
|
|
791
791
|
var fakeError = new Error(support.random.string());
|
|
792
792
|
|
|
793
|
-
sinon.stub(memoryStoreStub, 'get'
|
|
793
|
+
sinon.stub(memoryStoreStub, 'get').callsFake(function(key, options, cb) {
|
|
794
794
|
cb(fakeError);
|
|
795
795
|
});
|
|
796
796
|
|
|
@@ -810,7 +810,7 @@ describe("caching", function() {
|
|
|
810
810
|
|
|
811
811
|
var fakeError = new Error(support.random.string());
|
|
812
812
|
|
|
813
|
-
sinon.stub(memoryStoreStub, 'get'
|
|
813
|
+
sinon.stub(memoryStoreStub, 'get').callsFake(function(key, options, cb) {
|
|
814
814
|
cb(fakeError);
|
|
815
815
|
});
|
|
816
816
|
|
|
@@ -830,7 +830,7 @@ describe("caching", function() {
|
|
|
830
830
|
it("bubbles up that error", function(done) {
|
|
831
831
|
var fakeError = new Error(support.random.string());
|
|
832
832
|
|
|
833
|
-
sinon.stub(memoryStoreStub, 'set'
|
|
833
|
+
sinon.stub(memoryStoreStub, 'set').callsFake(function(key, val, ttl, cb) {
|
|
834
834
|
cb(fakeError);
|
|
835
835
|
});
|
|
836
836
|
|
|
@@ -865,7 +865,7 @@ describe("caching", function() {
|
|
|
865
865
|
context("when wrapped function calls back with an error", function() {
|
|
866
866
|
it("calls back with that error", function(done) {
|
|
867
867
|
var fakeError = new Error(support.random.string());
|
|
868
|
-
sinon.stub(methods, 'getWidget'
|
|
868
|
+
sinon.stub(methods, 'getWidget').callsFake(function(name, cb) {
|
|
869
869
|
cb(fakeError, {name: name});
|
|
870
870
|
});
|
|
871
871
|
|
|
@@ -942,7 +942,7 @@ describe("caching", function() {
|
|
|
942
942
|
context("when result is already cached", function() {
|
|
943
943
|
it("retrieves data from cache", function(done) {
|
|
944
944
|
var funcCalled = false;
|
|
945
|
-
sinon.stub(memoryStoreStub, 'mget'
|
|
945
|
+
sinon.stub(memoryStoreStub, 'mget').callsFake(function() {
|
|
946
946
|
var args = Array.prototype.slice.apply(arguments);
|
|
947
947
|
var cb = args.pop();
|
|
948
948
|
cb(null, [{name: name}, {name: name2}]);
|
|
@@ -1045,7 +1045,7 @@ describe("caching", function() {
|
|
|
1045
1045
|
it("bubbles up that error", function(done) {
|
|
1046
1046
|
var fakeError = new Error(support.random.string());
|
|
1047
1047
|
|
|
1048
|
-
sinon.stub(memoryStoreStub, 'mget'
|
|
1048
|
+
sinon.stub(memoryStoreStub, 'mget').callsFake(function() {
|
|
1049
1049
|
var args = Array.prototype.slice.apply(arguments);
|
|
1050
1050
|
var cb = args.pop();
|
|
1051
1051
|
cb(fakeError);
|
|
@@ -1067,7 +1067,7 @@ describe("caching", function() {
|
|
|
1067
1067
|
|
|
1068
1068
|
var fakeError = new Error(support.random.string());
|
|
1069
1069
|
|
|
1070
|
-
sinon.stub(memoryStoreStub, 'mget'
|
|
1070
|
+
sinon.stub(memoryStoreStub, 'mget').callsFake(function() {
|
|
1071
1071
|
var args = Array.prototype.slice.apply(arguments);
|
|
1072
1072
|
var cb = args.pop();
|
|
1073
1073
|
cb(fakeError);
|
|
@@ -1105,6 +1105,283 @@ describe("caching", function() {
|
|
|
1105
1105
|
});
|
|
1106
1106
|
});
|
|
1107
1107
|
|
|
1108
|
+
describe("using memory (lru-cache) store with a refreshThreshold", function() {
|
|
1109
|
+
var memoryStoreStub;
|
|
1110
|
+
var opts;
|
|
1111
|
+
|
|
1112
|
+
beforeEach(function() {
|
|
1113
|
+
opts = {ttl: 10};
|
|
1114
|
+
memoryStoreStub = memoryStore.create(opts);
|
|
1115
|
+
|
|
1116
|
+
sinon.stub(memoryStore, 'create').returns(memoryStoreStub);
|
|
1117
|
+
|
|
1118
|
+
cache = caching({store: 'memory', ttl: opts.ttl, refreshThreshold: 2, ignoreCacheErrors: false});
|
|
1119
|
+
key = support.random.string(20);
|
|
1120
|
+
name = support.random.string();
|
|
1121
|
+
});
|
|
1122
|
+
|
|
1123
|
+
afterEach(function() {
|
|
1124
|
+
memoryStore.create.restore();
|
|
1125
|
+
});
|
|
1126
|
+
|
|
1127
|
+
function getCachedWidget(name, cb) {
|
|
1128
|
+
cache.wrap(key, function(cacheCb) {
|
|
1129
|
+
methods.getWidget(name, cacheCb);
|
|
1130
|
+
}, opts, cb);
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
context("using standard memorystore", function() {
|
|
1134
|
+
beforeEach(function(done) {
|
|
1135
|
+
getCachedWidget(name, function(err, widget) {
|
|
1136
|
+
checkErr(err);
|
|
1137
|
+
assert.ok(widget);
|
|
1138
|
+
|
|
1139
|
+
memoryStoreStub.get(key, function(err, result) {
|
|
1140
|
+
checkErr(err);
|
|
1141
|
+
assert.ok(result);
|
|
1142
|
+
|
|
1143
|
+
sinon.spy(cache, 'checkRefreshThreshold');
|
|
1144
|
+
done();
|
|
1145
|
+
});
|
|
1146
|
+
});
|
|
1147
|
+
});
|
|
1148
|
+
|
|
1149
|
+
afterEach(function() {
|
|
1150
|
+
cache.checkRefreshThreshold.restore();
|
|
1151
|
+
});
|
|
1152
|
+
|
|
1153
|
+
it("retrieves data from cache and stops background if failing while checking ttl", function(done) {
|
|
1154
|
+
// NOTE: TTL function is not defined
|
|
1155
|
+
var funcCalled = false;
|
|
1156
|
+
cache.wrap(key, function(cb) {
|
|
1157
|
+
funcCalled = true;
|
|
1158
|
+
methods.getWidget(name, function(err, result) {
|
|
1159
|
+
cb(err, result);
|
|
1160
|
+
});
|
|
1161
|
+
}, function(err, widget) {
|
|
1162
|
+
checkErr(err);
|
|
1163
|
+
assert.deepEqual(widget, {name: name});
|
|
1164
|
+
assert.equal(cache.checkRefreshThreshold.callCount, 1);
|
|
1165
|
+
assert.equal(funcCalled, false);
|
|
1166
|
+
|
|
1167
|
+
done();
|
|
1168
|
+
});
|
|
1169
|
+
});
|
|
1170
|
+
});
|
|
1171
|
+
|
|
1172
|
+
context("using tweaked memory store, when result is already cached", function() {
|
|
1173
|
+
beforeEach(function(done) {
|
|
1174
|
+
memoryStoreStub.ttl = function(key, cb) {
|
|
1175
|
+
return cb(null, 5);
|
|
1176
|
+
};
|
|
1177
|
+
sinon.spy(memoryStoreStub, 'ttl');
|
|
1178
|
+
getCachedWidget(name, function(err, widget) {
|
|
1179
|
+
checkErr(err);
|
|
1180
|
+
assert.ok(widget);
|
|
1181
|
+
|
|
1182
|
+
memoryStoreStub.get(key, function(err, result) {
|
|
1183
|
+
checkErr(err);
|
|
1184
|
+
assert.ok(result);
|
|
1185
|
+
|
|
1186
|
+
sinon.spy(memoryStoreStub, 'get');
|
|
1187
|
+
sinon.spy(memoryStoreStub, 'set');
|
|
1188
|
+
|
|
1189
|
+
done();
|
|
1190
|
+
});
|
|
1191
|
+
});
|
|
1192
|
+
});
|
|
1193
|
+
|
|
1194
|
+
afterEach(function() {
|
|
1195
|
+
memoryStoreStub.get.restore();
|
|
1196
|
+
memoryStoreStub.set.restore();
|
|
1197
|
+
memoryStoreStub.ttl.restore();
|
|
1198
|
+
});
|
|
1199
|
+
|
|
1200
|
+
it("retrieves data from cache and checks ttl without refreshing", function(done) {
|
|
1201
|
+
var funcCalled = false;
|
|
1202
|
+
cache.wrap(key, function(cb) {
|
|
1203
|
+
funcCalled = true;
|
|
1204
|
+
methods.getWidget(name, function(err, result) {
|
|
1205
|
+
cb(err, result);
|
|
1206
|
+
});
|
|
1207
|
+
}, function(err, widget) {
|
|
1208
|
+
checkErr(err);
|
|
1209
|
+
assert.deepEqual(widget, {name: name});
|
|
1210
|
+
assert.ok(memoryStoreStub.get.calledWith(key));
|
|
1211
|
+
assert.ok(memoryStoreStub.ttl.calledWith(key));
|
|
1212
|
+
assert.equal(funcCalled, false);
|
|
1213
|
+
assert.equal(memoryStoreStub.set.callCount, 0);
|
|
1214
|
+
done();
|
|
1215
|
+
});
|
|
1216
|
+
});
|
|
1217
|
+
|
|
1218
|
+
it("retrieves data from cache and checks twice ttl without refreshing", function(done) {
|
|
1219
|
+
var funcCalled = 0;
|
|
1220
|
+
var values = [];
|
|
1221
|
+
|
|
1222
|
+
for (var i = 0; i < 2; i++) {
|
|
1223
|
+
values.push(i);
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
async.eachSeries(values, function(val, next) {
|
|
1227
|
+
cache.wrap(key, function(cb) {
|
|
1228
|
+
funcCalled += 1;
|
|
1229
|
+
methods.getWidget(name, function(err, result) {
|
|
1230
|
+
cb(err, result);
|
|
1231
|
+
});
|
|
1232
|
+
}, function(err, widget) {
|
|
1233
|
+
checkErr(err);
|
|
1234
|
+
assert.deepEqual(widget, {name: name});
|
|
1235
|
+
next(err);
|
|
1236
|
+
});
|
|
1237
|
+
}, function(err) {
|
|
1238
|
+
// Wait for just a bit, to be sure that the callback is called.
|
|
1239
|
+
setTimeout(function() {
|
|
1240
|
+
checkErr(err);
|
|
1241
|
+
assert.equal(memoryStoreStub.get.callCount, 2);
|
|
1242
|
+
assert.equal(memoryStoreStub.ttl.callCount, 2);
|
|
1243
|
+
assert.equal(funcCalled, 0);
|
|
1244
|
+
assert.equal(memoryStoreStub.set.callCount, 0);
|
|
1245
|
+
done();
|
|
1246
|
+
}, 500);
|
|
1247
|
+
});
|
|
1248
|
+
});
|
|
1249
|
+
});
|
|
1250
|
+
|
|
1251
|
+
context("using tweaked memorystore, when result is already cached but expiring", function() {
|
|
1252
|
+
beforeEach(function(done) {
|
|
1253
|
+
memoryStoreStub.ttl = function(key, cb) {
|
|
1254
|
+
return cb(null, 1);
|
|
1255
|
+
};
|
|
1256
|
+
sinon.spy(memoryStoreStub, 'ttl');
|
|
1257
|
+
|
|
1258
|
+
getCachedWidget(name, function(err, widget) {
|
|
1259
|
+
checkErr(err);
|
|
1260
|
+
assert.ok(widget);
|
|
1261
|
+
|
|
1262
|
+
memoryStoreStub.get(key, function(err, result) {
|
|
1263
|
+
checkErr(err);
|
|
1264
|
+
assert.ok(result);
|
|
1265
|
+
|
|
1266
|
+
sinon.spy(memoryStoreStub, 'get');
|
|
1267
|
+
sinon.spy(memoryStoreStub, 'set');
|
|
1268
|
+
|
|
1269
|
+
done();
|
|
1270
|
+
});
|
|
1271
|
+
});
|
|
1272
|
+
});
|
|
1273
|
+
|
|
1274
|
+
afterEach(function() {
|
|
1275
|
+
memoryStoreStub.get.restore();
|
|
1276
|
+
memoryStoreStub.set.restore();
|
|
1277
|
+
memoryStoreStub.ttl.restore();
|
|
1278
|
+
});
|
|
1279
|
+
|
|
1280
|
+
it("returns value and invokes worker in background", function(done) {
|
|
1281
|
+
var funcCalled = false;
|
|
1282
|
+
cache.wrap(key, function(cb) {
|
|
1283
|
+
funcCalled = true;
|
|
1284
|
+
methods.getWidget(name, function(err, result) {
|
|
1285
|
+
cb(err, result);
|
|
1286
|
+
});
|
|
1287
|
+
}, function(err, widget) {
|
|
1288
|
+
// Wait for just a bit, to be sure that the callback is called.
|
|
1289
|
+
setTimeout(function() {
|
|
1290
|
+
checkErr(err);
|
|
1291
|
+
assert.deepEqual(widget, {name: name});
|
|
1292
|
+
assert.ok(memoryStoreStub.get.calledWith(key));
|
|
1293
|
+
assert.ok(memoryStoreStub.ttl.calledWith(key));
|
|
1294
|
+
assert.equal(funcCalled, true);
|
|
1295
|
+
assert.equal(memoryStoreStub.set.callCount, 1);
|
|
1296
|
+
done();
|
|
1297
|
+
}, 500);
|
|
1298
|
+
});
|
|
1299
|
+
});
|
|
1300
|
+
|
|
1301
|
+
it("returns value and invokes worker in background using the provided ttl function", function(done) {
|
|
1302
|
+
var funcCalled = false;
|
|
1303
|
+
var ttlFuncCalled = false;
|
|
1304
|
+
var ttlFunc = function() {
|
|
1305
|
+
ttlFuncCalled = true;
|
|
1306
|
+
return 4;
|
|
1307
|
+
};
|
|
1308
|
+
|
|
1309
|
+
cache.wrap(key, function(cb) {
|
|
1310
|
+
funcCalled = true;
|
|
1311
|
+
methods.getWidget(name, function(err, result) {
|
|
1312
|
+
cb(err, result);
|
|
1313
|
+
});
|
|
1314
|
+
}, {ttl: ttlFunc}, function(err, widget) {
|
|
1315
|
+
// Wait for just a bit, to be sure that the callback is called.
|
|
1316
|
+
setTimeout(function() {
|
|
1317
|
+
checkErr(err);
|
|
1318
|
+
assert.deepEqual(widget, {name: name});
|
|
1319
|
+
assert.ok(memoryStoreStub.get.calledWith(key));
|
|
1320
|
+
assert.ok(memoryStoreStub.ttl.calledWith(key));
|
|
1321
|
+
assert.equal(funcCalled, true);
|
|
1322
|
+
assert.equal(ttlFuncCalled, true);
|
|
1323
|
+
assert.equal(memoryStoreStub.set.callCount, 1);
|
|
1324
|
+
done();
|
|
1325
|
+
}, 500);
|
|
1326
|
+
});
|
|
1327
|
+
});
|
|
1328
|
+
|
|
1329
|
+
it("returns value and invokes worker in background once when called multiple times", function(done) {
|
|
1330
|
+
var funcCalled = 0;
|
|
1331
|
+
var values = [];
|
|
1332
|
+
for (var i = 0; i < 2; i++) {
|
|
1333
|
+
values.push(i);
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
async.each(values, function(val, next) {
|
|
1337
|
+
cache.wrap(key, function(cb) {
|
|
1338
|
+
funcCalled += 1;
|
|
1339
|
+
var timeout = support.random.number(100);
|
|
1340
|
+
setTimeout(function() {
|
|
1341
|
+
methods.getWidget(name, function(err, result) {
|
|
1342
|
+
cb(err, result);
|
|
1343
|
+
});
|
|
1344
|
+
}, timeout);
|
|
1345
|
+
}, function(err, widget) {
|
|
1346
|
+
checkErr(err);
|
|
1347
|
+
assert.deepEqual(widget, {name: name});
|
|
1348
|
+
next(err);
|
|
1349
|
+
});
|
|
1350
|
+
}, function(err) {
|
|
1351
|
+
// Wait for just a bit, to be sure that the callback is called.
|
|
1352
|
+
setTimeout(function() {
|
|
1353
|
+
checkErr(err);
|
|
1354
|
+
assert.ok(memoryStoreStub.get.calledWith(key));
|
|
1355
|
+
assert.equal(memoryStoreStub.ttl.callCount, 1);
|
|
1356
|
+
assert.equal(funcCalled, 1);
|
|
1357
|
+
assert.equal(memoryStoreStub.set.callCount, 1);
|
|
1358
|
+
done();
|
|
1359
|
+
}, 500);
|
|
1360
|
+
});
|
|
1361
|
+
});
|
|
1362
|
+
|
|
1363
|
+
it("returns value and invokes worker in background discarding result if error", function(done) {
|
|
1364
|
+
var funcCalled = false;
|
|
1365
|
+
var fakeError = new Error(support.random.string());
|
|
1366
|
+
cache.wrap(key, function(cb) {
|
|
1367
|
+
funcCalled = true;
|
|
1368
|
+
cb(fakeError);
|
|
1369
|
+
}, function(err, widget) {
|
|
1370
|
+
// Wait for just a bit, to be sure that the callback is called.
|
|
1371
|
+
setTimeout(function() {
|
|
1372
|
+
checkErr(err);
|
|
1373
|
+
assert.deepEqual(widget, {name: name});
|
|
1374
|
+
assert.ok(memoryStoreStub.get.calledWith(key));
|
|
1375
|
+
assert.ok(memoryStoreStub.ttl.calledWith(key));
|
|
1376
|
+
assert.equal(funcCalled, true);
|
|
1377
|
+
assert.equal(memoryStoreStub.set.callCount, 0);
|
|
1378
|
+
done();
|
|
1379
|
+
}, 500);
|
|
1380
|
+
});
|
|
1381
|
+
});
|
|
1382
|
+
});
|
|
1383
|
+
});
|
|
1384
|
+
|
|
1108
1385
|
describe("when called multiple times in parallel with same key", function() {
|
|
1109
1386
|
var construct;
|
|
1110
1387
|
var constructMultiple;
|
|
@@ -1185,11 +1462,10 @@ describe("caching", function() {
|
|
|
1185
1462
|
it("should be able to chain with simple promise", function(done) {
|
|
1186
1463
|
cache.wrap('key', function() {
|
|
1187
1464
|
return 'OK';
|
|
1188
|
-
})
|
|
1189
|
-
.
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
});
|
|
1465
|
+
}).then(function(res) {
|
|
1466
|
+
assert.equal(res, 'OK');
|
|
1467
|
+
done();
|
|
1468
|
+
});
|
|
1193
1469
|
});
|
|
1194
1470
|
|
|
1195
1471
|
it("should be able to chain with cache function as a promise", function(done) {
|
|
@@ -1197,11 +1473,21 @@ describe("caching", function() {
|
|
|
1197
1473
|
return new Promise(function(resolve) {
|
|
1198
1474
|
resolve('OK');
|
|
1199
1475
|
});
|
|
1200
|
-
})
|
|
1201
|
-
.
|
|
1202
|
-
|
|
1203
|
-
|
|
1476
|
+
}).then(function(res) {
|
|
1477
|
+
assert.equal(res, 'OK');
|
|
1478
|
+
done();
|
|
1479
|
+
});
|
|
1480
|
+
});
|
|
1481
|
+
|
|
1482
|
+
it("should be able use a ttl while chaining cache function as a promise", function(done) {
|
|
1483
|
+
cache.wrap('key', function() {
|
|
1484
|
+
return new Promise(function(resolve) {
|
|
1485
|
+
resolve('OK');
|
|
1204
1486
|
});
|
|
1487
|
+
}, {ttl: 10}).then(function(res) {
|
|
1488
|
+
assert.equal(res, 'OK');
|
|
1489
|
+
done();
|
|
1490
|
+
});
|
|
1205
1491
|
});
|
|
1206
1492
|
|
|
1207
1493
|
it("should be able to catch errors in cache function as a promise", function(done) {
|
|
@@ -1209,23 +1495,20 @@ describe("caching", function() {
|
|
|
1209
1495
|
return new Promise(function(resolve, reject) {
|
|
1210
1496
|
reject('NOK');
|
|
1211
1497
|
});
|
|
1212
|
-
})
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
done();
|
|
1218
|
-
});
|
|
1498
|
+
}).then(function() {
|
|
1499
|
+
done(new Error('It should not call "then" since there is an error in the cache function!'));
|
|
1500
|
+
}).catch(function() {
|
|
1501
|
+
done();
|
|
1502
|
+
});
|
|
1219
1503
|
});
|
|
1220
1504
|
|
|
1221
1505
|
it("should be able to chain with non-cacheable value", function(done) {
|
|
1222
1506
|
cache.wrap('key', function() {
|
|
1223
1507
|
return;
|
|
1224
|
-
})
|
|
1225
|
-
.
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
});
|
|
1508
|
+
}).then(function(res) {
|
|
1509
|
+
assert.equal(res, undefined);
|
|
1510
|
+
done();
|
|
1511
|
+
});
|
|
1229
1512
|
});
|
|
1230
1513
|
});
|
|
1231
1514
|
});
|
|
@@ -1129,6 +1129,466 @@ describe("multiCaching", function() {
|
|
|
1129
1129
|
});
|
|
1130
1130
|
});
|
|
1131
1131
|
|
|
1132
|
+
describe("using a single cache store with refresh handling", function() {
|
|
1133
|
+
var memoryCache4 = caching({store: 'memory', ttl: 10, refreshThreshold: 2, promiseDependency: Promise});
|
|
1134
|
+
|
|
1135
|
+
context("using standard memory store when the result is valid", function() {
|
|
1136
|
+
beforeEach(function(done) {
|
|
1137
|
+
multiCache = multiCaching([memoryCache4]);
|
|
1138
|
+
|
|
1139
|
+
getCachedWidget(name, function(err, widget) {
|
|
1140
|
+
checkErr(err);
|
|
1141
|
+
assert.ok(widget);
|
|
1142
|
+
|
|
1143
|
+
multiCache.get(key, function(err, result) {
|
|
1144
|
+
checkErr(err);
|
|
1145
|
+
assert.ok(result);
|
|
1146
|
+
|
|
1147
|
+
sinon.spy(memoryCache4, 'checkRefreshThreshold');
|
|
1148
|
+
|
|
1149
|
+
done();
|
|
1150
|
+
});
|
|
1151
|
+
});
|
|
1152
|
+
});
|
|
1153
|
+
|
|
1154
|
+
afterEach(function(done) {
|
|
1155
|
+
memoryCache4.checkRefreshThreshold.restore();
|
|
1156
|
+
done();
|
|
1157
|
+
});
|
|
1158
|
+
|
|
1159
|
+
function getCachedWidget(name, cb) {
|
|
1160
|
+
multiCache.wrap(key, function(cacheCb) {
|
|
1161
|
+
methods.getWidget(name, cacheCb);
|
|
1162
|
+
}, cb);
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
it('returns the value without refreshing if failing during TTL check', function(done) {
|
|
1166
|
+
// NOTE: ttl function is not defined
|
|
1167
|
+
var funcCalled = false;
|
|
1168
|
+
multiCache.wrap(key, function(cb) {
|
|
1169
|
+
funcCalled = true;
|
|
1170
|
+
methods.getWidget(name, cb);
|
|
1171
|
+
}, function(err, widget) {
|
|
1172
|
+
setTimeout(function() {
|
|
1173
|
+
checkErr(err);
|
|
1174
|
+
assert.deepEqual(widget, {name: name});
|
|
1175
|
+
assert.equal(memoryCache4.checkRefreshThreshold.callCount, 1);
|
|
1176
|
+
assert.ok(!funcCalled);
|
|
1177
|
+
done();
|
|
1178
|
+
}, 500);
|
|
1179
|
+
});
|
|
1180
|
+
});
|
|
1181
|
+
});
|
|
1182
|
+
|
|
1183
|
+
context("using a tweaked memory store when the result is valid", function() {
|
|
1184
|
+
beforeEach(function(done) {
|
|
1185
|
+
multiCache = multiCaching([memoryCache4]);
|
|
1186
|
+
|
|
1187
|
+
memoryCache4.store.ttl = function(key, cb) {
|
|
1188
|
+
return cb(null, 5);
|
|
1189
|
+
};
|
|
1190
|
+
getCachedWidget(name, function(err, widget) {
|
|
1191
|
+
checkErr(err);
|
|
1192
|
+
assert.ok(widget);
|
|
1193
|
+
|
|
1194
|
+
multiCache.get(key, function(err, result) {
|
|
1195
|
+
checkErr(err);
|
|
1196
|
+
assert.ok(result);
|
|
1197
|
+
|
|
1198
|
+
sinon.spy(memoryCache4.store, 'set');
|
|
1199
|
+
sinon.spy(memoryCache4.store, 'ttl');
|
|
1200
|
+
sinon.spy(memoryCache4.store, 'get');
|
|
1201
|
+
|
|
1202
|
+
done();
|
|
1203
|
+
});
|
|
1204
|
+
});
|
|
1205
|
+
});
|
|
1206
|
+
|
|
1207
|
+
afterEach(function(done) {
|
|
1208
|
+
memoryCache4.store.get.restore();
|
|
1209
|
+
memoryCache4.store.set.restore();
|
|
1210
|
+
memoryCache4.store.ttl.restore();
|
|
1211
|
+
memoryCache4.store.ttl = undefined;
|
|
1212
|
+
done();
|
|
1213
|
+
});
|
|
1214
|
+
|
|
1215
|
+
function getCachedWidget(name, cb) {
|
|
1216
|
+
multiCache.wrap(key, function(cacheCb) {
|
|
1217
|
+
methods.getWidget(name, cacheCb);
|
|
1218
|
+
}, cb);
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
it('returns the value without refreshing', function(done) {
|
|
1222
|
+
var funcCalled = false;
|
|
1223
|
+
multiCache.wrap(key, function(cb) {
|
|
1224
|
+
funcCalled = true;
|
|
1225
|
+
methods.getWidget(name, cb);
|
|
1226
|
+
}, function(err, widget) {
|
|
1227
|
+
setTimeout(function() {
|
|
1228
|
+
checkErr(err);
|
|
1229
|
+
assert.deepEqual(widget, {name: name});
|
|
1230
|
+
assert.ok(memoryCache4.store.get.called);
|
|
1231
|
+
assert.ok(!memoryCache4.store.set.called);
|
|
1232
|
+
assert.ok(!funcCalled);
|
|
1233
|
+
done();
|
|
1234
|
+
}, 500);
|
|
1235
|
+
});
|
|
1236
|
+
});
|
|
1237
|
+
|
|
1238
|
+
it('returns the value without refreshing but checking twice', function(done) {
|
|
1239
|
+
var funcCalled = 0;
|
|
1240
|
+
var values = [];
|
|
1241
|
+
|
|
1242
|
+
for (var i = 0; i < 2; i++) {
|
|
1243
|
+
values.push(i);
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
async.eachSeries(values, function(val, next) {
|
|
1247
|
+
multiCache.wrap(key, function(cb) {
|
|
1248
|
+
funcCalled += 1;
|
|
1249
|
+
methods.getWidget(name, function(err, result) {
|
|
1250
|
+
cb(err, result);
|
|
1251
|
+
});
|
|
1252
|
+
}, function(err, widget) {
|
|
1253
|
+
checkErr(err);
|
|
1254
|
+
assert.deepEqual(widget, {name: name});
|
|
1255
|
+
next(err);
|
|
1256
|
+
});
|
|
1257
|
+
}, function(err) {
|
|
1258
|
+
setTimeout(function() {
|
|
1259
|
+
checkErr(err);
|
|
1260
|
+
assert.equal(memoryCache4.store.get.callCount, 2);
|
|
1261
|
+
assert.equal(memoryCache4.store.ttl.callCount, 2);
|
|
1262
|
+
assert.equal(memoryCache4.store.set.callCount, 0);
|
|
1263
|
+
assert.equal(funcCalled, 0);
|
|
1264
|
+
done();
|
|
1265
|
+
}, 500);
|
|
1266
|
+
});
|
|
1267
|
+
});
|
|
1268
|
+
});
|
|
1269
|
+
context("using a tweaked memory store when the result is valid but expiring", function() {
|
|
1270
|
+
beforeEach(function(done) {
|
|
1271
|
+
multiCache = multiCaching([memoryCache4]);
|
|
1272
|
+
|
|
1273
|
+
memoryCache4.store.ttl = function(key, cb) {
|
|
1274
|
+
return cb(null, 1);
|
|
1275
|
+
};
|
|
1276
|
+
getCachedWidget(name, function(err, widget) {
|
|
1277
|
+
checkErr(err);
|
|
1278
|
+
assert.ok(widget);
|
|
1279
|
+
|
|
1280
|
+
multiCache.get(key, function(err, result) {
|
|
1281
|
+
checkErr(err);
|
|
1282
|
+
assert.ok(result);
|
|
1283
|
+
|
|
1284
|
+
sinon.spy(memoryCache4.store, 'set');
|
|
1285
|
+
sinon.spy(memoryCache4.store, 'ttl');
|
|
1286
|
+
sinon.spy(memoryCache4.store, 'get');
|
|
1287
|
+
|
|
1288
|
+
done();
|
|
1289
|
+
});
|
|
1290
|
+
});
|
|
1291
|
+
});
|
|
1292
|
+
|
|
1293
|
+
afterEach(function(done) {
|
|
1294
|
+
memoryCache4.store.get.restore();
|
|
1295
|
+
memoryCache4.store.set.restore();
|
|
1296
|
+
memoryCache4.store.ttl.restore();
|
|
1297
|
+
done();
|
|
1298
|
+
});
|
|
1299
|
+
|
|
1300
|
+
function getCachedWidget(name, cb) {
|
|
1301
|
+
multiCache.wrap(key, function(cacheCb) {
|
|
1302
|
+
methods.getWidget(name, cacheCb);
|
|
1303
|
+
}, cb);
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
it('returns value and invokes worker in background', function(done) {
|
|
1307
|
+
var funcCalled = false;
|
|
1308
|
+
multiCache.wrap(key, function(cb) {
|
|
1309
|
+
funcCalled = true;
|
|
1310
|
+
methods.getWidget(name, cb);
|
|
1311
|
+
}, function(err, widget) {
|
|
1312
|
+
setTimeout(function() {
|
|
1313
|
+
checkErr(err);
|
|
1314
|
+
assert.deepEqual(widget, {name: name});
|
|
1315
|
+
assert.ok(memoryCache4.store.get.called);
|
|
1316
|
+
|
|
1317
|
+
assert.ok(funcCalled);
|
|
1318
|
+
assert.ok(memoryCache4.store.set.called);
|
|
1319
|
+
done();
|
|
1320
|
+
}, 500);
|
|
1321
|
+
});
|
|
1322
|
+
});
|
|
1323
|
+
|
|
1324
|
+
it("returns value and invokes worker in background once when called multiple times", function(done) {
|
|
1325
|
+
var funcCalled = 0;
|
|
1326
|
+
var values = [];
|
|
1327
|
+
for (var i = 0; i < 2; i++) {
|
|
1328
|
+
values.push(i);
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
async.each(values, function(val, next) {
|
|
1332
|
+
multiCache.wrap(key, function(cb) {
|
|
1333
|
+
funcCalled += 1;
|
|
1334
|
+
var timeout = support.random.number(100);
|
|
1335
|
+
setTimeout(function() {
|
|
1336
|
+
methods.getWidget(name, function(err, result) {
|
|
1337
|
+
cb(err, result);
|
|
1338
|
+
});
|
|
1339
|
+
}, timeout);
|
|
1340
|
+
}, function(err, widget) {
|
|
1341
|
+
checkErr(err);
|
|
1342
|
+
assert.deepEqual(widget, {name: name});
|
|
1343
|
+
next(err);
|
|
1344
|
+
});
|
|
1345
|
+
}, function(err) {
|
|
1346
|
+
// Wait for just a bit, to be sure that the callback is called.
|
|
1347
|
+
setTimeout(function() {
|
|
1348
|
+
checkErr(err);
|
|
1349
|
+
assert.ok(memoryCache4.store.get.calledWith(key));
|
|
1350
|
+
assert.equal(memoryCache4.store.ttl.callCount, 1);
|
|
1351
|
+
assert.equal(funcCalled, 1);
|
|
1352
|
+
assert.equal(memoryCache4.store.set.callCount, 1);
|
|
1353
|
+
done();
|
|
1354
|
+
}, 500);
|
|
1355
|
+
});
|
|
1356
|
+
});
|
|
1357
|
+
|
|
1358
|
+
it("returns value and invokes worker in background discarding result if error", function(done) {
|
|
1359
|
+
var funcCalled = false;
|
|
1360
|
+
var fakeError = new Error(support.random.string());
|
|
1361
|
+
multiCache.wrap(key, function(cb) {
|
|
1362
|
+
funcCalled = true;
|
|
1363
|
+
cb(fakeError);
|
|
1364
|
+
}, function(err, widget) {
|
|
1365
|
+
// Wait for just a bit, to be sure that the callback is called.
|
|
1366
|
+
setTimeout(function() {
|
|
1367
|
+
checkErr(err);
|
|
1368
|
+
assert.deepEqual(widget, {name: name});
|
|
1369
|
+
assert.ok(memoryCache4.store.get.calledWith(key));
|
|
1370
|
+
assert.ok(memoryCache4.store.ttl.calledWith(key));
|
|
1371
|
+
assert.ok(funcCalled);
|
|
1372
|
+
|
|
1373
|
+
assert.ok(!memoryCache4.store.set.called);
|
|
1374
|
+
done();
|
|
1375
|
+
}, 500);
|
|
1376
|
+
});
|
|
1377
|
+
});
|
|
1378
|
+
|
|
1379
|
+
it('returns value and invokes worker in background using the provided TTL function', function(done) {
|
|
1380
|
+
var funcCalled = false;
|
|
1381
|
+
var ttlFuncCalled = false;
|
|
1382
|
+
var ttlFunc = function() {
|
|
1383
|
+
ttlFuncCalled = true;
|
|
1384
|
+
return 4;
|
|
1385
|
+
};
|
|
1386
|
+
multiCache.wrap(key, function(cb) {
|
|
1387
|
+
funcCalled = true;
|
|
1388
|
+
methods.getWidget(name, cb);
|
|
1389
|
+
}, {ttl: ttlFunc}, function(err, widget) {
|
|
1390
|
+
setTimeout(function() {
|
|
1391
|
+
checkErr(err);
|
|
1392
|
+
assert.deepEqual(widget, {name: name});
|
|
1393
|
+
assert.ok(memoryCache4.store.get.called);
|
|
1394
|
+
|
|
1395
|
+
assert.ok(funcCalled);
|
|
1396
|
+
assert.ok(ttlFuncCalled);
|
|
1397
|
+
assert.ok(memoryCache4.store.set.called);
|
|
1398
|
+
done();
|
|
1399
|
+
}, 500);
|
|
1400
|
+
});
|
|
1401
|
+
});
|
|
1402
|
+
});
|
|
1403
|
+
});
|
|
1404
|
+
|
|
1405
|
+
describe("using two cache stores with refresh handling on the latter", function() {
|
|
1406
|
+
var memoryCache4 = caching({store: 'memory', ttl: 10, refreshThreshold: 2, promiseDependency: Promise});
|
|
1407
|
+
|
|
1408
|
+
context("when the result is only in [2]", function() {
|
|
1409
|
+
beforeEach(function(done) {
|
|
1410
|
+
multiCache = multiCaching([memoryCache3, memoryCache4]);
|
|
1411
|
+
|
|
1412
|
+
memoryCache4.store.ttl = function(key, cb) {
|
|
1413
|
+
return cb(null, 5);
|
|
1414
|
+
};
|
|
1415
|
+
getCachedWidget(name, function(err, widget) {
|
|
1416
|
+
checkErr(err);
|
|
1417
|
+
assert.ok(widget);
|
|
1418
|
+
|
|
1419
|
+
multiCache.get(key, function(err, result) {
|
|
1420
|
+
checkErr(err);
|
|
1421
|
+
assert.ok(result);
|
|
1422
|
+
|
|
1423
|
+
memoryCache3.del(key);
|
|
1424
|
+
|
|
1425
|
+
sinon.spy(memoryCache3.store, 'set');
|
|
1426
|
+
|
|
1427
|
+
sinon.spy(memoryCache4.store, 'set');
|
|
1428
|
+
sinon.spy(memoryCache4.store, 'ttl');
|
|
1429
|
+
sinon.spy(memoryCache4.store, 'get');
|
|
1430
|
+
|
|
1431
|
+
done();
|
|
1432
|
+
});
|
|
1433
|
+
});
|
|
1434
|
+
});
|
|
1435
|
+
|
|
1436
|
+
afterEach(function(done) {
|
|
1437
|
+
memoryCache3.store.set.restore();
|
|
1438
|
+
|
|
1439
|
+
memoryCache4.store.get.restore();
|
|
1440
|
+
memoryCache4.store.set.restore();
|
|
1441
|
+
memoryCache4.store.ttl.restore();
|
|
1442
|
+
memoryCache4.store.ttl = undefined;
|
|
1443
|
+
done();
|
|
1444
|
+
});
|
|
1445
|
+
|
|
1446
|
+
function getCachedWidget(name, cb) {
|
|
1447
|
+
multiCache.wrap(key, function(cacheCb) {
|
|
1448
|
+
methods.getWidget(name, cacheCb);
|
|
1449
|
+
}, cb);
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
it('returns the value without refreshing and setting on [1]', function(done) {
|
|
1453
|
+
var funcCalled = false;
|
|
1454
|
+
multiCache.wrap(key, function(cb) {
|
|
1455
|
+
funcCalled = true;
|
|
1456
|
+
methods.getWidget(name, cb);
|
|
1457
|
+
}, function(err, widget) {
|
|
1458
|
+
setTimeout(function() {
|
|
1459
|
+
checkErr(err);
|
|
1460
|
+
assert.deepEqual(widget, {name: name});
|
|
1461
|
+
assert.equal(memoryCache3.store.set.callCount, 1);
|
|
1462
|
+
assert.equal(memoryCache4.store.get.callCount, 1);
|
|
1463
|
+
assert.equal(memoryCache4.store.set.callCount, 0);
|
|
1464
|
+
assert.equal(funcCalled, false);
|
|
1465
|
+
done();
|
|
1466
|
+
}, 500);
|
|
1467
|
+
});
|
|
1468
|
+
});
|
|
1469
|
+
});
|
|
1470
|
+
context("when the result is only in [2] but expiring", function() {
|
|
1471
|
+
beforeEach(function(done) {
|
|
1472
|
+
multiCache = multiCaching([memoryCache3, memoryCache4]);
|
|
1473
|
+
|
|
1474
|
+
memoryCache4.store.ttl = function(key, cb) {
|
|
1475
|
+
return cb(null, 1);
|
|
1476
|
+
};
|
|
1477
|
+
getCachedWidget(name, function(err, widget) {
|
|
1478
|
+
checkErr(err);
|
|
1479
|
+
assert.ok(widget);
|
|
1480
|
+
|
|
1481
|
+
multiCache.get(key, function(err, result) {
|
|
1482
|
+
checkErr(err);
|
|
1483
|
+
assert.ok(result);
|
|
1484
|
+
|
|
1485
|
+
memoryCache3.del(key);
|
|
1486
|
+
|
|
1487
|
+
sinon.spy(memoryCache3.store, 'set');
|
|
1488
|
+
|
|
1489
|
+
sinon.spy(memoryCache4.store, 'set');
|
|
1490
|
+
sinon.spy(memoryCache4.store, 'ttl');
|
|
1491
|
+
sinon.spy(memoryCache4.store, 'get');
|
|
1492
|
+
|
|
1493
|
+
done();
|
|
1494
|
+
});
|
|
1495
|
+
});
|
|
1496
|
+
});
|
|
1497
|
+
|
|
1498
|
+
afterEach(function(done) {
|
|
1499
|
+
memoryCache3.store.set.restore();
|
|
1500
|
+
|
|
1501
|
+
memoryCache4.store.get.restore();
|
|
1502
|
+
memoryCache4.store.set.restore();
|
|
1503
|
+
memoryCache4.store.ttl.restore();
|
|
1504
|
+
done();
|
|
1505
|
+
});
|
|
1506
|
+
|
|
1507
|
+
function getCachedWidget(name, cb) {
|
|
1508
|
+
multiCache.wrap(key, function(cacheCb) {
|
|
1509
|
+
methods.getWidget(name, cacheCb);
|
|
1510
|
+
}, cb);
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
it('returns value and invokes worker in background, resetting all caches', function(done) {
|
|
1514
|
+
var funcCalled = false;
|
|
1515
|
+
multiCache.wrap(key, function(cb) {
|
|
1516
|
+
funcCalled = true;
|
|
1517
|
+
methods.getWidget(name, cb);
|
|
1518
|
+
}, function(err, widget) {
|
|
1519
|
+
setTimeout(function() {
|
|
1520
|
+
checkErr(err);
|
|
1521
|
+
assert.deepEqual(widget, {name: name});
|
|
1522
|
+
assert.equal(memoryCache4.store.get.callCount, 1);
|
|
1523
|
+
|
|
1524
|
+
assert.equal(funcCalled, true);
|
|
1525
|
+
assert.equal(memoryCache3.store.set.callCount, 2); // one call for the partial set
|
|
1526
|
+
assert.equal(memoryCache4.store.set.callCount, 1);
|
|
1527
|
+
done();
|
|
1528
|
+
}, 500);
|
|
1529
|
+
});
|
|
1530
|
+
});
|
|
1531
|
+
|
|
1532
|
+
it("returns value and invokes worker in background once when called multiple times", function(done) {
|
|
1533
|
+
var funcCalled = 0;
|
|
1534
|
+
var values = [];
|
|
1535
|
+
for (var i = 0; i < 2; i++) {
|
|
1536
|
+
values.push(i);
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
async.each(values, function(val, next) {
|
|
1540
|
+
multiCache.wrap(key, function(cb) {
|
|
1541
|
+
funcCalled += 1;
|
|
1542
|
+
var timeout = support.random.number(100);
|
|
1543
|
+
setTimeout(function() {
|
|
1544
|
+
methods.getWidget(name, function(err, result) {
|
|
1545
|
+
cb(err, result);
|
|
1546
|
+
});
|
|
1547
|
+
}, timeout);
|
|
1548
|
+
}, function(err, widget) {
|
|
1549
|
+
checkErr(err);
|
|
1550
|
+
assert.deepEqual(widget, {name: name});
|
|
1551
|
+
next(err);
|
|
1552
|
+
});
|
|
1553
|
+
}, function(err) {
|
|
1554
|
+
// Wait for just a bit, to be sure that the callback is called.
|
|
1555
|
+
setTimeout(function() {
|
|
1556
|
+
checkErr(err);
|
|
1557
|
+
assert.equal(memoryCache3.store.set.callCount, 2); // one call for the partial set
|
|
1558
|
+
|
|
1559
|
+
assert.ok(memoryCache4.store.get.calledWith(key));
|
|
1560
|
+
assert.equal(memoryCache4.store.ttl.callCount, 1);
|
|
1561
|
+
assert.equal(funcCalled, 1);
|
|
1562
|
+
assert.equal(memoryCache4.store.set.callCount, 1);
|
|
1563
|
+
done();
|
|
1564
|
+
}, 500);
|
|
1565
|
+
});
|
|
1566
|
+
});
|
|
1567
|
+
|
|
1568
|
+
it("returns value and invokes worker in background discarding result if error", function(done) {
|
|
1569
|
+
var funcCalled = false;
|
|
1570
|
+
var fakeError = new Error(support.random.string());
|
|
1571
|
+
multiCache.wrap(key, function(cb) {
|
|
1572
|
+
funcCalled = true;
|
|
1573
|
+
cb(fakeError);
|
|
1574
|
+
}, function(err, widget) {
|
|
1575
|
+
// Wait for just a bit, to be sure that the callback is called.
|
|
1576
|
+
setTimeout(function() {
|
|
1577
|
+
checkErr(err);
|
|
1578
|
+
assert.deepEqual(widget, {name: name});
|
|
1579
|
+
assert.equal(memoryCache3.store.set.callCount, 1);
|
|
1580
|
+
|
|
1581
|
+
assert.ok(memoryCache4.store.get.calledWith(key));
|
|
1582
|
+
assert.equal(memoryCache4.store.ttl.callCount, 1);
|
|
1583
|
+
assert.equal(funcCalled, true);
|
|
1584
|
+
|
|
1585
|
+
assert.equal(memoryCache4.store.set.callCount, 0);
|
|
1586
|
+
done();
|
|
1587
|
+
}, 500);
|
|
1588
|
+
});
|
|
1589
|
+
});
|
|
1590
|
+
});
|
|
1591
|
+
});
|
|
1132
1592
|
describe("using two cache stores", function() {
|
|
1133
1593
|
beforeEach(function() {
|
|
1134
1594
|
multiCache = multiCaching([memoryCache, memoryCache3]);
|
|
@@ -49,7 +49,7 @@ describe("memory store", function() {
|
|
|
49
49
|
context("when cache misses", function() {
|
|
50
50
|
function getCachedObject(name, cb) {
|
|
51
51
|
cache.wrap(key, function(cacheCb) {
|
|
52
|
-
cacheCb(null, {foo: 'bar'});
|
|
52
|
+
cacheCb(null, {foo: 'bar', arr: [1, 2, 3]});
|
|
53
53
|
}, opts, cb);
|
|
54
54
|
}
|
|
55
55
|
|
|
@@ -110,6 +110,7 @@ describe("memory store", function() {
|
|
|
110
110
|
getCachedObject('foo', function(err, result) {
|
|
111
111
|
checkErr(err);
|
|
112
112
|
assert.equal(result.foo, 'bar');
|
|
113
|
+
assert.deepEqual(result.arr, [1, 2, 3]);
|
|
113
114
|
done();
|
|
114
115
|
});
|
|
115
116
|
});
|