From afc70d8cd5fe4b0a5693d1a5abcda40cddbbd259 Mon Sep 17 00:00:00 2001 From: James Date: Sun, 12 Jan 2014 17:47:16 -0800 Subject: [PATCH] new arg to ignore cache errors. if set cache errors will be ignored and the cache_manager will go to the backing store --- lib/caching.js | 7 ++-- test/caching.unit.js | 85 +++++++++++++++++++++++++++++++++----------- 2 files changed, 69 insertions(+), 23 deletions(-) diff --git a/lib/caching.js b/lib/caching.js index c5bce72..a8a3172 100644 --- a/lib/caching.js +++ b/lib/caching.js @@ -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 @@ -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); } @@ -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); }); }); diff --git a/test/caching.unit.js b/test/caching.unit.js index 0192542..4e00c38 100644 --- a/test/caching.unit.js +++ b/test/caching.unit.js @@ -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(); }); @@ -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(); + }); }); }); });