Skip to content

Commit

Permalink
Merge branch 'release/0.2.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Muller committed Nov 7, 2016
2 parents 8f18b34 + d819703 commit 5c7af10
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 60 deletions.
19 changes: 4 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function redisStore(args) {

redisOptions.host = redisOptions.host || '127.0.0.1';
redisOptions.port = redisOptions.port || 6379;
redisOptions.db = redisOptions.db || 0;

var pool = new RedisPool(redisOptions, poolSettings);

Expand All @@ -39,19 +40,7 @@ function redisStore(args) {
* @param {Function} cb - A callback that returns
*/
function connect(cb) {
pool.acquire(function(err, conn) {
if (err) {
pool.release(conn);
return cb(err);
}

/* istanbul ignore else */
if (args.db || args.db === 0) {
conn.select(args.db);
}

cb(null, conn);
});
pool.acquireDb(cb, redisOptions.db);
}

/**
Expand Down Expand Up @@ -277,14 +266,14 @@ function redisStore(args) {
/**
* Specify which values should and should not be cached.
* If the function returns true, it will be stored in cache.
* By default, it caches everything except null and undefined values.
* By default, it caches everything except undefined values.
* Can be overriden via standard node-cache-manager options.
* @method isCacheableValue
* @param {String} value - The value to check
* @return {Boolean} - Returns true if the value is cacheable, otherwise false.
*/
self.isCacheableValue = args.isCacheableValue || function(value) {
return value !== null && value !== undefined;
return value !== undefined;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cache-manager-redis",
"version": "0.2.4",
"version": "0.2.5",
"description": "Redis store for the node-cache-manager",
"main": "index.js",
"scripts": {
Expand Down
78 changes: 34 additions & 44 deletions test/lib/redis-store-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var assert = require('assert');

var redisCache;
var customRedisCache;
var customRedisCache2;

before(function () {
redisCache = require('cache-manager').caching({
Expand All @@ -25,20 +24,11 @@ before(function () {
ttl: config.redis.ttl,
isCacheableValue: function (val) {
// allow undefined
if (val === undefined) return true;
return redisCache.store.isCacheableValue(val);
}
});

customRedisCache2 = require('cache-manager').caching({
store: redisStore,
host: config.redis.host,
port: config.redis.port,
db: config.redis.db,
ttl: config.redis.ttl,
isCacheableValue: function (val) {
if (val === undefined)
return true;
// disallow FooBarString
if (val === 'FooBarString') return false;
else if (val === 'FooBarString')
return false;
return redisCache.store.isCacheableValue(val);
}
});
Expand Down Expand Up @@ -92,20 +82,20 @@ describe('set', function () {
done();
});
});
});

it('should store a null value without error', function (done) {
redisCache.set('foo2', null, function (err) {
try {
it('should store a null value without error', function (done) {
redisCache.set('foo2', null, function (err) {
try {
assert.equal(err, null);
redisCache.get('foo2', function (err, value) {
assert.equal(err, null);
redisCache.get('foo2', function (err, value) {
assert.equal(err, null);
assert.equal(value, null);
done();
});
} catch (e) {
done(e);
}
});
assert.equal(value, null);
done();
});
} catch (e) {
done(e);
}
});
});

Expand All @@ -130,7 +120,7 @@ describe('set', function () {
});
});

it('should store an undefined value if permitted by isCacheableValue', function (done) {
it('should store an undefined value if permitted by isCacheableValue', function (done) {
assert(customRedisCache.store.isCacheableValue(undefined), true);
customRedisCache.set('foo3', undefined, function (err) {
try {
Expand All @@ -152,8 +142,8 @@ describe('set', function () {
});

it('should not store a value disallowed by isCacheableValue', function (done) {
assert.strictEqual(customRedisCache2.store.isCacheableValue('FooBarString'), false);
customRedisCache2.set('foobar', 'FooBarString', function (err) {
assert.strictEqual(customRedisCache.store.isCacheableValue('FooBarString'), false);
customRedisCache.set('foobar', 'FooBarString', function (err) {
try {
assert.notEqual(err, null);
assert.equal(err.message, 'value cannot be FooBarString');
Expand Down Expand Up @@ -199,10 +189,10 @@ describe('get', function () {

it('should return an error if there is an error acquiring a connection', function (done) {
var pool = redisCache.store._pool;
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
sinon.stub(pool, 'release');
redisCache.get('foo', function (err) {
pool.acquire.restore();
pool.acquireDb.restore();
pool.release.restore();
assert.notEqual(err, null);
done();
Expand All @@ -229,11 +219,11 @@ describe('del', function () {

it('should return an error if there is an error acquiring a connection', function (done) {
var pool = redisCache.store._pool;
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
sinon.stub(pool, 'release');
redisCache.set('foo', 'bar', function () {
redisCache.del('foo', function (err) {
pool.acquire.restore();
pool.acquireDb.restore();
pool.release.restore();
assert.notEqual(err, null);
done();
Expand All @@ -257,10 +247,10 @@ describe('reset', function () {

it('should return an error if there is an error acquiring a connection', function (done) {
var pool = redisCache.store._pool;
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
sinon.stub(pool, 'release');
redisCache.reset(function (err) {
pool.acquire.restore();
pool.acquireDb.restore();
pool.release.restore();
assert.notEqual(err, null);
done();
Expand Down Expand Up @@ -289,11 +279,11 @@ describe('ttl', function () {

it('should return an error if there is an error acquiring a connection', function (done) {
var pool = redisCache.store._pool;
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
sinon.stub(pool, 'release');
redisCache.set('foo', 'bar', function () {
redisCache.ttl('foo', function (err) {
pool.acquire.restore();
pool.acquireDb.restore();
pool.release.restore();
assert.notEqual(err, null);
done();
Expand Down Expand Up @@ -327,11 +317,11 @@ describe('keys', function () {

it('should return an error if there is an error acquiring a connection', function (done) {
var pool = redisCache.store._pool;
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
sinon.stub(pool, 'release');
redisCache.set('foo', 'bar', function () {
redisCache.keys('f*', function (err) {
pool.acquire.restore();
pool.acquireDb.restore();
pool.release.restore();
assert.notEqual(err, null);
done();
Expand All @@ -341,16 +331,16 @@ describe('keys', function () {
});

describe('isCacheableValue', function () {
it('should return true when the value is not null or undefined', function (done) {
it('should return true when the value is not undefined', function (done) {
assert.equal(redisCache.store.isCacheableValue(0), true);
assert.equal(redisCache.store.isCacheableValue(100), true);
assert.equal(redisCache.store.isCacheableValue(''), true);
assert.equal(redisCache.store.isCacheableValue('test'), true);
assert.equal(redisCache.store.isCacheableValue(null), true);
done();
});

it('should return false when the value is null or undefined', function (done) {
assert.equal(redisCache.store.isCacheableValue(null), false);
it('should return false when the value is undefined', function (done) {
assert.equal(redisCache.store.isCacheableValue(undefined), false);
done();
});
Expand Down Expand Up @@ -378,10 +368,10 @@ describe('getClient', function () {

it('should return an error if there is an error acquiring a connection', function (done) {
var pool = redisCache.store._pool;
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
sinon.stub(pool, 'release');
redisCache.store.getClient(function (err) {
pool.acquire.restore();
pool.acquireDb.restore();
pool.release.restore();
assert.notEqual(err, null);
done();
Expand Down

0 comments on commit 5c7af10

Please sign in to comment.