cache-manager 2.5.0 → 2.9.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/.npmignore +1 -0
- package/History.md +15 -0
- package/README.md +101 -3
- package/lib/caching.js +147 -21
- package/lib/multi_caching.js +355 -68
- package/lib/stores/memory.js +80 -4
- package/lib/utils.js +43 -0
- package/package.json +2 -2
- package/test/caching.unit.js +333 -2
- package/test/multi_caching.unit.js +589 -5
- package/test/utils.unit.js +25 -0
- package/config.js +0 -7
- package/test/foo.unit.js +0 -69
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cache-manager",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.9.0",
|
|
4
4
|
"description": "Cache module for Node.js",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"es6-promise": "^3.0.2",
|
|
29
29
|
"istanbul": "0.4.2",
|
|
30
30
|
"jscs": "2.11.0",
|
|
31
|
-
"jsdoc": "3.
|
|
31
|
+
"jsdoc": "3.5.5",
|
|
32
32
|
"jshint": "2.9.1",
|
|
33
33
|
"mocha": "2.4.5",
|
|
34
34
|
"optimist": "0.6.1",
|
package/test/caching.unit.js
CHANGED
|
@@ -15,7 +15,12 @@ var methods = {
|
|
|
15
15
|
process.nextTick(function() {
|
|
16
16
|
cb(null, {name: name});
|
|
17
17
|
});
|
|
18
|
-
}
|
|
18
|
+
},
|
|
19
|
+
getMultiWidget: function(names, cb) {
|
|
20
|
+
process.nextTick(function() {
|
|
21
|
+
cb(null, names.map(function(name) { return {name: name}; }));
|
|
22
|
+
});
|
|
23
|
+
},
|
|
19
24
|
};
|
|
20
25
|
|
|
21
26
|
describe("caching", function() {
|
|
@@ -82,6 +87,82 @@ describe("caching", function() {
|
|
|
82
87
|
});
|
|
83
88
|
});
|
|
84
89
|
|
|
90
|
+
describe("mget() and mset()", function() {
|
|
91
|
+
var key2;
|
|
92
|
+
var value2;
|
|
93
|
+
var store = 'memory';
|
|
94
|
+
|
|
95
|
+
beforeEach(function() {
|
|
96
|
+
key = support.random.string(20);
|
|
97
|
+
value = support.random.string();
|
|
98
|
+
key2 = support.random.string(20);
|
|
99
|
+
value2 = support.random.string();
|
|
100
|
+
|
|
101
|
+
cache = caching({store: store, ttl: defaultTtl, ignoreCacheErrors: false});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("lets us set and get several keys and data in cache", function(done) {
|
|
105
|
+
cache.mset(key, value, key2, value2, {ttl: defaultTtl}, function(err) {
|
|
106
|
+
checkErr(err);
|
|
107
|
+
|
|
108
|
+
cache.mget(key, key2, function(err, result) {
|
|
109
|
+
checkErr(err);
|
|
110
|
+
assert.equal(result[0], value);
|
|
111
|
+
assert.equal(result[1], value2);
|
|
112
|
+
done();
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("lets us set and get data without a callback", function(done) {
|
|
118
|
+
cache = caching({store: memoryStore.create({noPromises: true})});
|
|
119
|
+
cache.mset(key, value, key2, value2, {ttl: defaultTtl});
|
|
120
|
+
|
|
121
|
+
setTimeout(function() {
|
|
122
|
+
var result = cache.mget(key, key2);
|
|
123
|
+
assert.equal(result[0], value);
|
|
124
|
+
assert.equal(result[1], value2);
|
|
125
|
+
done();
|
|
126
|
+
}, 20);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("lets us set and get data without a callback, returning a promise", function(done) {
|
|
130
|
+
cache.mset(key, value, key2, value2, {ttl: defaultTtl});
|
|
131
|
+
setTimeout(function() {
|
|
132
|
+
cache.mget(key, key2)
|
|
133
|
+
.then(function(result) {
|
|
134
|
+
assert.equal(result[0], value);
|
|
135
|
+
assert.equal(result[1], value2);
|
|
136
|
+
done();
|
|
137
|
+
});
|
|
138
|
+
}, 20);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("lets us set and get data without options object or callback", function(done) {
|
|
142
|
+
cache = caching({store: memoryStore.create({noPromises: true})});
|
|
143
|
+
cache.mset(key, value, key2, value2);
|
|
144
|
+
|
|
145
|
+
setTimeout(function() {
|
|
146
|
+
var result = cache.mget(key, key2);
|
|
147
|
+
assert.equal(result[0], value);
|
|
148
|
+
assert.equal(result[1], value2);
|
|
149
|
+
done();
|
|
150
|
+
}, 20);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("lets us pass an 'options' object", function(done) {
|
|
154
|
+
cache = caching({store: memoryStore.create({noPromises: true})});
|
|
155
|
+
cache.mset(key, value, key2, value2);
|
|
156
|
+
|
|
157
|
+
setTimeout(function() {
|
|
158
|
+
var result = cache.mget(key, key2, {someConfig: true});
|
|
159
|
+
assert.equal(result[0], value);
|
|
160
|
+
assert.equal(result[1], value2);
|
|
161
|
+
done();
|
|
162
|
+
}, 20);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
85
166
|
describe("del()", function() {
|
|
86
167
|
['memory'].forEach(function(store) {
|
|
87
168
|
context("using " + store + " store", function() {
|
|
@@ -143,6 +224,55 @@ describe("caching", function() {
|
|
|
143
224
|
done(err);
|
|
144
225
|
});
|
|
145
226
|
});
|
|
227
|
+
|
|
228
|
+
describe('with multiple keys', function() {
|
|
229
|
+
var key2;
|
|
230
|
+
var value2;
|
|
231
|
+
|
|
232
|
+
beforeEach(function(done) {
|
|
233
|
+
cache = caching({store: store});
|
|
234
|
+
key2 = support.random.string(20);
|
|
235
|
+
value2 = support.random.string();
|
|
236
|
+
cache.mset(key, value, key2, value2, {ttl: defaultTtl}, function(err) {
|
|
237
|
+
checkErr(err);
|
|
238
|
+
done();
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('deletes an unlimited number of key arguments', function(done) {
|
|
243
|
+
cache.mget(key, key2, function(err, result) {
|
|
244
|
+
assert.equal(result[0], value);
|
|
245
|
+
assert.equal(result[1], value2);
|
|
246
|
+
|
|
247
|
+
cache.del(key, key2, function(err) {
|
|
248
|
+
checkErr(err);
|
|
249
|
+
|
|
250
|
+
cache.mget(key, key2, function(err, result) {
|
|
251
|
+
assert.ok(!result[0]);
|
|
252
|
+
assert.ok(!result[1]);
|
|
253
|
+
done();
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('deletes an array of keys', function(done) {
|
|
260
|
+
cache.mget(key, key2, function(err, result) {
|
|
261
|
+
assert.equal(result[0], value);
|
|
262
|
+
assert.equal(result[1], value2);
|
|
263
|
+
|
|
264
|
+
cache.del([key, key2], function(err) {
|
|
265
|
+
checkErr(err);
|
|
266
|
+
|
|
267
|
+
cache.mget(key, key2, function(err, result) {
|
|
268
|
+
assert.ok(!result[0]);
|
|
269
|
+
assert.ok(!result[1]);
|
|
270
|
+
done();
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
});
|
|
146
276
|
});
|
|
147
277
|
});
|
|
148
278
|
});
|
|
@@ -194,7 +324,7 @@ describe("caching", function() {
|
|
|
194
324
|
}, 10);
|
|
195
325
|
});
|
|
196
326
|
|
|
197
|
-
context("when store has no del() method", function() {
|
|
327
|
+
context("when store has no del(), mget() or mset() method", function() {
|
|
198
328
|
var fakeStore;
|
|
199
329
|
|
|
200
330
|
beforeEach(function() {
|
|
@@ -770,10 +900,165 @@ describe("caching", function() {
|
|
|
770
900
|
});
|
|
771
901
|
});
|
|
772
902
|
});
|
|
903
|
+
|
|
904
|
+
context("when passing multiple keys", function() {
|
|
905
|
+
var key2;
|
|
906
|
+
var name2;
|
|
907
|
+
|
|
908
|
+
beforeEach(function() {
|
|
909
|
+
key2 = support.random.string(20);
|
|
910
|
+
name2 = support.random.string();
|
|
911
|
+
sinon.spy(memoryStoreStub, 'mset');
|
|
912
|
+
});
|
|
913
|
+
|
|
914
|
+
afterEach(function() {
|
|
915
|
+
memoryStoreStub.mset.restore();
|
|
916
|
+
});
|
|
917
|
+
|
|
918
|
+
context("when result is already cached", function() {
|
|
919
|
+
it("retrieves data from cache", function(done) {
|
|
920
|
+
var funcCalled = false;
|
|
921
|
+
sinon.stub(memoryStoreStub, 'mget', function() {
|
|
922
|
+
var args = Array.prototype.slice.apply(arguments);
|
|
923
|
+
var cb = args.pop();
|
|
924
|
+
cb(null, [{name: name}, {name: name2}]);
|
|
925
|
+
});
|
|
926
|
+
|
|
927
|
+
cache.wrap(key, key2, function(cb) {
|
|
928
|
+
funcCalled = true;
|
|
929
|
+
cb();
|
|
930
|
+
}, function(err, widgets) {
|
|
931
|
+
checkErr(err);
|
|
932
|
+
assert.deepEqual(widgets[0], {name: name});
|
|
933
|
+
assert.deepEqual(widgets[1], {name: name2});
|
|
934
|
+
assert.ok(memoryStoreStub.mget.calledWith(key, key2));
|
|
935
|
+
assert.ok(!funcCalled);
|
|
936
|
+
memoryStoreStub.mget.restore();
|
|
937
|
+
done();
|
|
938
|
+
});
|
|
939
|
+
});
|
|
940
|
+
});
|
|
941
|
+
|
|
942
|
+
it("when a ttl is passed in", function(done) {
|
|
943
|
+
cache.wrap(key, key2, function(cb) {
|
|
944
|
+
methods.getMultiWidget([name, name2], cb);
|
|
945
|
+
}, opts, function(err, widgets) {
|
|
946
|
+
checkErr(err);
|
|
947
|
+
assert.deepEqual(widgets[0], {name: name});
|
|
948
|
+
assert.deepEqual(widgets[1], {name: name2});
|
|
949
|
+
sinon.assert.calledWith(memoryStoreStub.mset, key, {name: name}, key2, {name: name2}, opts);
|
|
950
|
+
done();
|
|
951
|
+
});
|
|
952
|
+
});
|
|
953
|
+
|
|
954
|
+
it("when a ttl is passed in (function)", function(done) {
|
|
955
|
+
var ttlFunc = function() { return 1234; };
|
|
956
|
+
opts = {ttl: ttlFunc};
|
|
957
|
+
|
|
958
|
+
cache.wrap(key, key2, function(cb) {
|
|
959
|
+
methods.getMultiWidget([name, name2], cb);
|
|
960
|
+
}, opts, function(err) {
|
|
961
|
+
checkErr(err);
|
|
962
|
+
sinon.assert.calledWith(
|
|
963
|
+
memoryStoreStub.mset, key, {name: name}, key2, {name: name2}, {ttl: 1234}
|
|
964
|
+
);
|
|
965
|
+
done();
|
|
966
|
+
});
|
|
967
|
+
});
|
|
968
|
+
|
|
969
|
+
it("when a ttl is not passed in", function(done) {
|
|
970
|
+
cache.wrap(key, key2, function(cb) {
|
|
971
|
+
methods.getMultiWidget([name, name2], cb);
|
|
972
|
+
}, function(err, widgets) {
|
|
973
|
+
checkErr(err);
|
|
974
|
+
assert.deepEqual(widgets[0], {name: name});
|
|
975
|
+
assert.deepEqual(widgets[1], {name: name2});
|
|
976
|
+
sinon.assert.calledWith(memoryStoreStub.mset, key, {name: name}, key2, {name: name2}, {});
|
|
977
|
+
done();
|
|
978
|
+
});
|
|
979
|
+
});
|
|
980
|
+
|
|
981
|
+
it("does not store non-allowed values", function(done) {
|
|
982
|
+
var name = 'bar';
|
|
983
|
+
|
|
984
|
+
cache = caching({store: 'memory', isCacheableValue: function() { return false; }});
|
|
985
|
+
cache.wrap(key, key2, function(cb) {
|
|
986
|
+
methods.getMultiWidget([name, name2], cb);
|
|
987
|
+
}, function(err) {
|
|
988
|
+
checkErr(err);
|
|
989
|
+
assert.ok(memoryStoreStub.mset.notCalled);
|
|
990
|
+
done();
|
|
991
|
+
});
|
|
992
|
+
});
|
|
993
|
+
|
|
994
|
+
context("when store.mget() calls back with an error", function() {
|
|
995
|
+
context("and ignoreCacheErrors is not set (default is false)", function() {
|
|
996
|
+
it("bubbles up that error", function(done) {
|
|
997
|
+
var fakeError = new Error(support.random.string());
|
|
998
|
+
|
|
999
|
+
sinon.stub(memoryStoreStub, 'mget', function() {
|
|
1000
|
+
var args = Array.prototype.slice.apply(arguments);
|
|
1001
|
+
var cb = args.pop();
|
|
1002
|
+
cb(fakeError);
|
|
1003
|
+
});
|
|
1004
|
+
|
|
1005
|
+
cache.wrap(key, key2, function(cb) {
|
|
1006
|
+
methods.getMultiWidget([name, name2], cb);
|
|
1007
|
+
}, function(err) {
|
|
1008
|
+
assert.equal(err, fakeError);
|
|
1009
|
+
memoryStoreStub.mget.restore();
|
|
1010
|
+
done();
|
|
1011
|
+
});
|
|
1012
|
+
});
|
|
1013
|
+
});
|
|
1014
|
+
|
|
1015
|
+
context("and ignoreCacheErrors is set to true", function() {
|
|
1016
|
+
it("does not bubble up that error", function(done) {
|
|
1017
|
+
cache = caching({store: 'memory', ttl: defaultTtl, ignoreCacheErrors: true});
|
|
1018
|
+
|
|
1019
|
+
var fakeError = new Error(support.random.string());
|
|
1020
|
+
|
|
1021
|
+
sinon.stub(memoryStoreStub, 'mget', function() {
|
|
1022
|
+
var args = Array.prototype.slice.apply(arguments);
|
|
1023
|
+
var cb = args.pop();
|
|
1024
|
+
cb(fakeError);
|
|
1025
|
+
});
|
|
1026
|
+
|
|
1027
|
+
cache.wrap(key, key2, function(cb) {
|
|
1028
|
+
methods.getMultiWidget([name, name2], cb);
|
|
1029
|
+
}, function(err) {
|
|
1030
|
+
assert.equal(err, null);
|
|
1031
|
+
memoryStoreStub.mget.restore();
|
|
1032
|
+
done();
|
|
1033
|
+
});
|
|
1034
|
+
});
|
|
1035
|
+
});
|
|
1036
|
+
});
|
|
1037
|
+
|
|
1038
|
+
context("when an error is thrown in the work function", function() {
|
|
1039
|
+
var fakeError;
|
|
1040
|
+
|
|
1041
|
+
beforeEach(function() {
|
|
1042
|
+
fakeError = new Error(support.random.string());
|
|
1043
|
+
});
|
|
1044
|
+
|
|
1045
|
+
it("does catch the error ", function(done) {
|
|
1046
|
+
cache = caching({store: 'memory', ttl: defaultTtl, ignoreCacheErrors: false});
|
|
1047
|
+
|
|
1048
|
+
cache.wrap(key, key2, function(cb) {
|
|
1049
|
+
return cb(fakeError);
|
|
1050
|
+
}, function(err) {
|
|
1051
|
+
assert.equal(err, fakeError);
|
|
1052
|
+
done();
|
|
1053
|
+
});
|
|
1054
|
+
});
|
|
1055
|
+
});
|
|
1056
|
+
});
|
|
773
1057
|
});
|
|
774
1058
|
|
|
775
1059
|
describe("when called multiple times in parallel with same key", function() {
|
|
776
1060
|
var construct;
|
|
1061
|
+
var constructMultiple;
|
|
777
1062
|
|
|
778
1063
|
beforeEach(function() {
|
|
779
1064
|
cache = caching({
|
|
@@ -788,6 +1073,13 @@ describe("caching", function() {
|
|
|
788
1073
|
cb(null, 'value');
|
|
789
1074
|
}, timeout);
|
|
790
1075
|
});
|
|
1076
|
+
|
|
1077
|
+
constructMultiple = sinon.spy(function(val, cb) {
|
|
1078
|
+
var timeout = support.random.number(100);
|
|
1079
|
+
setTimeout(function() {
|
|
1080
|
+
cb(null, ['value', 'value2']);
|
|
1081
|
+
}, timeout);
|
|
1082
|
+
});
|
|
791
1083
|
});
|
|
792
1084
|
|
|
793
1085
|
it("calls the wrapped function once", function(done) {
|
|
@@ -809,6 +1101,27 @@ describe("caching", function() {
|
|
|
809
1101
|
done();
|
|
810
1102
|
});
|
|
811
1103
|
});
|
|
1104
|
+
|
|
1105
|
+
it("calls the multiWrapped function once", function(done) {
|
|
1106
|
+
var values = [];
|
|
1107
|
+
for (var i = 0; i < 2; i++) {
|
|
1108
|
+
values.push(i);
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
async.each(values, function(val, next) {
|
|
1112
|
+
cache.wrap('key', 'key2', function(cb) {
|
|
1113
|
+
constructMultiple(val, cb);
|
|
1114
|
+
}, function(err, results) {
|
|
1115
|
+
assert.equal(results[0], 'value');
|
|
1116
|
+
assert.equal(results[1], 'value2');
|
|
1117
|
+
next(err);
|
|
1118
|
+
});
|
|
1119
|
+
}, function(err) {
|
|
1120
|
+
checkErr(err);
|
|
1121
|
+
assert.equal(constructMultiple.callCount, 1);
|
|
1122
|
+
done();
|
|
1123
|
+
});
|
|
1124
|
+
});
|
|
812
1125
|
});
|
|
813
1126
|
|
|
814
1127
|
describe("using native promises", function() {
|
|
@@ -921,4 +1234,22 @@ describe("caching", function() {
|
|
|
921
1234
|
});
|
|
922
1235
|
});
|
|
923
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
|
+
});
|
|
924
1255
|
});
|