diff --git a/lib/stores/memory.js b/lib/stores/memory.js index 00da94f..ed834bd 100644 --- a/lib/stores/memory.js +++ b/lib/stores/memory.js @@ -4,6 +4,8 @@ var memoryStore = function(args) { args = args || {}; var self = {}; self.name = 'memory'; + self.usePromises = !args.noPromises; + var ttl = args.ttl; var lruOpts = { max: args.max || 500, @@ -27,7 +29,7 @@ var memoryStore = function(args) { lruCache.set(key, value, maxAge); if (cb) { process.nextTick(cb); - } else { + } else if (self.usePromises) { return Promise.resolve(value); } }; @@ -42,6 +44,8 @@ var memoryStore = function(args) { process.nextTick(function() { cb(null, value); }); + } else if (self.usePromises) { + return Promise.resolve(value) } else { return value; } @@ -51,9 +55,13 @@ var memoryStore = function(args) { if (typeof options === 'function') { cb = options; } + lruCache.del(key); + if (cb) { process.nextTick(cb); + } else if (self.usePromises) { + return Promise.resolve(); } }; @@ -61,6 +69,8 @@ var memoryStore = function(args) { lruCache.reset(); if (cb) { process.nextTick(cb); + } else if (self.usePromises) { + return Promise.resolve(); } }; @@ -70,6 +80,8 @@ var memoryStore = function(args) { process.nextTick(function() { cb(null, keys); }); + } else if (self.usePromises) { + return Promise.resolve(); } else { return keys; } diff --git a/package.json b/package.json index b6e10fa..e5d5a4e 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ "es6-promise": "^3.0.2", "istanbul": "^0.2.11", "jscs": "^1.9.0", - "jsdoc": "^3.3.0", "jshint": "^2.5.4", "mocha": "^1.20.1", "optimist": "^0.6.1", diff --git a/test/caching.unit.js b/test/caching.unit.js index c43ce7a..ddb10c6 100644 --- a/test/caching.unit.js +++ b/test/caching.unit.js @@ -45,6 +45,7 @@ describe("caching", function() { }); it("lets us set and get data without a callback", function(done) { + cache = caching({store: memoryStore.create({noPromises: true})}); cache.set(key, value, {ttl: defaultTtl}); setTimeout(function() { @@ -54,7 +55,19 @@ describe("caching", function() { }, 20); }); + it("lets us set and get data without a callback, returning a promise", function(done) { + cache.set(key, value, {ttl: defaultTtl}); + setTimeout(function() { + cache.get(key) + .then(function(result) { + assert.equal(result, value); + done(); + }); + }, 20); + }); + it("lets us set and get data without options object or callback", function(done) { + cache = caching({store: memoryStore.create({noPromises: true})}); cache.set(key, value); setTimeout(function() { @@ -254,6 +267,7 @@ describe("caching", function() { }); it("lets us get the keys without a callback (memory store only)", function() { + cache = caching({store: memoryStore.create({noPromises: true})}); var keys = cache.keys(); assert.deepEqual(keys.sort, savedKeys.sort); }); diff --git a/test/multi_caching.unit.js b/test/multi_caching.unit.js index 97da932..0bcc2fe 100644 --- a/test/multi_caching.unit.js +++ b/test/multi_caching.unit.js @@ -374,7 +374,10 @@ describe("multiCaching", function() { }) .then(function() { process.nextTick(function() { - assert.equal(memoryCache.get(key), value); + memoryCache.get(key) + .then(function(fetchedValue) { + assert.equal(fetchedValue, value); + }); }); }) .then(done); diff --git a/test/stores/memory.unit.js b/test/stores/memory.unit.js index ab3565d..7c95c48 100644 --- a/test/stores/memory.unit.js +++ b/test/stores/memory.unit.js @@ -1,3 +1,4 @@ +var assert = require('assert'); var support = require('../support'); var memoryStore = require('../../lib/stores/memory'); @@ -8,4 +9,21 @@ describe("memory store", function() { support.testSetGetDel(memoryCache, done); }); }); + + describe("set()", function() { + var memoryCache; + + beforeEach(function() { + memoryCache = memoryStore.create({noPromises: true}); + }); + + it("does not require a callback", function(done) { + memoryCache.set('foo', 'bar'); + + setTimeout(function() { + assert.equal(memoryCache.get('foo'), 'bar'); + done(); + }, 10); + }); + }); });