Browse Source

new arg to ignore cache errors. if set cache errors will be ignored and the cache_manager will go to the backing store

feature/nested-cache-fetch-fix
James 11 years ago
parent
commit
afc70d8cd5
  1. 7
      lib/caching.js
  2. 85
      test/caching.unit.js

7
lib/caching.js

@ -14,6 +14,9 @@ var caching = function (args) {
self.store = require('./stores/' + store_name).create(args); self.store = require('./stores/' + store_name).create(args);
} }
// do we handle a cache error the same as a cache miss?
self.ignoreCacheErrors = args.ignoreCacheErrors || false;
/** /**
* Wraps a function in cache. I.e., the first time the function is run, * Wraps a function in cache. I.e., the first time the function is run,
* its results are stored in cache so subsequent calls retrieve from cache * its results are stored in cache so subsequent calls retrieve from cache
@ -30,7 +33,7 @@ var caching = function (args) {
*/ */
self.wrap = function (key, work, cb) { self.wrap = function (key, work, cb) {
self.store.get(key, function (err, result) { self.store.get(key, function (err, result) {
if (err) { return cb(err); } if (err && (!self.ignoreCacheErrors)) { return cb(err); }
if (result) { if (result) {
return cb(null, result); return cb(null, result);
} }
@ -41,7 +44,7 @@ var caching = function (args) {
return cb(work_args[0]); return cb(work_args[0]);
} }
self.store.set(key, work_args[1], function (err) { self.store.set(key, work_args[1], function (err) {
if (err) { return cb(err); } if (err && (!self.ignoreCacheErrors)) { return cb(err); }
cb.apply(null, work_args); cb.apply(null, work_args);
}); });
}); });

85
test/caching.unit.js

@ -106,7 +106,7 @@ describe("caching", function () {
sinon.stub(memory_store, 'create').returns(memory_store_stub); sinon.stub(memory_store, 'create').returns(memory_store_stub);
cache = caching({store: 'memory', ttl: ttl}); cache = caching({store: 'memory', ttl: ttl, ignoreCacheErrors: false});
key = support.random.string(20); key = support.random.string(20);
name = support.random.string(); name = support.random.string();
}); });
@ -187,37 +187,80 @@ describe("caching", function () {
}); });
context("when store.get() calls back with an error", function () { context("when store.get() calls back with an error", function () {
it("bubbles up that error", function (done) { context("and ignoreCacheErrors is not set (default is false)", function () {
var fake_error = new Error(support.random.string()); it("bubbles up that error", function (done) {
var fake_error = new Error(support.random.string());
sinon.stub(memory_store_stub, 'get', function (key, cb) {
cb(fake_error);
});
sinon.stub(memory_store_stub, 'get', function (key, cb) { cache.wrap(key, function (cb) {
cb(fake_error); methods.get_widget(name, cb);
}, function (err) {
assert.equal(err, fake_error);
memory_store_stub.get.restore();
done();
});
}); });
});
cache.wrap(key, function (cb) { context("and ignoreCacheErrors is set to true", function () {
methods.get_widget(name, cb); it("does not bubble up that error", function (done) {
}, function (err) { cache = caching({store: 'memory', ttl: ttl, ignoreCacheErrors: true});
assert.equal(err, fake_error);
memory_store_stub.get.restore(); var fake_error = new Error(support.random.string());
done();
sinon.stub(memory_store_stub, 'get', function (key, cb) {
cb(fake_error);
});
cache.wrap(key, function (cb) {
methods.get_widget(name, cb);
}, function (err) {
assert.equal(err, null);
memory_store_stub.get.restore();
done();
});
}); });
}); });
}); });
context("when store.set() calls back with an error", function () { context("when store.set() calls back with an error", function () {
it("bubbles up that error", function (done) { context("and ignoreCacheErrors is not set", function () {
var fake_error = new Error(support.random.string()); it("bubbles up that error", function (done) {
var fake_error = new Error(support.random.string());
sinon.stub(memory_store_stub, 'set', function (key, val, cb) { sinon.stub(memory_store_stub, 'set', function (key, val, cb) {
cb(fake_error); cb(fake_error);
});
cache.wrap(key, function (cb) {
methods.get_widget(name, cb);
}, function (err) {
assert.equal(err, fake_error);
memory_store_stub.set.restore();
done();
});
}); });
});
cache.wrap(key, function (cb) { context("and ignoreCacheErrors is set to true", function () {
methods.get_widget(name, cb); it("does not bubbles up that error", function (done) {
}, function (err) { cache = caching({store: 'memory', ttl: ttl, ignoreCacheErrors: true});
assert.equal(err, fake_error); var fake_error = new Error(support.random.string());
memory_store_stub.set.restore();
done(); sinon.stub(memory_store_stub, 'set', function (key, val, cb) {
cb(fake_error);
});
cache.wrap(key, function (cb) {
methods.get_widget(name, cb);
}, function (err) {
assert.equal(err, null);
memory_store_stub.set.restore();
done();
});
}); });
}); });
}); });

Loading…
Cancel
Save