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);
* });
*/
self.wrap = function(key, work, ttl, cb, options) {
if (typeof ttl === 'function') {
cb = ttl;
options = cb;
ttl = undefined;
self.wrap = function(key, work, options, cb) {
if (typeof options === 'function') {
cb = options;
options = undefined;
}
if (self.queues[key]) {
@ -59,7 +58,7 @@ var caching = function(args) {
delete self.queues[key];
}
self.store.get(key, function(err, result) {
self.store.get(key, options, function(err, result) {
if (err && (!self.ignoreCacheErrors)) {
fillCallbacks(err);
} else if (result) {
@ -75,16 +74,16 @@ var caching = function(args) {
fillCallbacks(err);
return;
}
self.store.set(key, data, ttl, function(err) {
self.store.set(key, data, options, function(err) {
if (err && (!self.ignoreCacheErrors)) {
fillCallbacks(err);
} else {
fillCallbacks(null, data);
}
}, options);
});
});
}
}, options);
});
};
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);
self.set = function(key, value, ttl, cb) {
self.set = function(key, value, options, cb) {
lru_cache.set(key, value);
if (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);
if (cb) {
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);
if (cb) {
process.nextTick(cb);

4
test/caching.unit.js

@ -379,7 +379,7 @@ describe("caching", function() {
it("bubbles up that error", function(done) {
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);
});
@ -399,7 +399,7 @@ describe("caching", function() {
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);
});

83
test/stores/options.unit.js

@ -1,6 +1,7 @@
var caching = require("../../index");
var assert = require("assert");
var support = require('../support');
var support = require("../support");
var check_err = support.check_err;
var memoryFlag = "";
var key;
var value;
@ -8,35 +9,70 @@ var testStore = function(args) {
args = args || {};
var self = {};
self.name = "options";
self.store = {};
self.get = function(key, options, cb) {
var optionsMapped = false;
if (typeof options === "function") {
cb = options;
options = false;
optionsMapped = true;
}
if (options && options.value) {
return cb(null, options.value + "ValueOption");
} else if (options && options.fn) {
options.fn("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");
};
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) {
memoryFlag = options.value + "ValueOption";
return cb();
} else if (options && options.fn) {
options.fn("SetFunctionOption");
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");
};
self.del = function(key, options, cb) {
var optionsMapped = false;
if (typeof options === "function") {
cb = options;
options = false;
optionsMapped = true;
}
if (options && options.value) {
memoryFlag = options.value + "ValueOption";
return cb();
} else if (options && options.fn) {
options.fn("DeleteFunctionOption");
return cb();
} else if (options && options.runNormal) {
delete self.store[key];
return cb(null, "");
} else if (optionsMapped) {
return cb();
}
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