diff --git a/History.md b/History.md index b35a65a..de5b3ea 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,6 @@ +- 0.5.0 2014-05-02 + Adding reset() function to caching.js. Closes #5. + - 0.4.0 2014-05-02 New arg to ignore cache errors. if set cache errors will be ignored and the cache_manager will go to the backing store. (Thanks londonjamo). diff --git a/lib/caching.js b/lib/caching.js index a8a3172..9e560db 100644 --- a/lib/caching.js +++ b/lib/caching.js @@ -57,6 +57,10 @@ var caching = function (args) { self.del = self.store.del.bind(self.store); + if (typeof self.store.reset === 'function') { + self.reset = self.store.reset.bind(self.store); + } + return self; }; diff --git a/lib/stores/memory.js b/lib/stores/memory.js index 7c27f66..351af0d 100644 --- a/lib/stores/memory.js +++ b/lib/stores/memory.js @@ -32,6 +32,13 @@ var memory_store = function (args) { } }; + self.reset = function (cb) { + lru_cache.reset(); + if (cb) { + process.nextTick(cb); + } + }; + return self; }; diff --git a/package.json b/package.json index 07a307a..fdd8622 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cache-manager", - "version": "0.4.0", + "version": "0.5.0", "description": "Cache module for Node.js", "main": "index.js", "scripts": { diff --git a/test/caching.unit.js b/test/caching.unit.js index 4e00c38..b9b8a3a 100644 --- a/test/caching.unit.js +++ b/test/caching.unit.js @@ -1,3 +1,5 @@ +// TODO: These are really a mix of unit and integration tests. + var assert = require('assert'); var sinon = require('sinon'); var support = require('./support'); @@ -96,6 +98,55 @@ describe("caching", function () { }); }); + describe("reset()", function () { + var key2; + var value2; + + beforeEach(function (done) { + cache = caching({store: 'memory'}); + key = support.random.string(20); + value = support.random.string(); + + cache.set(key, value, function (err) { + check_err(err); + + key2 = support.random.string(20); + value2 = support.random.string(); + + cache.set(key, value, done); + }); + }); + + it("clears the cache", function (done) { + cache.reset(function (err) { + check_err(err); + + cache.get(key, function (err, result) { + assert.ok(!result); + + cache.get(key2, function (err, result) { + assert.ok(!result); + done(); + }); + }); + }); + }); + + it("lets us clear the cache without a callback", function (done) { + cache.reset(); + setTimeout(function () { + cache.get(key, function (err, result) { + assert.ok(!result); + + cache.get(key2, function (err, result) { + assert.ok(!result); + done(); + }); + }); + }, 10); + }); + }); + describe("wrap()", function () { describe("using memory (lru-cache) store", function () { var memory_store_stub; @@ -282,7 +333,6 @@ describe("caching", function () { }); }); }); - }); });