From 34777d39f58b7dc0e2572b0dfa9e73d40696a8bd Mon Sep 17 00:00:00 2001 From: Adriano Raiano Date: Wed, 11 Jul 2018 11:01:53 +0200 Subject: [PATCH] parallel support with ioredis --- README.md | 2 +- lib/databases/redis.js | 64 ++++++++++++++++++++++++++++++---------- package.json | 2 +- test/sessionStoreTest.js | 3 +- 4 files changed, 53 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 9ac07e3..bc37c7d 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,7 @@ Currently these databases are supported: 2. mongodb ([node-mongodb-native] (https://github.com/mongodb/node-mongodb-native)) 3. couchdb ([cradle] (https://github.com/cloudhead/cradle)) 4. tingodb ([tingodb] (https://github.com/sergeyksv/tingodb)) -5. redis ([redis] (https://github.com/mranney/node_redis)) +5. redis ([ioredis] (https://github.com/luin/ioredis)) 6. memcached ([memjs] (https://github.com/alevy/memjs)) 7. elasticsearch ([elasticsearch] (https://github.com/elastic/elasticsearch-js)) diff --git a/lib/databases/redis.js b/lib/databases/redis.js index 18d2c71..642b8b5 100644 --- a/lib/databases/redis.js +++ b/lib/databases/redis.js @@ -3,9 +3,19 @@ var util = require('util'), use = require('../use'), _ = require('lodash'), async = require('async'), - redis = use('redis'), jsondate = require('jsondate'); +var redis, Redis; +try { + Redis = use('ioredis'); +} catch (e) { + try { + redis = use('redis'); + } catch (e2) { + throw e; + } +} + var RedisSessionStore = function (options) { options = options || {}; Session.Store.call(this, options); @@ -13,6 +23,7 @@ var RedisSessionStore = function (options) { var defaults = { host: 'localhost', port: 6379, + family: 4, prefix: 'sess', ttl: 60 * 60 * 24 * 14, // 14 days retry_strategy: function (options) { @@ -41,6 +52,16 @@ var RedisSessionStore = function (options) { } } + if (Redis) { + options.maxRetriesPerRequest = options.maxRetriesPerRequest || null; + delete options.retry_strategy; + options.reconnectOnError = options.reconnectOnError || function () { return false; }; + if (options.sentinels) { + delete options.host; + delete options.port; + } + } + this.options = options; } @@ -53,25 +74,31 @@ _.extend(RedisSessionStore.prototype, { var options = this.options; - this.client = new redis.createClient(options.port || options.socket, options.host, _.omit(options, 'prefix')); + if (Redis) { + this.client = new Redis(_.omit(options, 'prefix')); + } else { + this.client = new redis.createClient(options.port || options.socket, options.host, _.omit(options, 'prefix')); + } this.prefix = options.prefix; var calledBack = false; - if (options.password) { - this.client.auth(options.password, function(err) { - if (err && !calledBack && callback) { - calledBack = true; - if (callback) callback(err, self); - return; - } - if (err) throw err; - }); - } + if (!Redis) { + if (options.password) { + this.client.auth(options.password, function(err) { + if (err && !calledBack && callback) { + calledBack = true; + if (callback) callback(err, self); + return; + } + if (err) throw err; + }); + } - if (options.db) { - this.client.select(options.db); + if (options.db) { + this.client.select(options.db); + } } this.client.on('end', function () { @@ -79,6 +106,13 @@ _.extend(RedisSessionStore.prototype, { self.stopHeartbeat(); }); + if (Redis) { + this.client.on('close', function () { + self.disconnect(); + self.stopHeartbeat(); + }); + } + this.client.on('error', function (err) { console.log(err); @@ -88,7 +122,7 @@ _.extend(RedisSessionStore.prototype, { }); this.client.on('connect', function () { - if (options.db) { + if (options.db && !Redis) { self.client.send_anyways = true; self.client.select(options.db); self.client.send_anyways = false; diff --git a/package.json b/package.json index 1aa4fb1..61a5c5f 100644 --- a/package.json +++ b/package.json @@ -24,10 +24,10 @@ "expect.js": ">= 0.1.2", "express": ">= 2.5.0", "express-session": ">= 1.0.0", + "ioredis": ">= 3.0.0", "memjs": "0.x.x", "mocha": ">= 5.1.1", "mongodb": "2.1.x", - "redis": ">= 0.10.1", "tingodb": ">= 0.0.1" }, "description": "Sessionstore is a node.js module for multiple databases. It can be very useful if you work with express or connect.", diff --git a/test/sessionStoreTest.js b/test/sessionStoreTest.js index ec0efe8..c27a9b6 100644 --- a/test/sessionStoreTest.js +++ b/test/sessionStoreTest.js @@ -62,7 +62,8 @@ describe('SessionStore', function() { describe('with options containing a type property with the value of', function() { - var types = ['inmemory', 'mongodb', 'tingodb', 'redis', 'memcached', 'couchdb']; + // var types = ['inmemory', 'mongodb', 'tingodb', 'redis', 'memcached', 'couchdb']; + var types = ['inmemory', 'redis']; types.forEach(function(type) {