cache-manager 3.2.0 → 3.4.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/History.md CHANGED
@@ -1,3 +1,15 @@
1
+ - 3.4.1 2021-03-05
2
+ - Update lodash to fix vulnerability (#152).
3
+
4
+ - 3.4.0 2020-04-30
5
+ - Update dependencies (#145). - @orgads
6
+
7
+ - 3.3.0 2020-04-30
8
+ - Use full lodash lib instead of lodash.clonedeep npm (#141)
9
+
10
+ - 3.2.1 2020-03-20
11
+ - Background refresh fixes (#139). -@marcoreni
12
+
1
13
  - 3.2.0 2020-03-13
2
14
  - Background refresh of expiring entries (#138). - @marcoreni
3
15
 
package/lib/caching.js CHANGED
@@ -67,7 +67,7 @@ var caching = function(args) {
67
67
  }
68
68
 
69
69
  function handleBackgroundRefresh(key, work, options) {
70
- if (!backgroundQueue.has(key)) {
70
+ if (self.refreshThreshold && !backgroundQueue.has(key)) {
71
71
  backgroundQueue.add(key);
72
72
  self.checkRefreshThreshold(key, function(err, isExpiring) {
73
73
  if (err) {
@@ -87,6 +87,8 @@ var caching = function(args) {
87
87
  backgroundQueue.delete(key);
88
88
  });
89
89
  });
90
+ } else {
91
+ backgroundQueue.delete(key);
90
92
  }
91
93
  });
92
94
  }
@@ -324,8 +324,8 @@ var multiCaching = function(caches, options) {
324
324
  });
325
325
  }
326
326
 
327
- function handleBackgroundRefresh(caches, index, key, work) {
328
- if (!backgroundQueue.has(key)) {
327
+ function handleBackgroundRefresh(caches, index, key, work, options) {
328
+ if (caches[index].refreshThreshold && !backgroundQueue.has(key)) {
329
329
  backgroundQueue.add(key);
330
330
  caches[index].checkRefreshThreshold(key, function(err, isExpiring) {
331
331
  if (err) {
@@ -338,14 +338,13 @@ var multiCaching = function(caches, options) {
338
338
  backgroundQueue.delete(key);
339
339
  return;
340
340
  }
341
- if (options && typeof options.ttl === 'function') {
342
- options.ttl = options.ttl(workData);
343
- }
344
341
  var args = [caches, key, workData, options, function() {
345
342
  backgroundQueue.delete(key);
346
343
  }];
347
344
  setInMultipleCaches.apply(null, args);
348
345
  });
346
+ } else {
347
+ backgroundQueue.delete(key);
349
348
  }
350
349
  });
351
350
  }
@@ -401,7 +400,7 @@ var multiCaching = function(caches, options) {
401
400
  if (err) {
402
401
  return callbackFiller.fill(key, err);
403
402
  } else if (self._isCacheableValue(result)) {
404
- handleBackgroundRefresh(caches, index, key, work);
403
+ handleBackgroundRefresh(caches, index, key, work, options);
405
404
  var cachesToUpdate = caches.slice(0, index);
406
405
  var args = [cachesToUpdate, key, result, options, function(err) {
407
406
  callbackFiller.fill(key, err, result);
@@ -1,6 +1,6 @@
1
1
  /*eslint no-unused-vars:0*/
2
2
  var Lru = require("lru-cache");
3
- var cloneDeep = require('lodash.clonedeep');
3
+ var cloneDeep = require('lodash/cloneDeep');
4
4
  var utils = require('../utils');
5
5
  var isObject = utils.isObject;
6
6
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cache-manager",
3
- "version": "3.2.0",
3
+ "version": "3.4.1",
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
- "lodash.clonedeep": "4.5.0",
24
- "async": "1.5.2",
25
- "lru-cache": "4.0.0"
23
+ "async": "3.2.0",
24
+ "lodash": "^4.17.21",
25
+ "lru-cache": "6.0.0"
26
26
  },
27
27
  "devDependencies": {
28
- "coveralls": "3.0.9",
29
- "es6-promise": "^3.0.2",
30
- "eslint": "5.16.0",
31
- "jsdoc": "3.5.5",
32
- "mocha": "7.0.1",
33
- "nyc": "14.1.1",
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": "1.17.3"
35
+ "sinon": "9.0.3"
36
36
  }
37
37
  }
@@ -397,8 +397,8 @@ describe("caching", function() {
397
397
 
398
398
  cache = caching({store: 'memory'});
399
399
 
400
- function isDone() {
401
- return processed === keyCount;
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
- return processed === keyCount;
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', function(key, options, cb) {
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', function(key, options, cb) {
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', function(key, val, ttl, cb) {
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', function(name, cb) {
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', function() {
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', function() {
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', function() {
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,7 +1105,7 @@ describe("caching", function() {
1105
1105
  });
1106
1106
  });
1107
1107
 
1108
- describe("using tweaked memory (lru-cache) store with a refreshThreshold", function() {
1108
+ describe("using memory (lru-cache) store with a refreshThreshold", function() {
1109
1109
  var memoryStoreStub;
1110
1110
  var opts;
1111
1111
 
@@ -1124,13 +1124,52 @@ describe("caching", function() {
1124
1124
  memoryStore.create.restore();
1125
1125
  });
1126
1126
 
1127
- context("when result is already cached", function() {
1128
- function getCachedWidget(name, cb) {
1129
- cache.wrap(key, function(cacheCb) {
1130
- methods.getWidget(name, cacheCb);
1131
- }, opts, cb);
1132
- }
1127
+ function getCachedWidget(name, cb) {
1128
+ cache.wrap(key, function(cacheCb) {
1129
+ methods.getWidget(name, cacheCb);
1130
+ }, opts, cb);
1131
+ }
1133
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() {
1134
1173
  beforeEach(function(done) {
1135
1174
  memoryStoreStub.ttl = function(key, cb) {
1136
1175
  return cb(null, 5);
@@ -1175,15 +1214,41 @@ describe("caching", function() {
1175
1214
  done();
1176
1215
  });
1177
1216
  });
1178
- });
1179
1217
 
1180
- context("when result is already cached but expiring", function() {
1181
- function getCachedWidget(name, cb) {
1182
- cache.wrap(key, function(cacheCb) {
1183
- methods.getWidget(name, cacheCb);
1184
- }, opts, cb);
1185
- }
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
+ });
1186
1250
 
1251
+ context("using tweaked memorystore, when result is already cached but expiring", function() {
1187
1252
  beforeEach(function(done) {
1188
1253
  memoryStoreStub.ttl = function(key, cb) {
1189
1254
  return cb(null, 1);
@@ -1233,6 +1298,34 @@ describe("caching", function() {
1233
1298
  });
1234
1299
  });
1235
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
+
1236
1329
  it("returns value and invokes worker in background once when called multiple times", function(done) {
1237
1330
  var funcCalled = 0;
1238
1331
  var values = [];
@@ -1369,11 +1462,10 @@ describe("caching", function() {
1369
1462
  it("should be able to chain with simple promise", function(done) {
1370
1463
  cache.wrap('key', function() {
1371
1464
  return 'OK';
1372
- })
1373
- .then(function(res) {
1374
- assert.equal(res, 'OK');
1375
- done();
1376
- });
1465
+ }).then(function(res) {
1466
+ assert.equal(res, 'OK');
1467
+ done();
1468
+ });
1377
1469
  });
1378
1470
 
1379
1471
  it("should be able to chain with cache function as a promise", function(done) {
@@ -1381,11 +1473,21 @@ describe("caching", function() {
1381
1473
  return new Promise(function(resolve) {
1382
1474
  resolve('OK');
1383
1475
  });
1384
- })
1385
- .then(function(res) {
1386
- assert.equal(res, 'OK');
1387
- done();
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');
1388
1486
  });
1487
+ }, {ttl: 10}).then(function(res) {
1488
+ assert.equal(res, 'OK');
1489
+ done();
1490
+ });
1389
1491
  });
1390
1492
 
1391
1493
  it("should be able to catch errors in cache function as a promise", function(done) {
@@ -1393,23 +1495,20 @@ describe("caching", function() {
1393
1495
  return new Promise(function(resolve, reject) {
1394
1496
  reject('NOK');
1395
1497
  });
1396
- })
1397
- .then(function() {
1398
- done(new Error('It should not call then since there is an error in the cache function!'));
1399
- })
1400
- .catch(function() {
1401
- done();
1402
- });
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
+ });
1403
1503
  });
1404
1504
 
1405
1505
  it("should be able to chain with non-cacheable value", function(done) {
1406
1506
  cache.wrap('key', function() {
1407
1507
  return;
1408
- })
1409
- .then(function(res) {
1410
- assert.equal(res, undefined);
1411
- done();
1412
- });
1508
+ }).then(function(res) {
1509
+ assert.equal(res, undefined);
1510
+ done();
1511
+ });
1413
1512
  });
1414
1513
  });
1415
1514
  });
@@ -1132,7 +1132,55 @@ describe("multiCaching", function() {
1132
1132
  describe("using a single cache store with refresh handling", function() {
1133
1133
  var memoryCache4 = caching({store: 'memory', ttl: 10, refreshThreshold: 2, promiseDependency: Promise});
1134
1134
 
1135
- context("when the result is valid", function() {
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() {
1136
1184
  beforeEach(function(done) {
1137
1185
  multiCache = multiCaching([memoryCache4]);
1138
1186
 
@@ -1186,8 +1234,39 @@ describe("multiCaching", function() {
1186
1234
  }, 500);
1187
1235
  });
1188
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
+ });
1189
1268
  });
1190
- context("when the result is valid but expiring", function() {
1269
+ context("using a tweaked memory store when the result is valid but expiring", function() {
1191
1270
  beforeEach(function(done) {
1192
1271
  multiCache = multiCaching([memoryCache4]);
1193
1272
 
@@ -1296,6 +1375,30 @@ describe("multiCaching", function() {
1296
1375
  }, 500);
1297
1376
  });
1298
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
+ });
1299
1402
  });
1300
1403
  });
1301
1404
 
@@ -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
  });