Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new arg to ignore cache errors. if set cache errors will be ignored and ... #4

Merged
merged 1 commit into from
May 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/caching.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ var caching = function (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,
* its results are stored in cache so subsequent calls retrieve from cache
Expand All @@ -30,7 +33,7 @@ var caching = function (args) {
*/
self.wrap = function (key, work, cb) {
self.store.get(key, function (err, result) {
if (err) { return cb(err); }
if (err && (!self.ignoreCacheErrors)) { return cb(err); }
if (result) {
return cb(null, result);
}
Expand All @@ -41,7 +44,7 @@ var caching = function (args) {
return cb(work_args[0]);
}
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);
});
});
Expand Down
85 changes: 64 additions & 21 deletions test/caching.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe("caching", function () {

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);
name = support.random.string();
});
Expand Down Expand Up @@ -187,37 +187,80 @@ describe("caching", function () {
});

context("when store.get() calls back with an error", function () {
it("bubbles up that error", function (done) {
var fake_error = new Error(support.random.string());
context("and ignoreCacheErrors is not set (default is false)", 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) {
cb(fake_error);
});

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, fake_error);
memory_store_stub.get.restore();
done();
});
});
});

cache.wrap(key, function (cb) {
methods.get_widget(name, cb);
}, function (err) {
assert.equal(err, fake_error);
memory_store_stub.get.restore();
done();
context("and ignoreCacheErrors is set to true", function () {
it("does not bubble up that error", function (done) {
cache = caching({store: 'memory', ttl: ttl, ignoreCacheErrors: true});

var fake_error = new Error(support.random.string());

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 () {
it("bubbles up that error", function (done) {
var fake_error = new Error(support.random.string());
context("and ignoreCacheErrors is not set", function () {
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) {
cb(fake_error);
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, fake_error);
memory_store_stub.set.restore();
done();
});
});
});

cache.wrap(key, function (cb) {
methods.get_widget(name, cb);
}, function (err) {
assert.equal(err, fake_error);
memory_store_stub.set.restore();
done();
context("and ignoreCacheErrors is set to true", function () {
it("does not bubbles up that error", function (done) {
cache = caching({store: 'memory', ttl: ttl, ignoreCacheErrors: true});
var fake_error = new Error(support.random.string());

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();
});
});
});
});
Expand Down