cache-manager 2.7.0 → 2.10.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 CHANGED
@@ -1,3 +1,17 @@
1
+ - 2.10.0 2019-07-11
2
+ - Add development store "none" (#129). -@ R3VoLuT1OneR
3
+
4
+ - 2.9.1 2019-05-28
5
+ - Bind isCacheableValue to its underlying store (#126). - @arash16
6
+
7
+ - 2.9.0 2018-03-26
8
+ - Fix store's this context for isCacheableValue method (#119). - @alexandrusavin
9
+
10
+ - 2.8.0 2018-02-28
11
+ - Added link to node-cache-manager-fs-hash - @rolandstarke
12
+ - Bugfix for options object in multiCache.set (#115) - @sebelga
13
+ - Bugfix for keysToFetch in getFromHighestProrityCache (#117) - @sebelga
14
+
1
15
  - 2.7.0 2018-02-13
2
16
  - allow setting, getting and deleting multiple keys (#114) - @sebelga
3
17
  - allow passing in a function to determine TTL based on store - @sebelga
package/README.md CHANGED
@@ -43,6 +43,8 @@ See the [Express.js cache-manager example app](https://github.com/BryanDonovan/n
43
43
 
44
44
  * [node-cache-manager-fs-binary](https://github.com/sheershoff/node-cache-manager-fs-binary)
45
45
 
46
+ * [node-cache-manager-fs-hash](https://github.com/rolandstarke/node-cache-manager-fs-hash)
47
+
46
48
  * [node-cache-manager-hazelcast](https://github.com/marudor/node-cache-manager-hazelcast)
47
49
 
48
50
  * [node-cache-manager-memcached-store](https://github.com/theogravity/node-cache-manager-memcached-store)
@@ -409,6 +411,9 @@ var multiCache = cacheManager.multiCaching([memoryCache, someOtherCache], {
409
411
 
410
412
  ```
411
413
 
414
+ ### Development environment
415
+ You may disable real caching but still get all the callback functionality working by setting `none` store.
416
+
412
417
  ## Docs
413
418
 
414
419
  To generate JSDOC 3 documentation:
package/lib/caching.js CHANGED
@@ -38,7 +38,7 @@ var caching = function(args) {
38
38
  if (typeof args.isCacheableValue === 'function') {
39
39
  self._isCacheableValue = args.isCacheableValue;
40
40
  } else if (typeof self.store.isCacheableValue === 'function') {
41
- self._isCacheableValue = self.store.isCacheableValue;
41
+ self._isCacheableValue = self.store.isCacheableValue.bind(self.store);
42
42
  } else {
43
43
  self._isCacheableValue = function(value) {
44
44
  return value !== undefined;
@@ -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
  }
@@ -172,7 +172,7 @@ var multiCaching = function(caches, options) {
172
172
  mapResult[key] = res;
173
173
 
174
174
  // delete key from our keysToFetch array
175
- keysToFetch.splice(i, 1);
175
+ keysToFetch.splice(i - diff, 1);
176
176
  diff += 1;
177
177
  }
178
178
  });
@@ -209,7 +209,7 @@ var multiCaching = function(caches, options) {
209
209
  return setInMultipleCachesPromise.apply(this, args);
210
210
  }
211
211
 
212
- if (isObject(args[args.length - 1])) {
212
+ if (args.length % 2 > 0 && isObject(args[args.length - 1])) {
213
213
  options = args.pop();
214
214
  }
215
215
 
@@ -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.7.0",
3
+ "version": "2.10.0",
4
4
  "description": "Cache module for Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1234,4 +1234,22 @@ describe("caching", function() {
1234
1234
  });
1235
1235
  });
1236
1236
  });
1237
+
1238
+ describe("when using store's isCacheableValue method", function() {
1239
+ it("should not break its' context", function() {
1240
+ var store = {
1241
+ isCacheableValue: function() {
1242
+ if (this !== store) {
1243
+ throw new Error("Broken store context");
1244
+ }
1245
+ },
1246
+ get: function() {},
1247
+ set: function() {},
1248
+ };
1249
+
1250
+ cache = caching({store: store});
1251
+
1252
+ assert.doesNotThrow(cache._isCacheableValue);
1253
+ });
1254
+ });
1237
1255
  });
@@ -89,6 +89,29 @@ describe("multiCaching", function() {
89
89
  });
90
90
  });
91
91
 
92
+ it("lets us set data ('Object' type) in all caches", function(done) {
93
+ value = {name: support.random.string()};
94
+ multiCache.set(key, value, function(err) {
95
+ checkErr(err);
96
+
97
+ memoryCache.get(key, function(err, result) {
98
+ checkErr(err);
99
+ assert.equal(result, value);
100
+
101
+ memoryCache2.get(key, function(err, result) {
102
+ checkErr(err);
103
+ assert.equal(result, value);
104
+
105
+ memoryCache3.get(key, function(err, result) {
106
+ checkErr(err);
107
+ assert.equal(result, value);
108
+ done();
109
+ });
110
+ });
111
+ });
112
+ });
113
+ });
114
+
92
115
  it("lets us set data without a callback", function(done) {
93
116
  multiCache.set(key, value, {ttl: defaultTtl});
94
117
  setTimeout(function() {
@@ -195,6 +218,32 @@ describe("multiCaching", function() {
195
218
  });
196
219
  });
197
220
 
221
+ it("lets us set data ('Object' type) in all caches", function(done) {
222
+ value2 = {name: support.random.string()};
223
+ multiCache.mset(key, value, key2, value2, function(err) {
224
+ checkErr(err);
225
+
226
+ memoryCache.mget(key, key2, function(err, result) {
227
+ checkErr(err);
228
+ assert.equal(result[0], value);
229
+ assert.equal(result[1], value2);
230
+
231
+ memoryCache2.mget(key, key2, function(err, result) {
232
+ checkErr(err);
233
+ assert.equal(result[0], value);
234
+ assert.equal(result[1], value2);
235
+
236
+ memoryCache3.mget(key, key2, function(err, result) {
237
+ checkErr(err);
238
+ assert.equal(result[0], value);
239
+ assert.equal(result[1], value2);
240
+ done();
241
+ });
242
+ });
243
+ });
244
+ });
245
+ });
246
+
198
247
  it("lets us set data without a callback", function(done) {
199
248
  multiCache.mset(key, value, key2, value2, {ttl: defaultTtl});
200
249
  setTimeout(function() {
@@ -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