cache-manager 2.9.0 → 2.10.2

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/.travis.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  language: node_js
2
2
  node_js:
3
- - '0.12'
4
3
  - '5.5'
4
+ - '10'
5
5
  script: 'make test-travis'
6
6
  branches:
7
7
  only:
package/History.md CHANGED
@@ -1,5 +1,17 @@
1
+ - 2.10.2 2020-01-30
2
+ - Disallow mutation of values in memory store (#103).
3
+
4
+ - 2.10.1 2019-11-06
5
+ - Add .js to module path to fix compilers (#131). -@imjohnbo
6
+
7
+ - 2.10.0 2019-07-11
8
+ - Add development store "none" (#129). -@R3VoLuT1OneR
9
+
10
+ - 2.9.1 2019-05-28
11
+ - Bind isCacheableValue to its underlying store (#126). - @arash16
12
+
1
13
  - 2.9.0 2018-03-26
2
- - Fix store's this context for isCacheableValue method (#119). -@alexandrusavin
14
+ - Fix store's this context for isCacheableValue method (#119). - @alexandrusavin
3
15
 
4
16
  - 2.8.0 2018-02-28
5
17
  - Added link to node-cache-manager-fs-hash - @rolandstarke
package/Makefile CHANGED
@@ -25,7 +25,7 @@ lint:
25
25
  ./node_modules/.bin/jscs ./test && \
26
26
  ./node_modules/.bin/jscs ./examples && \
27
27
  ./node_modules/.bin/jshint ./lib --config $(BASE)/.jshintrc && \
28
- ./node_modules/.bin/jshint ./test --config $(BASE)/.jshintrc
28
+ ./node_modules/.bin/jshint ./test --config $(BASE)/.jshintrc && \
29
29
  ./node_modules/.bin/jshint ./examples --config $(BASE)/.jshintrc
30
30
 
31
31
  docs:
package/README.md CHANGED
@@ -184,7 +184,20 @@ memoryCache.wrap(key, function(cb) {
184
184
  }
185
185
  ```
186
186
 
187
- You can get several keys at once. E.g.
187
+ You can get several keys at once. Note that this will return whatever records it
188
+ finds in the cache and it is up to the user to check the results against the
189
+ supplied keys and make any calls to the underlying data store to fill in
190
+ missing records. In practice, this should not be much of a concern if you are
191
+ only using the `wrap` function to set these records in cache.
192
+
193
+ Side note: Ideally the `wrap` function would get what it can from the cache and fill in
194
+ the missing records from the data store, but I can't think of a way to do this
195
+ that is generic to all situations. Another option is to only return the data
196
+ from the cache if all records are found, but this woul break multi-caching.
197
+
198
+ See unit tests in `caching.unit.js` for more information.
199
+
200
+ Example:
188
201
 
189
202
  ```js
190
203
 
@@ -411,6 +424,9 @@ var multiCache = cacheManager.multiCaching([memoryCache, someOtherCache], {
411
424
 
412
425
  ```
413
426
 
427
+ ### Development environment
428
+ You may disable real caching but still get all the callback functionality working by setting `none` store.
429
+
414
430
  ## Docs
415
431
 
416
432
  To generate JSDOC 3 documentation:
package/lib/caching.js CHANGED
@@ -25,7 +25,7 @@ var caching = function(args) {
25
25
  }
26
26
  } else {
27
27
  var storeName = args.store || 'memory';
28
- self.store = require('./stores/' + storeName).create(args);
28
+ self.store = require('./stores/' + storeName + '.js').create(args);
29
29
  }
30
30
 
31
31
  // do we handle a cache error the same as a cache miss?
@@ -185,16 +185,16 @@ var caching = function(args) {
185
185
  * are valid. If one or more of the values is not cacheable
186
186
  * the cache result is not valid.
187
187
  */
188
- var cacheOK = Array.isArray(result) && result.filter(function(_result) {
188
+ var cacheOk = Array.isArray(result) && result.filter(function(_result) {
189
189
  return self._isCacheableValue(_result);
190
190
  }).length === result.length;
191
191
 
192
- if (cacheOK) {
192
+ if (cacheOk) {
193
193
  return callbackFiller.fill(combinedKey, null, result);
194
194
  }
195
195
 
196
196
  return work(function(err, data) {
197
- if (err) {
197
+ if (err || !data) {
198
198
  return done(err);
199
199
  }
200
200
 
@@ -45,7 +45,7 @@ var multiCaching = function(caches, options) {
45
45
  */
46
46
  function getIsCacheableValueFunction(cache) {
47
47
  if (cache.store && typeof cache.store.isCacheableValue === 'function') {
48
- return cache.store.isCacheableValue;
48
+ return cache.store.isCacheableValue.bind(cache.store);
49
49
  } else {
50
50
  return self._isCacheableValue;
51
51
  }
@@ -1,4 +1,5 @@
1
1
  var Lru = require("lru-cache");
2
+ var cloneDeep = require('lodash.clonedeep');
2
3
  var utils = require('../utils');
3
4
  var isObject = utils.isObject;
4
5
 
@@ -31,6 +32,8 @@ var memoryStore = function(args) {
31
32
  };
32
33
 
33
34
  self.set = function(key, value, options, cb) {
35
+ value = cloneDeep(value);
36
+
34
37
  if (typeof options === 'function') {
35
38
  cb = options;
36
39
  options = {};
@@ -0,0 +1,142 @@
1
+ var utils = require('../utils');
2
+ var isObject = utils.isObject;
3
+
4
+ /**
5
+ * Store that do nothing.
6
+ * Can be used for development environment.
7
+ */
8
+ var noneStore = function(args) {
9
+ args = args || {};
10
+ var Promise = args.promiseDependency || global.Promise;
11
+
12
+ var self = {};
13
+ self.name = 'none';
14
+ self.usePromises = (typeof Promise === 'undefined' || args.noPromises) ? false : true;
15
+
16
+ self.set = function(key, value, options, cb) {
17
+ if (typeof options === 'function') {
18
+ cb = options;
19
+ options = {};
20
+ }
21
+
22
+ if (cb) {
23
+ process.nextTick(cb.bind(null, null));
24
+ } else if (self.usePromises) {
25
+ return Promise.resolve(value);
26
+ }
27
+ };
28
+
29
+ self.mset = function() {
30
+ var args = Array.prototype.slice.apply(arguments);
31
+ var cb;
32
+ var options = {};
33
+
34
+ if (typeof args[args.length - 1] === 'function') {
35
+ cb = args.pop();
36
+ }
37
+
38
+ if (args.length % 2 > 0 && isObject(args[args.length - 1])) {
39
+ options = args.pop();
40
+ }
41
+
42
+ if (cb) {
43
+ process.nextTick(cb.bind(null, null));
44
+ } else if (self.usePromises) {
45
+ var values = [];
46
+ for (var i = 0; i < args.length; i += 2) {
47
+ values.push(args[i + 1]);
48
+ }
49
+ return Promise.resolve(values);
50
+ }
51
+ };
52
+
53
+ self.get = function(key, options, cb) {
54
+ var value;
55
+ if (typeof options === 'function') {
56
+ cb = options;
57
+ }
58
+
59
+ if (cb) {
60
+ process.nextTick(cb.bind(null, null, value));
61
+ } else if (self.usePromises) {
62
+ return Promise.resolve(value);
63
+ } else {
64
+ return value;
65
+ }
66
+ };
67
+
68
+ self.mget = function() {
69
+ var args = Array.prototype.slice.apply(arguments);
70
+ var cb;
71
+ var options = {};
72
+
73
+ if (typeof args[args.length - 1] === 'function') {
74
+ cb = args.pop();
75
+ }
76
+
77
+ if (isObject(args[args.length - 1])) {
78
+ options = args.pop();
79
+ }
80
+
81
+ var values = args.map(function() {
82
+ return;
83
+ });
84
+
85
+ if (cb) {
86
+ process.nextTick(cb.bind(null, null, values));
87
+ } else if (self.usePromises) {
88
+ return Promise.resolve(values);
89
+ } else {
90
+ return values;
91
+ }
92
+ };
93
+
94
+ self.del = function() {
95
+ var args = Array.prototype.slice.apply(arguments);
96
+ var cb;
97
+ var options = {};
98
+
99
+ if (typeof args[args.length - 1] === 'function') {
100
+ cb = args.pop();
101
+ }
102
+
103
+ if (isObject(args[args.length - 1])) {
104
+ options = args.pop();
105
+ }
106
+
107
+ if (cb) {
108
+ process.nextTick(cb.bind(null, null));
109
+ } else if (self.usePromises) {
110
+ return Promise.resolve();
111
+ }
112
+ };
113
+
114
+ self.reset = function(cb) {
115
+ if (cb) {
116
+ process.nextTick(cb.bind(null, null));
117
+ } else if (self.usePromises) {
118
+ return Promise.resolve();
119
+ }
120
+ };
121
+
122
+ self.keys = function(cb) {
123
+ var keys = [];
124
+ if (cb) {
125
+ process.nextTick(cb.bind(null, null, keys));
126
+ } else if (self.usePromises) {
127
+ return Promise.resolve(keys);
128
+ } else {
129
+ return keys;
130
+ }
131
+ };
132
+
133
+ return self;
134
+ };
135
+
136
+ var methods = {
137
+ create: function(args) {
138
+ return noneStore(args);
139
+ }
140
+ };
141
+
142
+ module.exports = methods;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cache-manager",
3
- "version": "2.9.0",
3
+ "version": "2.10.2",
4
4
  "description": "Cache module for Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,6 +21,7 @@
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
23
  "async": "1.5.2",
24
+ "lodash.clonedeep": "4.5.0",
24
25
  "lru-cache": "4.0.0"
25
26
  },
26
27
  "devDependencies": {
@@ -906,8 +906,8 @@ describe("caching", function() {
906
906
  var name2;
907
907
 
908
908
  beforeEach(function() {
909
- key2 = support.random.string(20);
910
- name2 = support.random.string();
909
+ key2 = '2-' + support.random.string(20);
910
+ name2 = '2-' + support.random.string();
911
911
  sinon.spy(memoryStoreStub, 'mset');
912
912
  });
913
913
 
@@ -915,6 +915,30 @@ describe("caching", function() {
915
915
  memoryStoreStub.mset.restore();
916
916
  });
917
917
 
918
+ context("when no keys are already cached", function() {
919
+ it("retrieves data from data store and caches results via mset", function(done) {
920
+ var funcCalled = false;
921
+ sinon.stub(memoryStoreStub, 'mget').yields();
922
+
923
+ cache.wrap(key, key2, function(cb) {
924
+ funcCalled = true;
925
+ cb(null, [{name: name}, {name: name2}]);
926
+ }, function(err) {
927
+ checkErr(err);
928
+ assert.ok(memoryStoreStub.mget.calledWith(key, key2));
929
+ assert.ok(funcCalled);
930
+ assert.ok(memoryStoreStub.mset.called);
931
+ var callArgs = memoryStoreStub.mset.args[0];
932
+ assert.equal(callArgs[0], key);
933
+ assert.deepEqual(callArgs[1], {name: name});
934
+ assert.equal(callArgs[2], key2);
935
+ assert.deepEqual(callArgs[3], {name: name2});
936
+ memoryStoreStub.mget.restore();
937
+ done();
938
+ });
939
+ });
940
+ });
941
+
918
942
  context("when result is already cached", function() {
919
943
  it("retrieves data from cache", function(done) {
920
944
  var funcCalled = false;
@@ -939,6 +963,31 @@ describe("caching", function() {
939
963
  });
940
964
  });
941
965
 
966
+ context("when some keys are already cached but others are not", function() {
967
+ // This means the user must manually call the data store to get any
968
+ // values that are not in cache, which is not ideal.
969
+ it("responds with only the data that exists in the cache", function(done) {
970
+ var key3 = support.random.string();
971
+ var name3 = support.random.string();
972
+ var funcCalled = false;
973
+
974
+ sinon.stub(memoryStoreStub, 'mget').yields(null, [{name: name}, {name: name3}]);
975
+
976
+ cache.wrap(key, key2, key3, function(cb) {
977
+ funcCalled = true;
978
+ methods.getMultiWidget([name2], cb);
979
+ }, function(err, widgets) {
980
+ checkErr(err);
981
+ assert.deepEqual(widgets[0], {name: name});
982
+ assert.deepEqual(widgets[1], {name: name3});
983
+ assert.ok(memoryStoreStub.mget.calledWith(key, key2));
984
+ assert.ok(!funcCalled);
985
+ memoryStoreStub.mget.restore();
986
+ done();
987
+ });
988
+ });
989
+ });
990
+
942
991
  it("when a ttl is passed in", function(done) {
943
992
  cache.wrap(key, key2, function(cb) {
944
993
  methods.getMultiWidget([name, name2], cb);
@@ -96,15 +96,15 @@ describe("multiCaching", function() {
96
96
 
97
97
  memoryCache.get(key, function(err, result) {
98
98
  checkErr(err);
99
- assert.equal(result, value);
99
+ assert.deepEqual(result, value);
100
100
 
101
101
  memoryCache2.get(key, function(err, result) {
102
102
  checkErr(err);
103
- assert.equal(result, value);
103
+ assert.deepEqual(result, value);
104
104
 
105
105
  memoryCache3.get(key, function(err, result) {
106
106
  checkErr(err);
107
- assert.equal(result, value);
107
+ assert.deepEqual(result, value);
108
108
  done();
109
109
  });
110
110
  });
@@ -1,5 +1,7 @@
1
1
  var assert = require('assert');
2
2
  var support = require('../support');
3
+ var checkErr = support.checkErr;
4
+ var caching = require('../../index').caching;
3
5
  var memoryStore = require('../../lib/stores/memory');
4
6
 
5
7
  describe("memory store", function() {
@@ -34,4 +36,96 @@ describe("memory store", function() {
34
36
  }, 10);
35
37
  });
36
38
  });
39
+
40
+ describe("when used with wrap() function", function() {
41
+ var cache;
42
+ var key;
43
+ var opts = {};
44
+
45
+ beforeEach(function() {
46
+ key = support.random.string(20);
47
+ });
48
+
49
+ context("when cache misses", function() {
50
+ function getCachedObject(name, cb) {
51
+ cache.wrap(key, function(cacheCb) {
52
+ cacheCb(null, {foo: 'bar'});
53
+ }, opts, cb);
54
+ }
55
+
56
+ function getCachedString(name, cb) {
57
+ cache.wrap(key, function(cacheCb) {
58
+ cacheCb(null, 'bar');
59
+ }, opts, cb);
60
+ }
61
+
62
+ function getCachedArray(name, cb) {
63
+ cache.wrap(key, function(cacheCb) {
64
+ cacheCb(null, [1, 2, 3]);
65
+ }, opts, cb);
66
+ }
67
+
68
+ function getCachedNumber(name, cb) {
69
+ cache.wrap(key, function(cacheCb) {
70
+ cacheCb(null, 34);
71
+ }, opts, cb);
72
+ }
73
+
74
+ beforeEach(function() {
75
+ cache = caching({store: 'memory', safeClone: true, ttl: opts.ttl, ignoreCacheErrors: false});
76
+ });
77
+
78
+ it("does not allow mutation of objects", function(done) {
79
+ getCachedObject('foo', function(err, result) {
80
+ checkErr(err);
81
+ result.foo = 'buzz';
82
+
83
+ getCachedObject('foo', function(err, result) {
84
+ checkErr(err);
85
+ assert.equal(result.foo, 'bar');
86
+ done();
87
+ });
88
+ });
89
+ });
90
+
91
+ it("does not allow mutation of arrays", function(done) {
92
+ getCachedArray('foo', function(err, result) {
93
+ checkErr(err);
94
+ result = ['a', 'b', 'c'];
95
+
96
+ getCachedArray('foo', function(err, result) {
97
+ checkErr(err);
98
+ assert.deepEqual(result, [1, 2, 3]);
99
+ done();
100
+ });
101
+ });
102
+ });
103
+
104
+ it("does not allow mutation of strings", function(done) {
105
+ getCachedString('foo', function(err, result) {
106
+ checkErr(err);
107
+ result = 'buzz';
108
+
109
+ getCachedString('foo', function(err, result) {
110
+ checkErr(err);
111
+ assert.equal(result, 'bar');
112
+ done();
113
+ });
114
+ });
115
+ });
116
+
117
+ it("does not allow mutation of numbers", function(done) {
118
+ getCachedNumber('foo', function(err, result) {
119
+ checkErr(err);
120
+ result = 12;
121
+
122
+ getCachedNumber('foo', function(err, result) {
123
+ checkErr(err);
124
+ assert.equal(result, 34);
125
+ done();
126
+ });
127
+ });
128
+ });
129
+ });
130
+ });
37
131
  });
@@ -0,0 +1,148 @@
1
+ var assert = require('assert');
2
+ // var support = require('../support');
3
+ var noneStore = require('../../lib/stores/none');
4
+
5
+ describe('none store', function() {
6
+ var key = 'test-key';
7
+ var value = 'test-value';
8
+ var key2 = 'test-key-2';
9
+ var value2 = 'test-value-2';
10
+
11
+ describe('with callback', function() {
12
+ var origPromise;
13
+ var noneCache;
14
+
15
+ beforeEach(function() {
16
+ origPromise = global.Promise;
17
+ delete global.Promise;
18
+ noneCache = noneStore.create({noPromises: true});
19
+ });
20
+
21
+ afterEach(function() {
22
+ global.Promise = origPromise;
23
+ });
24
+
25
+ it('set()', function(done) {
26
+ assert.strictEqual(noneCache.set(key, value), undefined);
27
+ noneCache.set(key, value, function(err) {
28
+ assert.strictEqual(err, null);
29
+ noneCache.set(key, value, {}, done);
30
+ });
31
+ });
32
+
33
+ it('mset()', function(done) {
34
+ assert.strictEqual(noneCache.mset(key, value), undefined);
35
+ noneCache.mset(key, value, key2, value2, function(err) {
36
+ assert.strictEqual(err, null);
37
+ noneCache.mset(key, value, {}, done);
38
+ });
39
+ });
40
+
41
+ it('get()', function(done) {
42
+ assert.strictEqual(noneCache.get(key), undefined);
43
+ noneCache.get(key2, function(err, result) {
44
+ assert.strictEqual(err, null);
45
+ assert.strictEqual(result, undefined);
46
+ noneCache.get(key2, {}, done);
47
+ });
48
+ });
49
+
50
+ it('mget()', function(done) {
51
+ assert.deepEqual(noneCache.mget(key, key2), [undefined, undefined]);
52
+ noneCache.mget(key, key2, function(err, result) {
53
+ assert.strictEqual(err, null);
54
+ assert.deepEqual(result, [undefined, undefined]);
55
+ noneCache.mget(key, key2, key, {}, function(err, result) {
56
+ assert.strictEqual(err, null);
57
+ assert.deepEqual(result, [undefined, undefined, undefined]);
58
+ done();
59
+ });
60
+ });
61
+ });
62
+
63
+ it('del()', function(done) {
64
+ assert.strictEqual(noneCache.del(), undefined);
65
+ noneCache.del(function() {
66
+ noneCache.del({}, done);
67
+ });
68
+ });
69
+
70
+ it('reset()', function(done) {
71
+ assert.strictEqual(noneCache.reset(), undefined);
72
+ noneCache.reset(done);
73
+ });
74
+
75
+ it('keys()', function(done) {
76
+ assert.deepEqual(noneCache.keys(), []);
77
+ noneCache.keys(function(err, keys) {
78
+ assert.deepEqual(keys, []);
79
+ done();
80
+ });
81
+ });
82
+ });
83
+
84
+ describe('with promise', function() {
85
+ var noneCache;
86
+
87
+ beforeEach(function() {
88
+ noneCache = noneStore.create({promiseDependency: global.Promise});
89
+ });
90
+
91
+ it('get() global promise', function(done) {
92
+ noneStore.create()
93
+ .get(key)
94
+ .then(function(result) {
95
+ assert.strictEqual(result, undefined);
96
+ done();
97
+ });
98
+ });
99
+
100
+ it('set()', function(done) {
101
+ noneCache.set(key, value)
102
+ .then(function(result) {
103
+ assert.equal(result, value);
104
+ done();
105
+ });
106
+ });
107
+
108
+ it('mset()', function(done) {
109
+ noneCache.mset(key, value, key2, value2)
110
+ .then(function(result) {
111
+ assert.deepEqual(result, [value, value2]);
112
+ done();
113
+ });
114
+ });
115
+
116
+ it('get()', function(done) {
117
+ noneCache.get(key)
118
+ .then(function(result) {
119
+ assert.strictEqual(result, undefined);
120
+ done();
121
+ });
122
+ });
123
+
124
+ it('mget()', function(done) {
125
+ noneCache.mget(key, key2)
126
+ .then(function(result) {
127
+ assert.deepEqual(result, [undefined, undefined]);
128
+ done();
129
+ });
130
+ });
131
+
132
+ it('del()', function(done) {
133
+ noneCache.del(key).then(done);
134
+ });
135
+
136
+ it('reset()', function(done) {
137
+ noneCache.reset().then(done);
138
+ });
139
+
140
+ it('keys()', function(done) {
141
+ noneCache.keys()
142
+ .then(function(result) {
143
+ assert.deepEqual(result, []);
144
+ done();
145
+ });
146
+ });
147
+ });
148
+ });
package/.npmignore DELETED
@@ -1,6 +0,0 @@
1
- node_modules
2
- coverage
3
- .idea
4
- *.iml
5
- out
6
- .vscode