Skip to content

Commit 5c7af10

Browse files
author
Jonathan Muller
committed
Merge branch 'release/0.2.5'
2 parents 8f18b34 + d819703 commit 5c7af10

File tree

3 files changed

+39
-60
lines changed

3 files changed

+39
-60
lines changed

index.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function redisStore(args) {
2626

2727
redisOptions.host = redisOptions.host || '127.0.0.1';
2828
redisOptions.port = redisOptions.port || 6379;
29+
redisOptions.db = redisOptions.db || 0;
2930

3031
var pool = new RedisPool(redisOptions, poolSettings);
3132

@@ -39,19 +40,7 @@ function redisStore(args) {
3940
* @param {Function} cb - A callback that returns
4041
*/
4142
function connect(cb) {
42-
pool.acquire(function(err, conn) {
43-
if (err) {
44-
pool.release(conn);
45-
return cb(err);
46-
}
47-
48-
/* istanbul ignore else */
49-
if (args.db || args.db === 0) {
50-
conn.select(args.db);
51-
}
52-
53-
cb(null, conn);
54-
});
43+
pool.acquireDb(cb, redisOptions.db);
5544
}
5645

5746
/**
@@ -277,14 +266,14 @@ function redisStore(args) {
277266
/**
278267
* Specify which values should and should not be cached.
279268
* If the function returns true, it will be stored in cache.
280-
* By default, it caches everything except null and undefined values.
269+
* By default, it caches everything except undefined values.
281270
* Can be overriden via standard node-cache-manager options.
282271
* @method isCacheableValue
283272
* @param {String} value - The value to check
284273
* @return {Boolean} - Returns true if the value is cacheable, otherwise false.
285274
*/
286275
self.isCacheableValue = args.isCacheableValue || function(value) {
287-
return value !== null && value !== undefined;
276+
return value !== undefined;
288277
};
289278

290279
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cache-manager-redis",
3-
"version": "0.2.4",
3+
"version": "0.2.5",
44
"description": "Redis store for the node-cache-manager",
55
"main": "index.js",
66
"scripts": {

test/lib/redis-store-spec.js

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var assert = require('assert');
55

66
var redisCache;
77
var customRedisCache;
8-
var customRedisCache2;
98

109
before(function () {
1110
redisCache = require('cache-manager').caching({
@@ -25,20 +24,11 @@ before(function () {
2524
ttl: config.redis.ttl,
2625
isCacheableValue: function (val) {
2726
// allow undefined
28-
if (val === undefined) return true;
29-
return redisCache.store.isCacheableValue(val);
30-
}
31-
});
32-
33-
customRedisCache2 = require('cache-manager').caching({
34-
store: redisStore,
35-
host: config.redis.host,
36-
port: config.redis.port,
37-
db: config.redis.db,
38-
ttl: config.redis.ttl,
39-
isCacheableValue: function (val) {
27+
if (val === undefined)
28+
return true;
4029
// disallow FooBarString
41-
if (val === 'FooBarString') return false;
30+
else if (val === 'FooBarString')
31+
return false;
4232
return redisCache.store.isCacheableValue(val);
4333
}
4434
});
@@ -92,20 +82,20 @@ describe('set', function () {
9282
done();
9383
});
9484
});
85+
});
9586

96-
it('should store a null value without error', function (done) {
97-
redisCache.set('foo2', null, function (err) {
98-
try {
87+
it('should store a null value without error', function (done) {
88+
redisCache.set('foo2', null, function (err) {
89+
try {
90+
assert.equal(err, null);
91+
redisCache.get('foo2', function (err, value) {
9992
assert.equal(err, null);
100-
redisCache.get('foo2', function (err, value) {
101-
assert.equal(err, null);
102-
assert.equal(value, null);
103-
done();
104-
});
105-
} catch (e) {
106-
done(e);
107-
}
108-
});
93+
assert.equal(value, null);
94+
done();
95+
});
96+
} catch (e) {
97+
done(e);
98+
}
10999
});
110100
});
111101

@@ -130,7 +120,7 @@ describe('set', function () {
130120
});
131121
});
132122

133-
it('should store an undefined value if permitted by isCacheableValue', function (done) {
123+
it('should store an undefined value if permitted by isCacheableValue', function (done) {
134124
assert(customRedisCache.store.isCacheableValue(undefined), true);
135125
customRedisCache.set('foo3', undefined, function (err) {
136126
try {
@@ -152,8 +142,8 @@ describe('set', function () {
152142
});
153143

154144
it('should not store a value disallowed by isCacheableValue', function (done) {
155-
assert.strictEqual(customRedisCache2.store.isCacheableValue('FooBarString'), false);
156-
customRedisCache2.set('foobar', 'FooBarString', function (err) {
145+
assert.strictEqual(customRedisCache.store.isCacheableValue('FooBarString'), false);
146+
customRedisCache.set('foobar', 'FooBarString', function (err) {
157147
try {
158148
assert.notEqual(err, null);
159149
assert.equal(err.message, 'value cannot be FooBarString');
@@ -199,10 +189,10 @@ describe('get', function () {
199189

200190
it('should return an error if there is an error acquiring a connection', function (done) {
201191
var pool = redisCache.store._pool;
202-
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
192+
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
203193
sinon.stub(pool, 'release');
204194
redisCache.get('foo', function (err) {
205-
pool.acquire.restore();
195+
pool.acquireDb.restore();
206196
pool.release.restore();
207197
assert.notEqual(err, null);
208198
done();
@@ -229,11 +219,11 @@ describe('del', function () {
229219

230220
it('should return an error if there is an error acquiring a connection', function (done) {
231221
var pool = redisCache.store._pool;
232-
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
222+
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
233223
sinon.stub(pool, 'release');
234224
redisCache.set('foo', 'bar', function () {
235225
redisCache.del('foo', function (err) {
236-
pool.acquire.restore();
226+
pool.acquireDb.restore();
237227
pool.release.restore();
238228
assert.notEqual(err, null);
239229
done();
@@ -257,10 +247,10 @@ describe('reset', function () {
257247

258248
it('should return an error if there is an error acquiring a connection', function (done) {
259249
var pool = redisCache.store._pool;
260-
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
250+
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
261251
sinon.stub(pool, 'release');
262252
redisCache.reset(function (err) {
263-
pool.acquire.restore();
253+
pool.acquireDb.restore();
264254
pool.release.restore();
265255
assert.notEqual(err, null);
266256
done();
@@ -289,11 +279,11 @@ describe('ttl', function () {
289279

290280
it('should return an error if there is an error acquiring a connection', function (done) {
291281
var pool = redisCache.store._pool;
292-
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
282+
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
293283
sinon.stub(pool, 'release');
294284
redisCache.set('foo', 'bar', function () {
295285
redisCache.ttl('foo', function (err) {
296-
pool.acquire.restore();
286+
pool.acquireDb.restore();
297287
pool.release.restore();
298288
assert.notEqual(err, null);
299289
done();
@@ -327,11 +317,11 @@ describe('keys', function () {
327317

328318
it('should return an error if there is an error acquiring a connection', function (done) {
329319
var pool = redisCache.store._pool;
330-
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
320+
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
331321
sinon.stub(pool, 'release');
332322
redisCache.set('foo', 'bar', function () {
333323
redisCache.keys('f*', function (err) {
334-
pool.acquire.restore();
324+
pool.acquireDb.restore();
335325
pool.release.restore();
336326
assert.notEqual(err, null);
337327
done();
@@ -341,16 +331,16 @@ describe('keys', function () {
341331
});
342332

343333
describe('isCacheableValue', function () {
344-
it('should return true when the value is not null or undefined', function (done) {
334+
it('should return true when the value is not undefined', function (done) {
345335
assert.equal(redisCache.store.isCacheableValue(0), true);
346336
assert.equal(redisCache.store.isCacheableValue(100), true);
347337
assert.equal(redisCache.store.isCacheableValue(''), true);
348338
assert.equal(redisCache.store.isCacheableValue('test'), true);
339+
assert.equal(redisCache.store.isCacheableValue(null), true);
349340
done();
350341
});
351342

352-
it('should return false when the value is null or undefined', function (done) {
353-
assert.equal(redisCache.store.isCacheableValue(null), false);
343+
it('should return false when the value is undefined', function (done) {
354344
assert.equal(redisCache.store.isCacheableValue(undefined), false);
355345
done();
356346
});
@@ -378,10 +368,10 @@ describe('getClient', function () {
378368

379369
it('should return an error if there is an error acquiring a connection', function (done) {
380370
var pool = redisCache.store._pool;
381-
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
371+
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
382372
sinon.stub(pool, 'release');
383373
redisCache.store.getClient(function (err) {
384-
pool.acquire.restore();
374+
pool.acquireDb.restore();
385375
pool.release.restore();
386376
assert.notEqual(err, null);
387377
done();

0 commit comments

Comments
 (0)