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/support/fakes/**"
],
"validateJSDoc": {
"jsDoc": {
"checkParamNames": true,
"requireParamTypes": true
},

2
.travis.yml

@ -1,6 +1,8 @@
language: node_js
node_js:
- '0.10'
- '0.12'
- '5.5'
script: 'make test-travis'
branches:
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
- 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 || {};
var self = {};
self.name = 'memory';
self.usePromises = (typeof Promise === 'undefined' || args.noPromises) ? false : true;
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(keys);
} else {
return keys;
}

18
package.json

@ -1,6 +1,6 @@
{
"name": "cache-manager",
"version": "1.4.0",
"version": "1.5.0",
"description": "Cache module for Node.js",
"main": "index.js",
"scripts": {
@ -20,18 +20,18 @@
"author": "Bryan Donovan",
"license": "MIT",
"dependencies": {
"async": "^0.9.0",
"async": "^1.5.2",
"lru-cache": "4.0.0"
},
"devDependencies": {
"coveralls": "^2.3.0",
"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",
"sinon": "^1.10.2"
"istanbul": "0.4.2",
"jscs": "2.11.0",
"jsdoc": "3.3.0",
"jshint": "2.9.1",
"mocha": "2.4.5",
"optimist": "0.6.1",
"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) {
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() {
@ -224,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'});
@ -248,14 +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() {
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());
});
});
});

5
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);

26
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,29 @@ describe("memory store", function() {
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