Browse Source

Updated options in caching to replace t/l object. Added more test cases.

feature/specify-what-to-cache
Sean Cady 10 years ago
parent
commit
3f4389c869
  1. 17
      lib/caching.js
  2. 12
      lib/stores/memory.js
  3. 4
      test/caching.unit.js
  4. 83
      test/stores/options.unit.js

17
lib/caching.js

@ -37,11 +37,10 @@ var caching = function(args) {
* console.log(user); * console.log(user);
* }); * });
*/ */
self.wrap = function(key, work, ttl, cb, options) { self.wrap = function(key, work, options, cb) {
if (typeof ttl === 'function') { if (typeof options === 'function') {
cb = ttl; cb = options;
options = cb; options = undefined;
ttl = undefined;
} }
if (self.queues[key]) { if (self.queues[key]) {
@ -59,7 +58,7 @@ var caching = function(args) {
delete self.queues[key]; delete self.queues[key];
} }
self.store.get(key, function(err, result) { self.store.get(key, options, function(err, result) {
if (err && (!self.ignoreCacheErrors)) { if (err && (!self.ignoreCacheErrors)) {
fillCallbacks(err); fillCallbacks(err);
} else if (result) { } else if (result) {
@ -75,16 +74,16 @@ var caching = function(args) {
fillCallbacks(err); fillCallbacks(err);
return; return;
} }
self.store.set(key, data, ttl, function(err) { self.store.set(key, data, options, function(err) {
if (err && (!self.ignoreCacheErrors)) { if (err && (!self.ignoreCacheErrors)) {
fillCallbacks(err); fillCallbacks(err);
} else { } else {
fillCallbacks(null, data); fillCallbacks(null, data);
} }
}, options); });
}); });
} }
}, options); });
}; };
self.get = self.store.get.bind(self.store); self.get = self.store.get.bind(self.store);

12
lib/stores/memory.js

