From f85538f0e4198e481938ae5734926a1917f4c124 Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Thu, 3 Mar 2016 15:38:19 -0800 Subject: [PATCH 1/4] support noPromises option in memory store (#46) --- lib/stores/memory.js | 14 +++++++++++++- package.json | 1 - test/caching.unit.js | 14 ++++++++++++++ test/multi_caching.unit.js | 5 ++++- test/stores/memory.unit.js | 18 ++++++++++++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) 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); + }); + }); }); From a4b9ca7140d13b27d13eeeec8bc14f305c481ebc Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Thu, 3 Mar 2016 16:00:48 -0800 Subject: [PATCH 2/4] minor fixes in memory store for promise support --- lib/stores/memory.js | 4 ++-- package.json | 1 + test/caching.unit.js | 45 +++++++++++++++++++++++++++++++++----- test/stores/memory.unit.js | 3 ++- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/lib/stores/memory.js b/lib/stores/memory.js index ed834bd..ed5b94c 100644 --- a/lib/stores/memory.js +++ b/lib/stores/memory.js @@ -45,7 +45,7 @@ var memoryStore = function(args) { cb(null, value); }); } else if (self.usePromises) { - return Promise.resolve(value) + return Promise.resolve(value); } else { return value; } @@ -81,7 +81,7 @@ var memoryStore = function(args) { cb(null, keys); }); } else if (self.usePromises) { - return Promise.resolve(); + return Promise.resolve(keys); } else { return keys; } diff --git a/package.json b/package.json index e5d5a4e..b6e10fa 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "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 ddb10c6..67dfcbb 100644 --- a/test/caching.unit.js +++ b/test/caching.unit.js @@ -237,10 +237,11 @@ describe("caching", function() { describe("keys()", function() { var keyCount; - var savedKeys = []; + var savedKeys; beforeEach(function(done) { keyCount = 10; + savedKeys = []; var processed = 0; cache = caching({store: 'memory'}); @@ -261,15 +262,47 @@ describe("caching", function() { it("calls back with all keys in cache", function(done) { cache.keys(function(err, keys) { checkErr(err); - assert.deepEqual(keys.sort, savedKeys.sort); + assert.deepEqual(keys.sort(), savedKeys.sort()); done(); }); }); - 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); + it("lets us set and get data without a callback, returning a promise", function(done) { + cache.keys() + .then(function(keys) { + assert.deepEqual(keys.sort(), savedKeys.sort()); + done(); + }) + .catch(function(err) { + done(err); + }); + }); + + context("when not using promises", function() { + beforeEach(function(done) { + savedKeys = []; + keyCount = 10; + var processed = 0; + + cache = caching({store: memoryStore.create({noPromises: true})}); + + function isDone() { + return processed === keyCount; + } + + async.until(isDone, function(cb) { + processed += 1; + key = support.random.string(20); + savedKeys.push(key); + value = support.random.string(); + cache.set(key, value, cb); + }, done); + }); + + it("lets us get the keys without a callback (memory store only)", function() { + var keys = cache.keys(); + assert.deepEqual(keys.sort(), savedKeys.sort()); + }); }); }); diff --git a/test/stores/memory.unit.js b/test/stores/memory.unit.js index 7c95c48..b031b78 100644 --- a/test/stores/memory.unit.js +++ b/test/stores/memory.unit.js @@ -17,7 +17,8 @@ describe("memory store", function() { memoryCache = memoryStore.create({noPromises: true}); }); - it("does not require a callback", function(done) { + // This test should pass in node v0.10.x: + it("does not require a callback or use of Promises", function(done) { memoryCache.set('foo', 'bar'); setTimeout(function() { From 267862be72a5b1d3ff7b40b7fe001f4340bc588e Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Sun, 13 Mar 2016 13:56:49 -0700 Subject: [PATCH 3/4] checking if Promise is defined before trying to use it --- lib/stores/memory.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/stores/memory.js b/lib/stores/memory.js index ed5b94c..dad43dc 100644 --- a/lib/stores/memory.js +++ b/lib/stores/memory.js @@ -4,7 +4,7 @@ var memoryStore = function(args) { args = args || {}; var self = {}; self.name = 'memory'; - self.usePromises = !args.noPromises; + self.usePromises = (typeof Promise === 'undefined' || args.noPromises) ? false : true; var ttl = args.ttl; var lruOpts = { From 7309dab5696bfdacad28f69043f9f3b2969c4fa5 Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Sun, 13 Mar 2016 13:59:08 -0700 Subject: [PATCH 4/4] 1.4.1 --- History.md | 3 +++ package.json | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index a8dd962..c0b495d 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,6 @@ +- 1.4.1 2016-03-13 + - Fixing backward-compatibility Promise issue with node 0.10.x in memory store. + - 1.4.0 2016-02-03 - Passing ttl of 0 to lruCache, upgrading to lru-cache 4.0.0 diff --git a/package.json b/package.json index b6e10fa..045b42b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cache-manager", - "version": "1.4.0", + "version": "1.4.1", "description": "Cache module for Node.js", "main": "index.js", "scripts": {