Skip to content

Commit 64a22e7

Browse files
author
zhangmz
committed
add driver option to support custom redis client.
1 parent 50a553a commit 64a22e7

File tree

4 files changed

+71
-9
lines changed

4 files changed

+71
-9
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,26 @@
44

55
Fastify Redis connection plugin, with this you can share the same Redis connection in every part of your server.
66

7-
Under the hood the official [redis](https://github.com/NodeRedis/node_redis) client is used, the options that you pass to `register` will be passed to the Redis client.
7+
Under the hood the official [redis](https://github.com/NodeRedis/node_redis) client is used, the ``options`` that you pass to `register` which the ``redis`` option will be passed to the Redis client.
88

99
## Install
1010
```
1111
npm i fastify-redis --save
1212
```
1313
## Usage
14-
Add it to you project with `register` and you are done!
14+
Add it to you project with `register` and you are done!
1515
You can access the *Redis* client via `fastify.redis`.
16+
17+
And you can custom your own redis client, use ``driver`` option, now only support [ioredis](https://github.com/luin/ioredis) client, default is [redis](https://github.com/NodeRedis/node_redis).
18+
1619
```js
1720
const fastify = require('fastify')
1821

1922
fastify.register(require('fastify-redis'), {
20-
host: '127.0.0.1'
23+
driver: require('ioredis'), // when you custom your redis client. optional
24+
redis: {
25+
host: '127.0.0.1'
26+
}
2127
}, err => {
2228
if (err) throw err
2329
})

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ const redis = require('redis')
55

66
function fastifyRedis (fastify, options, next) {
77
var client = null
8-
98
try {
10-
client = redis.createClient(options)
9+
// if custom redis module, default is redis.
10+
var Redis = options.driver
11+
client = Redis ? new Redis(options.redis) : redis.createClient(options.redis)
1112
} catch (err) {
1213
return next(err)
1314
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"homepage": "https://github.com/fastify/fastify-redis#readme",
2727
"devDependencies": {
2828
"fastify": "^0.30.2",
29+
"ioredis": "^3.2.1",
2930
"standard": "^10.0.3",
3031
"tap": "^10.7.2"
3132
},

test.js

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ t.beforeEach(done => {
99
const fastify = Fastify()
1010

1111
fastify.register(fastifyRedis, {
12-
host: '127.0.0.1'
12+
redis: {
13+
host: '127.0.0.1'
14+
}
1315
}, (err) => {
1416
t.error(err)
1517
})
@@ -27,9 +29,10 @@ t.beforeEach(done => {
2729
test('fastify.redis should exist', t => {
2830
t.plan(3)
2931
const fastify = Fastify()
30-
3132
fastify.register(fastifyRedis, {
32-
host: '127.0.0.1'
33+
redis: {
34+
host: '127.0.0.1'
35+
}
3336
}, (err) => {
3437
t.error(err)
3538
})
@@ -47,7 +50,58 @@ test('fastify.redis should be the redis client', t => {
4750
const fastify = Fastify()
4851

4952
fastify.register(fastifyRedis, {
50-
host: '127.0.0.1'
53+
redis: {
54+
host: '127.0.0.1'
55+
}
56+
}, (err) => {
57+
t.error(err)
58+
})
59+
60+
fastify.ready(err => {
61+
t.error(err)
62+
63+
fastify.redis.set('key', 'value', err => {
64+
t.error(err)
65+
fastify.redis.get('key', (err, val) => {
66+
t.error(err)
67+
t.equal(val, 'value')
68+
69+
fastify.close()
70+
})
71+
})
72+
})
73+
})
74+
75+
test('fastify.redis should exist when use the custom redis driver', t => {
76+
t.plan(3)
77+
const fastify = Fastify()
78+
79+
fastify.register(fastifyRedis, {
80+
driver: require('ioredis'),
81+
redis: {
82+
host: '127.0.0.1'
83+
}
84+
}, (err) => {
85+
t.error(err)
86+
})
87+
88+
fastify.ready(err => {
89+
t.error(err)
90+
t.ok(fastify.redis)
91+
92+
fastify.close()
93+
})
94+
})
95+
96+
test('fastify.redis should be the redis client when use the custom redis driver', t => {
97+
t.plan(5)
98+
const fastify = Fastify()
99+
100+
fastify.register(fastifyRedis, {
101+
driver: require('ioredis'),
102+
redis: {
103+
host: '127.0.0.1'
104+
}
51105
}, (err) => {
52106
t.error(err)
53107
})

0 commit comments

Comments
 (0)