Browse Source

test cases passing. but not enough coverage.

feature/specify-what-to-cache
Sean Cady 10 years ago
parent
commit
1c341dd985
  1. 85
      lib/multi_caching.js
  2. 29
      test/stores/options.unit.js

85
lib/multi_caching.js

@ -8,15 +8,38 @@ var domain = require('domain');
*/ */
var multi_caching = function (caches) { var multi_caching = function (caches) {
var self = {}; var self = {};
if (!Array.isArray(caches)) { throw new Error('multi_caching requires an array of caches'); } if (!Array.isArray(caches)) {
throw new Error('multi_caching requires an array of caches');
}
self.queues = {}; self.queues = {};
function get_from_highest_priority_cache(key, cb, options) { function get_from_highest_priority_cache(key, options, cb) {
if (typeof options === 'function') {
cb = options;
options = undefined;
}
var i = 0; var i = 0;
async.forEachSeries(caches, function (cache, async_cb) { async.forEachSeries(caches, function (cache, async_cb) {
if(typeof options === 'object'){
cache.store.get(key, options, function (err, result) {
if (err) {
return cb(err);
}
if (result) {
// break out of async loop.
return cb(err, result, i);
}
i += 1;
async_cb(err);
});
}else{
cache.store.get(key, function (err, result) { cache.store.get(key, function (err, result) {
if (err) { return cb(err); } if (err) {
return cb(err);
}
if (result) { if (result) {
// break out of async loop. // break out of async loop.
return cb(err, result, i); return cb(err, result, i);
@ -24,13 +47,19 @@ var multi_caching = function(caches) {
i += 1; i += 1;
async_cb(err); async_cb(err);
}, options); });
}
}, cb); }, cb);
} }
function set_in_multiple_caches(caches, opts, cb) { function set_in_multiple_caches(caches, opts, cb) {
async.forEach(caches, function (cache, async_cb) { async.forEach(caches, function (cache, async_cb) {
cache.store.set(opts.key, opts.value, opts.ttl, async_cb, opts.options); if (typeof opts.options !== 'object') {
cache.store.set(opts.key, opts.value, opts.ttl, async_cb);
} else {
cache.store.set(opts.key, opts.value, opts.options, async_cb);
}
}, cb); }, cb);
} }
@ -66,11 +95,10 @@ var multi_caching = function(caches) {
* If a key doesn't exist in a higher-priority cache but exists in a lower-priority * If a key doesn't exist in a higher-priority cache but exists in a lower-priority
* cache, it gets set in all higher-priority caches. * cache, it gets set in all higher-priority caches.
*/ */
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]) {
@ -96,9 +124,13 @@ var multi_caching = function(caches) {
var opts = { var opts = {
key: key, key: key,
value: result, value: result,
ttl: ttl,
options: options options: options
}; };
if (typeof options !== 'object') {
opts.ttl = options;
}
set_in_multiple_caches(caches_to_update, opts, function (err) { set_in_multiple_caches(caches_to_update, opts, function (err) {
fillCallbacks(err, result); fillCallbacks(err, result);
}); });
@ -116,9 +148,12 @@ var multi_caching = function(caches) {
var opts = { var opts = {
key: key, key: key,
value: data, value: data,
ttl: ttl,
options: options options: options
}; };
if (typeof options !== 'object') {
opts.ttl = options;
}
set_in_multiple_caches(caches, opts, function (err) { set_in_multiple_caches(caches, opts, function (err) {
if (err) { if (err) {
fillCallbacks(err); fillCallbacks(err);
@ -131,23 +166,37 @@ var multi_caching = function(caches) {
}); });
}; };
self.set = function(key, value, ttl, cb, options) { self.set = function (key, value, options, cb) {
var opts = { var opts = {
key: key, key: key,
value: value, value: value,
ttl: ttl,
options: options options: options
}; };
if (typeof options !== 'object') {
opts.ttl = options;
}
set_in_multiple_caches(caches, opts, cb); set_in_multiple_caches(caches, opts, cb);
}; };
self.get = function(key, cb, options) { self.get = function (key, options, cb) {
get_from_highest_priority_cache(key, cb, options); if (typeof options === 'function') {
cb = options;
options = false;
}
get_from_highest_priority_cache(key, options, cb);
}; };
self.del = function(key, cb, options) { self.del = function (key, options, cb) {
if (typeof options === 'function') {
cb = options;
options = false;
}
async.forEach(caches, function (cache, async_cb) { async.forEach(caches, function (cache, async_cb) {
cache.store.del(key, async_cb, options); if(typeof options ==='object'){
cache.store.del(key, options, async_cb );
}else{
cache.store.del(key, async_cb );
}
}, cb); }, cb);
}; };

29
test/stores/options.unit.js

@ -11,7 +11,7 @@ var testStore = function(args) {
var self = {}; var self = {};
self.name = "options"; self.name = "options";
self.get = function(key, cb, options) { self.get = function(key, options, cb) {
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) {
@ -21,7 +21,7 @@ var testStore = function(args) {
return cb("Error No Options"); return cb("Error No Options");
}; };
self.set = function(key, value, ttl, cb, options) { self.set = function(key, value, options, cb) {
if (options && options.value) { if (options && options.value) {
memoryFlag = options.value + "ValueOption"; memoryFlag = options.value + "ValueOption";
return cb(); return cb();
@ -32,7 +32,7 @@ var testStore = function(args) {
return cb("Error No Options"); return cb("Error No Options");
}; };
self.del = function(key, cb, options) { self.del = function(key, options, cb) {
if (options && options.value) { if (options && options.value) {
memoryFlag = options.value + "ValueOption"; memoryFlag = options.value + "ValueOption";
return cb(); return cb();
@ -64,10 +64,10 @@ describe("Methods with options", function() {
it("lets us pass options by value", function(done) { it("lets us pass options by value", function(done) {
var options = {value: value}; var options = {value: value};
testCache.get(key, function(err, response) { testCache.get(key, options, function(err, response) {
assert.equal(response, value + "ValueOption"); assert.equal(response, value + "ValueOption");
done(); done();
}, options); });
}); });
it("lets us pass options by function", function(done) { it("lets us pass options by function", function(done) {
@ -77,9 +77,9 @@ describe("Methods with options", function() {
done(); done();
} }
}; };
testCache.get(key, function(err, response) { testCache.get(key , options, function(err, response) {
assert.equal(response, "GetFunctionOption"); assert.equal(response, "GetFunctionOption");
}, options); });
}); });
}); });
describe("set with options", function() { describe("set with options", function() {
@ -91,21 +91,22 @@ describe("Methods with options", function() {
}); });
it("lets us pass options by value", function(done) { it("lets us pass options by value", function(done) {
var options = {value: value}; var options = {ttl: ttl, value: value};
testCache.set(key, value, ttl, function() { testCache.set(key, value, options, function() {
assert.equal(memoryFlag, value + "ValueOption"); assert.equal(memoryFlag, value + "ValueOption");
done(); done();
}, options); });
}); });
it("lets us pass options by function", function(done) { it("lets us pass options by function", function(done) {
var options = { var options = {
ttl: ttl,
fn: function(response) { fn: function(response) {
assert.equal(response, "SetFunctionOption"); assert.equal(response, "SetFunctionOption");
done(); done();
} }
}; };
testCache.set(key, value, ttl, function() {}, options); testCache.set(key, value, options, function() {}, options);
}); });
}); });
describe("delete with options", function() { describe("delete with options", function() {
@ -117,10 +118,10 @@ describe("Methods with options", function() {
it("lets us pass options by value", function(done) { it("lets us pass options by value", function(done) {
var options = {value: value}; var options = {value: value};
testCache.del(key, function() { testCache.del(key, options, function() {
assert.equal(memoryFlag,value + "ValueOption"); assert.equal(memoryFlag,value + "ValueOption");
done(); done();
}, options); });
}); });
it("lets us pass options by function", function(done) { it("lets us pass options by function", function(done) {
@ -130,7 +131,7 @@ describe("Methods with options", function() {
done(); done();
} }
}; };
testCache.del(key, function() {}, options); testCache.del(key, options, function() {}, options);
}); });
}); });
}); });

Loading…
Cancel
Save