@ -12,14 +12,17 @@ var memory_store = function(args) {
var lru_cache = new Lru(lru_opts); var lru_cache = new Lru(lru_opts);
self.set = function(key, value, ttl, cb) { self.set = function(key, value, options, cb) {
lru_cache.set(key, value); lru_cache.set(key, value);
if (cb) { if (cb) {
process.nextTick(cb); process.nextTick(cb);
} }
}; };
self.get = function(key, cb) { self.get = function(key, options, cb) {
if (typeof options === 'function') {
cb = options;
}
var value = lru_cache.get(key); var value = lru_cache.get(key);
if (cb) { if (cb) {
process.nextTick(function() { process.nextTick(function() {
@ -30,7 +33,10 @@ var memory_store = function(args) {
} }
}; };
self.del = function(key, cb) { self.del = function(key, options, cb) {
if (typeof options === 'function') {
cb = options;
}
lru_cache.del(key); lru_cache.del(key);
if (cb) { if (cb) {
process.nextTick(cb); process.nextTick(cb);

4
test/caching.unit.js

@ -379,7 +379,7 @@ describe("caching", function() {
it("bubbles up that error", function(done) { it("bubbles up that error", function(done) {
var fake_error = new Error(support.random.string()); var fake_error = new Error(support.random.string());
sinon.stub(memory_store_stub, 'get', function(key, cb) { sinon.stub(memory_store_stub, 'get', function(key, options, cb) {
cb(fake_error); cb(fake_error);
}); });
@ -399,7 +399,7 @@ describe("caching", function() {
var fake_error = new Error(support.random.string()); var fake_error = new Error(support.random.string());
sinon.stub(memory_store_stub, 'get', function(key, cb) { sinon.stub(memory_store_stub, 'get', function(key, options, cb) {
cb(fake_error); cb(fake_error);
}); });

83
test/stores/options.unit.js

@ -1,6 +1,7 @@
var caching = require("../../index"); var caching = require("../../index");
var assert = require("assert"); var assert = require("assert");
var support = require('../support'); var support = require("../support");
var check_err = support.check_err;
var memoryFlag = ""; var memoryFlag = "";
var key; var key;
var value; var value;
@ -8,35 +9,70 @@ var testStore = function(args) {
args = args || {}; args = args || {};
var self = {}; var self = {};
self.name = "options"; self.name = "options";
self.store = {};
self.get = function(key, options, cb) { self.get = function(key, options, cb) {
var optionsMapped = false;
if (typeof options === "function") {
cb = options;
options = false;
optionsMapped = true;
}
if (options && options.value) { if (options && options.value) {
return cb(null, options.value + "ValueOption"); return cb(null, options.value + "ValueOption");
} else if (options && options.fn) { } else if (options && options.fn) {
options.fn("GetFunctionOption"); options.fn("GetFunctionOption");
return cb(null, "GetFunctionOption"); return cb(null, "GetFunctionOption");
} else if (options && options.runNormal) {
return cb(null, self.store[key]);
} else if (optionsMapped) {
return cb();
} }
return cb("Error No Options"); return cb("Error No Options");
}; };
self.set = function(key, value, options, cb) { self.set = function(key, value, options, cb) {
var optionsMapped = false;
if (typeof options === "function") {
cb = options;
options = false;
optionsMapped = true;
} else if (typeof options !== 'object') {
options = {ttl: options, runNormal: true};
}
if (options && options.value) { if (options && options.value) {
memoryFlag = options.value + "ValueOption"; memoryFlag = options.value + "ValueOption";
return cb(); return cb();
} else if (options && options.fn) { } else if (options && options.fn) {
options.fn("SetFunctionOption"); options.fn("SetFunctionOption");
return cb(); return cb();
} else if (options && options.runNormal) {
self.store[key] = value;
return cb(null, self.store[key]);
} else if (optionsMapped) {
return cb();
} }
return cb("Error No Options"); return cb("Error No Options");
}; };
self.del = function(key, options, cb) { self.del = function(key, options, cb) {
var optionsMapped = false;
if (typeof options === "function") {
cb = options;
options = false;
optionsMapped = true;
}
if (options && options.value) { if (options && options.value) {
memoryFlag = options.value + "ValueOption"; memoryFlag = options.value + "ValueOption";
return cb(); return cb();
} else if (options && options.fn) { } else if (options && options.fn) {
options.fn("DeleteFunctionOption"); options.fn("DeleteFunctionOption");
return cb(); return cb();
} else if (options && options.runNormal) {
delete self.store[key];
return cb(null, "");
} else if (optionsMapped) {
return cb();
} }
return cb("Error No Options"); return cb("Error No Options");
}; };
@ -133,3 +169,48 @@ describe("Methods with options", function() {
}); });
}); });
}); });
describe("Multiple stores with options", function() {
var testInstance = caching.caching({store: testStore()});
var memInstance = caching.caching({store: "memory"});
var testCache;
var options = {runNormal: true};
var ttl = 1;
before(function() {
key = support.random.string(20);
value = support.random.string(20);
testCache = caching.multi_caching([testInstance, memInstance]);
});
it("lets us pass options which only one store uses", function() {
testCache.set(key, value, options, function(err) {
check_err(err);
testCache.get(key, options, function(err, response) {
check_err(err);
assert.equal(response, value);
testCache.del(key, options, function(err) {
check_err(err);
testCache.get(key, options, function(err, response) {
check_err(err);
assert.equal(response, undefined);
});
});
});
});
});
it("lets us not pass options which only one store uses", function() {
testCache.set(key, value, ttl, function(err) {
check_err(err);
testCache.get(key, function(err, response) {
check_err(err);
assert.equal(response, value);
testCache.del(key, function(err) {
check_err(err);
testCache.get(key, function(err, response) {
check_err(err);
assert.equal(response, undefined);
});
});
});
});
});
});

Loading…
Cancel
Save