Browse Source

Merge pull request #19 from raadad/feature/update_higher_caches

Get and pass up feature to update higher caches
feature/nested-cache-fetch-fix
Bryan Donovan 10 years ago
parent
commit
430058c863
  1. 23
      lib/multi_caching.js
  2. 88
      test/multi_caching.unit.js

23
lib/multi_caching.js

@ -32,6 +32,29 @@ var multi_caching = function (caches) {
}, cb);
}
/**
* Looks for an item in cache tiers.
*
* When a key is found in a lower cache, all higher levels are updated
*/
self.get_and_pass_up = function(key, cb) {
get_from_highest_priority_cache(key, function(err, result, index) {
if (err) {
return cb(err);
}
cb(err, result);
if (result !== undefined && index) {
var cachesToUpdate = caches.slice(0, index);
async.forEach(cachesToUpdate, function(cache, async_cb) {
cache.set(key, result, result.ttl, async_cb);
});
}
});
};
/**
* Wraps a function in one or more caches.
* Has same API as regular caching module.

88
test/multi_caching.unit.js

@ -178,6 +178,94 @@ describe("multi_caching", function () {
});
});
describe("get_and_pass_up()", function () {
var value;
var key;
describe("using a single cache store", function () {
beforeEach(function () {
multi_cache = multi_caching([memory_cache3]);
key = support.random.string(20);
value = support.random.string();
});
it("gets data from first cache that has it", function (done) {
memory_cache3.set(key, value, ttl, function (err) {
check_err(err);
multi_cache.get_and_pass_up(key, function (err, result) {
check_err(err);
assert.equal(result, value);
done();
});
});
});
});
describe("when value is not found in any cache", function() {
var response;
beforeEach(function(done) {
key = support.random.string(10);
sinon.spy(memory_cache, 'set');
sinon.spy(memory_cache2, 'set');
sinon.spy(memory_cache3, 'set');
multi_cache.get_and_pass_up(key, function (err, result) {
check_err(err);
response = result;
done();
});
});
afterEach(function() {
memory_cache.set.restore();
memory_cache2.set.restore();
memory_cache3.set.restore();
});
it("calls back with undefined", function() {
assert.strictEqual(response, undefined);
});
it("does not set anything in caches", function(done) {
process.nextTick(function () {
assert.ok(memory_cache.set.notCalled);
assert.ok(memory_cache2.set.notCalled);
assert.ok(memory_cache3.set.notCalled);
done();
});
});
});
describe("using multi cache store", function () {
beforeEach(function () {
multi_cache = multi_caching([memory_cache,memory_cache2,memory_cache3]);
key = support.random.string(20);
value = support.random.string();
});
it("checks to see if higher levels have item", function (done) {
memory_cache3.set(key, value, ttl, function (err) {
check_err(err);
multi_cache.get_and_pass_up(key, function (err, result) {
check_err(err);
assert.equal(result, value);
process.nextTick(function() {
memory_cache.get(key, function (err, result) {
assert.equal(result, value);
check_err(err);
done();
});
});
});
});
});
});
});
describe("wrap()", function () {
describe("using a single cache store", function () {
beforeEach(function () {

Loading…
Cancel
Save