diff --git a/README.md b/README.md index e8ec163..962da1d 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ Redis standalone caching library for Node.JS and also cache engine for [cacheman](https://github.com/cayasso/cacheman). +Edit: Implemented ioredis to the library and allowed Cluster connections. + ## Instalation ``` bash diff --git a/package.json b/package.json index 8d4f245..8c990ac 100644 --- a/package.json +++ b/package.json @@ -23,14 +23,14 @@ "devDependencies": { "mocha": "*", "pre-commit": "1.2.2", - "redis": "^2.8.0" + "ioredis": "^4.14.0" }, "dependencies": { "each": "1.2.1", "parse-redis-url": "0.0.2" }, "peerDependencies": { - "redis": "2.x" + "ioredis": "^4.14.0" }, "pre-commit": [ "test" diff --git a/src/index.js b/src/index.js index ba8d93d..b7bfd36 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,7 @@ * Module dependencies. */ -const redis = require('redis') +const Redis = require('ioredis') const parser = require('parse-redis-url') const each = require('each') @@ -12,7 +12,7 @@ const each = require('each') * Module constants. */ -const parse = parser(redis).parse +const parse = parser(Redis).parse const noop = () => {} class RedisStore { @@ -28,17 +28,19 @@ class RedisStore { options = parse(options) } - const { port, host, client, setex, password, database, prefix } = options + const { port, host, client, setex, password, database, prefix, cluster} = options if ('function' === typeof setex) { this.client = options } else if (client) { this.client = client - } else if (!port && !host) { - this.client = redis.createClient() - } else { + } else if (!port && !host && !cluster) { + this.client = new Redis(); + } else if(!cluster) { const opts = Object.assign({}, options, { prefix: null }) - this.client = redis.createClient(port, host, opts) + this.client = new Redis(port, host, opts) + }else { + this.client = new Redis.Cluster(...cluster) } if (password) { @@ -50,6 +52,7 @@ class RedisStore { if (database) { this.client.select(database, (err) => { if (err) throw err + this.client.selected_db = database }) } diff --git a/test/index.js b/test/index.js index 76ca303..88cbe89 100644 --- a/test/index.js +++ b/test/index.js @@ -1,5 +1,5 @@ const assert = require('assert') -const redis = require('redis') +const redis = require('ioredis') const Cache = require('../src') const uri = 'redis://127.0.0.1:6379/5'