Browse Source

Merge branch 'develop' into feature/remove-domains

develop
Bryan Donovan 9 years ago
parent
commit
6dab0a1863
  1. 2
      .jscs.json
  2. 2
      .travis.yml
  3. 6
      History.md
  4. 14
      lib/stores/memory.js
  5. 18
      package.json
  6. 57
      test/caching.unit.js
  7. 5
      test/multi_caching.unit.js
  8. 26
      test/stores/memory.unit.js

2
.jscs.json

@ -95,7 +95,7 @@
"test/data/**", "test/data/**",
"test/support/fakes/**" "test/support/fakes/**"
], ],
"validateJSDoc": { "jsDoc": {
"checkParamNames": true, "checkParamNames": true,
"requireParamTypes": true "requireParamTypes": true
}, },

2
.travis.yml

@ -1,6 +1,8 @@
language: node_js language: node_js
node_js: node_js:
- '0.10' - '0.10'
- '0.12'
- '5.5'
script: 'make test-travis' script: 'make test-travis'
branches: branches:
only: only:

6
History.md

@ -1,3 +1,9 @@
- 1.5.0 2015-03-13
-npm bumps, making sure global.Promise is not defined in node 0.10 memory store test.
- 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 - 1.4.0 2016-02-03
- Passing ttl of 0 to lruCache, upgrading to lru-cache 4.0.0 - Passing ttl of 0 to lruCache, upgrading to lru-cache 4.0.0

14
lib/stores/memory.js

@ -4,6 +4,8 @@ var memoryStore = function(args) {
args = args || {}; args = args || {};
var self = {}; var self = {};
self.name = 'memory'; self.name = 'memory';
self.usePromises = (typeof Promise === 'undefined' || args.noPromises) ? false : true;
var ttl = args.ttl; var ttl = args.ttl;
var lruOpts = { var lruOpts = {
max: args.max || 500, max: args.max || 500,
@ -27,7 +29,7 @@ var memoryStore = function(args) {
lruCache.set(key, value, maxAge); lruCache.set(key, value, maxAge);
if (cb) { if (cb) {
process.nextTick(cb); process.nextTick(cb);
} else { } else if (self.usePromises) {
return Promise.resolve(value); return Promise.resolve(value);
} }
}; };
@ -42,6 +44,8 @@ var memoryStore = function(args) {
process.nextTick(function() { process.nextTick(function() {
cb(null, value); cb(null, value);
}); });
} else if (self.usePromises) {
return Promise.resolve(value);
} else { } else {
return value; return value;
} }
@ -51,9 +55,13 @@ var memoryStore = function(args) {
if (typeof options === 'function') { if (typeof options === 'function') {
cb = options; cb = options;
} }
lruCache.del(key); lruCache.del(key);
if (cb) { if (cb) {
process.nextTick(cb); process.nextTick(cb);
} else if (self.usePromises) {
return Promise.resolve();
} }
}; };
@ -61,6 +69,8 @@ var memoryStore = function(args) {
lruCache.reset(); lruCache.reset();
if (cb) { if (cb) {
process.nextTick(cb); process.nextTick(cb);
} else if (self.usePromises) {
return Promise.resolve();
} }
}; };
@ -70,6 +80,8 @@ var memoryStore = function(args) {
process.nextTick(function() { process.nextTick(function() {
cb(null, keys); cb(null, keys);
}); });
} else if (self.usePromises) {
return Promise.resolve(keys);
} else { } else {
return keys; return keys;
} }

18
package.json

@ -1,6 +1,6 @@
{ {
"name": "cache-manager", "name": "cache-manager",
"version": "1.4.0", "version": "1.5.0",
"description": "Cache module for Node.js", "description": "Cache module for Node.js",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@ -20,18 +20,18 @@
"author": "Bryan Donovan", "author": "Bryan Donovan",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"async": "^0.9.0", "async": "^1.5.2",
"lru-cache": "4.0.0" "lru-cache": "4.0.0"
}, },
"devDependencies": { "devDependencies": {
"coveralls": "^2.3.0", "coveralls": "^2.3.0",
"es6-promise": "^3.0.2", "es6-promise": "^3.0.2",
"istanbul": "^0.2.11", "istanbul": "0.4.2",
"jscs": "^1.9.0", "jscs": "2.11.0",
"jsdoc": "^3.3.0", "jsdoc": "3.3.0",
"jshint": "^2.5.4", "jshint": "2.9.1",
"mocha": "^1.20.1", "mocha": "2.4.5",
"optimist": "^0.6.1", "optimist": "0.6.1",
"sinon": "^1.10.2" "sinon": "1.17.3"
} }
} }

57
test/caching.unit.js

@ -45,6 +45,7 @@ describe("caching", function() {
}); });
it("lets us set and get data without a callback", function(done) { 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}); cache.set(key, value, {ttl: defaultTtl});
setTimeout(function() { setTimeout(function() {
@ -54,7 +55,19 @@ describe("caching", function() {
}, 20); }, 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) { 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); cache.set(key, value);
setTimeout(function() { setTimeout(function() {
@ -224,10 +237,11 @@ describe("caching", function() {
describe("keys()", function() { describe("keys()", function() {
var keyCount; var keyCount;
var savedKeys = []; var savedKeys;
beforeEach(function(done) { beforeEach(function(done) {
keyCount = 10; keyCount = 10;
savedKeys = [];
var processed = 0; var processed = 0;
cache = caching({store: 'memory'}); cache = caching({store: 'memory'});
@ -248,14 +262,47 @@ describe("caching", function() {
it("calls back with all keys in cache", function(done) { it("calls back with all keys in cache", function(done) {
cache.keys(function(err, keys) { cache.keys(function(err, keys) {
checkErr(err); checkErr(err);
assert.deepEqual(keys.sort, savedKeys.sort); assert.deepEqual(keys.sort(), savedKeys.sort());
done(); done();
}); });
}); });
it("lets us get the keys without a callback (memory store only)", function() { it("lets us set and get data without a callback, returning a promise", function(done) {
var keys = cache.keys(); cache.keys()
assert.deepEqual(keys.sort, savedKeys.sort); .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());
});
}); });
}); });

5
test/multi_caching.unit.js

@ -374,7 +374,10 @@ describe("multiCaching", function() {
}) })
.then(function() { .then(function() {
process.nextTick(function() { process.nextTick(function() {
assert.equal(memoryCache.get(key), value); memoryCache.get(key)
.then(function(fetchedValue) {
assert.equal(fetchedValue, value);
});
}); });
}) })
.then(done); .then(done);

26
test/stores/memory.unit.js

@ -1,3 +1,4 @@
var assert = require('assert');
var support = require('../support'); var support = require('../support');
var memoryStore = require('../../lib/stores/memory'); var memoryStore = require('../../lib/stores/memory');
@ -8,4 +9,29 @@ describe("memory store", function() {
support.testSetGetDel(memoryCache, done); support.testSetGetDel(memoryCache, done);
}); });
}); });
describe("set()", function() {
var memoryCache;
var origPromise;
beforeEach(function() {
origPromise = global.Promise;
delete global.Promise;
memoryCache = memoryStore.create({noPromises: true});
});
afterEach(function() {
global.Promise = origPromise;
});
// 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() {
assert.equal(memoryCache.get('foo'), 'bar');
done();
}, 10);
});
});
}); });

Loading…
Cancel
Save