333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# egg-redis
 
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
 
[npm-image]: https://img.shields.io/npm/v/egg-redis.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-redis
[travis-image]: https://img.shields.io/travis/eggjs/egg-redis.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-redis
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-redis.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-redis?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-redis.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-redis
[snyk-image]: https://snyk.io/test/npm/egg-redis/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-redis
[download-image]: https://img.shields.io/npm/dm/egg-redis.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-redis
 
Redis client(support redis portocal) based on ioredis for egg framework
 
## Install
 
```bash
$ npm i egg-redis --save
```
 
redis Plugin for egg, support egg application access to redis.
 
This plugin based on [ioredis](https://github.com/luin/ioredis), if you want to know specific usage, you should refer to the document of [ioredis](https://github.com/luin/ioredis).
 
## Configuration
 
Change `${app_root}/config/plugin.js` to enable redis plugin:
 
```js
exports.redis = {
  enable: true,
  package: 'egg-redis',
};
```
 
Configure redis information in `${app_root}/config/config.default.js`:
 
**Single Client**
 
```javascript
config.redis = {
  client: {
    port: 6379,          // Redis port
    host: '127.0.0.1',   // Redis host
    password: 'auth',
    db: 0,
  },
}
```
 
**Multi Clients**
 
```javascript
config.redis = {
  clients: {
    foo: {                 // instanceName. See below
      port: 6379,          // Redis port
      host: '127.0.0.1',   // Redis host
      password: 'auth',
      db: 0,
    },
    bar: {
      port: 6379,
      host: '127.0.0.1',
      password: 'auth',
      db: 1,
    },
  }
}
```
 
**Sentinel**
 
```javascript
config.redis = {
  client: {
    sentinels: [{          // Sentinel instances
      port: 26379,         // Sentinel port
      host: '127.0.0.1',   // Sentinel host
    }],
    name: 'mymaster',      // Master name
    password: 'auth',
    db: 0
  },
}
```
 
**No password**
 
Redis support no authentication access, but we are highly recommand you to use redis `requirepass` in `redis.conf`.
 
```bash
 
$vim /etc/redis/redis.conf
 
requirepass xxxxxxxxxx  // xxxxxxxxxx is your password
 
```
 
Because it may be cause security risk, refer:
 
- https://ruby-china.org/topics/28094
- https://zhuoroger.github.io/2016/07/29/redis-sec/
 
If you want to access redis with no password, use `password: null`.
 
See [ioredis API Documentation](https://github.com/luin/ioredis/blob/master/API.md#new_Redis) for all available options.
 
### Customize `ioredis` version
 
`egg-redis` using ioredis@3 now, if you want to upgrade your ioredis, you can pass the instance by `config.redis.Redis`:
 
```js
// config/config.default.js
config.redis = {
  Redis: require('ioredis'), // customize ioredis version, only set when you needed
  client: {
    port: 6379,          // Redis port
    host: '127.0.0.1',   // Redis host
    password: 'auth',
    db: 0,
  },
}
```
 
**weakDependent**
 
```javascript
config.redis = {
  client: {
    port: 6379,          // Redis port
    host: '127.0.0.1',   // Redis host
    password: 'auth',
    db: 0,
    weakDependent: true, // this redis instance won't block app start
  },
}
```
 
## Usage
 
In controller, you can use `app.redis` to get the redis instance, check [ioredis](https://github.com/luin/ioredis#basic-usage) to see how to use.
 
```js
// app/controller/home.js
 
module.exports = app => {
  return class HomeController extends app.Controller {
    async index() {
      const { ctx, app } = this;
      // set
      await app.redis.set('foo', 'bar');
      // get
      ctx.body = await app.redis.get('foo');
    }
  };
};
```
 
### Multi Clients
 
If your Configure with multi clients, you can use `app.redis.get(instanceName)` to get the specific redis instance and use it like above.
 
```js
// app/controller/home.js
 
module.exports = app => {
  return class HomeController extends app.Controller {
    async index() {
      const { ctx, app } = this;
      // set
      await app.redis.get('instance1').set('foo', 'bar');
      // get
      ctx.body = await app.redis.get('instance1').get('foo');
    }
  };
};
```
 
 
### Clients Depend on Redis Cluster
 
Before you start to use Redis Cluster, please checkout the [document](https://redis.io/topics/cluster-tutorial) first, especially confirm `cluster-enabled yes` in Redis Cluster configuration file.
 
In controller, you also can use `app.redis` to get the redis instance based on Redis Cluster.
 
```js
 
// app/config/config.default.js
 
exports.redis = {
   client: {
     cluster: true,
     nodes: [{
       host: '127.0.0.1',
       port: '6379',
       family: 'user',
       password: 'password',
       db: 'db',
     }, {
       host: '127.0.0.1',
       port: '6380',
       family: 'user',
       password: 'password',
       db: 'db',
     }]
   },
};
 
// app/controller/home.js
 
module.exports = app => {
  return class HomeController extends app.Controller {
    async index() {
      const { ctx, app } = this;
      // set
      await app.redis.set('foo', 'bar');
      // get
      ctx.body = await app.redis.get('foo');
    }
  };
};
```
 
 
## Questions & Suggestions
 
Please open an issue [here](https://github.com/eggjs/egg/issues).
 
## License
 
[MIT](LICENSE)