Skip to content

Commit a00d91a

Browse files
authored
Move db selection to the pool and default to 0 (#51)
1 parent 8701d62 commit a00d91a

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

index.js

Lines changed: 2 additions & 13 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
/**

test/lib/redis-store-spec.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ describe('get', function () {
189189

190190
it('should return an error if there is an error acquiring a connection', function (done) {
191191
var pool = redisCache.store._pool;
192-
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
192+
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
193193
sinon.stub(pool, 'release');
194194
redisCache.get('foo', function (err) {
195-
pool.acquire.restore();
195+
pool.acquireDb.restore();
196196
pool.release.restore();
197197
assert.notEqual(err, null);
198198
done();
@@ -219,11 +219,11 @@ describe('del', function () {
219219

220220
it('should return an error if there is an error acquiring a connection', function (done) {
221221
var pool = redisCache.store._pool;
222-
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
222+
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
223223
sinon.stub(pool, 'release');
224224
redisCache.set('foo', 'bar', function () {
225225
redisCache.del('foo', function (err) {
226-
pool.acquire.restore();
226+
pool.acquireDb.restore();
227227
pool.release.restore();
228228
assert.notEqual(err, null);
229229
done();
@@ -247,10 +247,10 @@ describe('reset', function () {
247247

248248
it('should return an error if there is an error acquiring a connection', function (done) {
249249
var pool = redisCache.store._pool;
250-
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
250+
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
251251
sinon.stub(pool, 'release');
252252
redisCache.reset(function (err) {
253-
pool.acquire.restore();
253+
pool.acquireDb.restore();
254254
pool.release.restore();
255255
assert.notEqual(err, null);
256256
done();
@@ -279,11 +279,11 @@ describe('ttl', function () {
279279

280280
it('should return an error if there is an error acquiring a connection', function (done) {
281281
var pool = redisCache.store._pool;
282-
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
282+
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
283283
sinon.stub(pool, 'release');
284284
redisCache.set('foo', 'bar', function () {
285285
redisCache.ttl('foo', function (err) {
286-
pool.acquire.restore();
286+
pool.acquireDb.restore();
287287
pool.release.restore();
288288
assert.notEqual(err, null);
289289
done();
@@ -317,11 +317,11 @@ describe('keys', function () {
317317

318318
it('should return an error if there is an error acquiring a connection', function (done) {
319319
var pool = redisCache.store._pool;
320-
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
320+
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
321321
sinon.stub(pool, 'release');
322322
redisCache.set('foo', 'bar', function () {
323323
redisCache.keys('f*', function (err) {
324-
pool.acquire.restore();
324+
pool.acquireDb.restore();
325325
pool.release.restore();
326326
assert.notEqual(err, null);
327327
done();
@@ -368,10 +368,10 @@ describe('getClient', function () {
368368

369369
it('should return an error if there is an error acquiring a connection', function (done) {
370370
var pool = redisCache.store._pool;
371-
sinon.stub(pool, 'acquire').yieldsAsync('Something unexpected');
371+
sinon.stub(pool, 'acquireDb').yieldsAsync('Something unexpected');
372372
sinon.stub(pool, 'release');
373373
redisCache.store.getClient(function (err) {
374-
pool.acquire.restore();
374+
pool.acquireDb.restore();
375375
pool.release.restore();
376376
assert.notEqual(err, null);
377377
done();

0 commit comments

Comments
 (0)