Skip to content

Commit 3f4389c

Browse files
committed
Updated options in caching to replace t/l object. Added more test cases.
1 parent 8ee4c79 commit 3f4389c

File tree

4 files changed

+101
-15
lines changed

4 files changed

+101
-15
lines changed

lib/caching.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ var caching = function(args) {
3737
* console.log(user);
3838
* });
3939
*/
40-
self.wrap = function(key, work, ttl, cb, options) {
41-
if (typeof ttl === 'function') {
42-
cb = ttl;
43-
options = cb;
44-
ttl = undefined;
40+
self.wrap = function(key, work, options, cb) {
41+
if (typeof options === 'function') {
42+
cb = options;
43+
options = undefined;
4544
}
4645

4746
if (self.queues[key]) {
@@ -59,7 +58,7 @@ var caching = function(args) {
5958
delete self.queues[key];
6059
}
6160

62-
self.store.get(key, function(err, result) {
61+
self.store.get(key, options, function(err, result) {
6362
if (err && (!self.ignoreCacheErrors)) {
6463
fillCallbacks(err);
6564
} else if (result) {
@@ -75,16 +74,16 @@ var caching = function(args) {
7574
fillCallbacks(err);
7675
return;
7776
}
78-
self.store.set(key, data, ttl, function(err) {
77+
self.store.set(key, data, options, function(err) {
7978
if (err && (!self.ignoreCacheErrors)) {
8079
fillCallbacks(err);
8180
} else {
8281
fillCallbacks(null, data);
8382
}
84-
}, options);
83+
});
8584
});
8685
}
87-
}, options);
86+
});
8887
};
8988

9089
self.get = self.store.get.bind(self.store);

lib/stores/memory.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ var memory_store = function(args) {
1212

1313
var lru_cache = new Lru(lru_opts);
1414

15-
self.set = function(key, value, ttl, cb) {
15+
self.set = function(key, value, options, cb) {
1616
lru_cache.set(key, value);
1717
if (cb) {
1818
process.nextTick(cb);
1919
}
2020
};
2121

22-
self.get = function(key, cb) {
22+
self.get = function(key, options, cb) {
23+
if (typeof options === 'function') {
24+
cb = options;
25+
}
2326
var value = lru_cache.get(key);
2427
if (cb) {
2528
process.nextTick(function() {
@@ -30,7 +33,10 @@ var memory_store = function(args) {
3033
}
3134
};
3235

33-
self.del = function(key, cb) {
36+
self.del = function(key, options, cb) {
37+
if (typeof options === 'function') {
38+
cb = options;
39+
}
3440
lru_cache.del(key);
3541
if (cb) {
3642
process.nextTick(cb);

test/caching.unit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ describe("caching", function() {
379379
it("bubbles up that error", function(done) {
380380
var fake_error = new Error(support.random.string());
381381

382-
sinon.stub(memory_store_stub, 'get', function(key, cb) {
382+
sinon.stub(memory_store_stub, 'get', function(key, options, cb) {
383383
cb(fake_error);
384384
});
385385

@@ -399,7 +399,7 @@ describe("caching", function() {
399399

400400
var fake_error = new Error(support.random.string());
401401

402-
sinon.stub(memory_store_stub, 'get', function(key, cb) {
402+
sinon.stub(memory_store_stub, 'get', function(key, options, cb) {
403403
cb(fake_error);
404404
});
405405

test/stores/options.unit.js

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,78 @@
11
var caching = require("../../index");
22
var assert = require("assert");
3-
var support = require('../support');
3+
var support = require("../support");
4+
var check_err = support.check_err;
45
var memoryFlag = "";
56
var key;
67
var value;
78
var testStore = function(args) {
89
args = args || {};
910
var self = {};
1011
self.name = "options";
12+
self.store = {};
1113

1214
self.get = function(key, options, cb) {
15+
var optionsMapped = false;
16+
if (typeof options === "function") {
17+
cb = options;
18+
options = false;
19+
optionsMapped = true;
20+
}
1321
if (options && options.value) {
1422
return cb(null, options.value + "ValueOption");
1523
} else if (options && options.fn) {
1624
options.fn("GetFunctionOption");
1725
return cb(null, "GetFunctionOption");
26+
} else if (options && options.runNormal) {
27+
return cb(null, self.store[key]);
28+
} else if (optionsMapped) {
29+
return cb();
1830
}
1931
return cb("Error No Options");
2032
};
2133

2234
self.set = function(key, value, options, cb) {
35+
var optionsMapped = false;
36+
if (typeof options === "function") {
37+
cb = options;
38+
options = false;
39+
optionsMapped = true;
40+
} else if (typeof options !== 'object') {
41+
options = {ttl: options, runNormal: true};
42+
}
2343
if (options && options.value) {
2444
memoryFlag = options.value + "ValueOption";
2545
return cb();
2646
} else if (options && options.fn) {
2747
options.fn("SetFunctionOption");
2848
return cb();
49+
} else if (options && options.runNormal) {
50+
self.store[key] = value;
51+
return cb(null, self.store[key]);
52+
} else if (optionsMapped) {
53+
return cb();
2954
}
3055
return cb("Error No Options");
3156
};
3257

3358
self.del = function(key, options, cb) {
59+
var optionsMapped = false;
60+
if (typeof options === "function") {
61+
cb = options;
62+
options = false;
63+
optionsMapped = true;
64+
}
3465
if (options && options.value) {
3566
memoryFlag = options.value + "ValueOption";
3667
return cb();
3768
} else if (options && options.fn) {
3869
options.fn("DeleteFunctionOption");
3970
return cb();
71+
} else if (options && options.runNormal) {
72+
delete self.store[key];
73+
return cb(null, "");
74+
} else if (optionsMapped) {
75+
return cb();
4076
}
4177
return cb("Error No Options");
4278
};
@@ -133,3 +169,48 @@ describe("Methods with options", function() {
133169
});
134170
});
135171
});
172+
describe("Multiple stores with options", function() {
173+
var testInstance = caching.caching({store: testStore()});
174+
var memInstance = caching.caching({store: "memory"});
175+
var testCache;
176+
var options = {runNormal: true};
177+
var ttl = 1;
178+
before(function() {
179+
key = support.random.string(20);
180+
value = support.random.string(20);
181+
testCache = caching.multi_caching([testInstance, memInstance]);
182+
});
183+
184+
it("lets us pass options which only one store uses", function() {
185+
testCache.set(key, value, options, function(err) {
186+
check_err(err);
187+
testCache.get(key, options, function(err, response) {
188+
check_err(err);
189+
assert.equal(response, value);
190+
testCache.del(key, options, function(err) {
191+
check_err(err);
192+
testCache.get(key, options, function(err, response) {
193+
check_err(err);
194+
assert.equal(response, undefined);
195+
});
196+
});
197+
});
198+
});
199+
});
200+
it("lets us not pass options which only one store uses", function() {
201+
testCache.set(key, value, ttl, function(err) {
202+
check_err(err);
203+
testCache.get(key, function(err, response) {
204+
check_err(err);
205+
assert.equal(response, value);
206+
testCache.del(key, function(err) {
207+
check_err(err);
208+
testCache.get(key, function(err, response) {
209+
check_err(err);
210+
assert.equal(response, undefined);
211+
});
212+
});
213+
});
214+
});
215+
});
216+
});

0 commit comments

Comments
 (0)