Redis client(support redis portocal) based on ioredis for egg framework

14.1 安装

1
npm i egg-redis --save

14.2 配置

Change ${app_root}/config/plugin.js to enable redis plugin:

1
2
3
4
5
6
7
exports.redis = {

enable: true,

package: 'egg-redis',

};

Configure redis information in ${app_root}/config/config.default.js:
Single Client

1
2
3
4
5
6
7
8
config.redis = {
client: {
port: 6379, // Redis port
host: '127.0.0.1', // Redis host
password: 'auth',
db: 0,
}
}

14.3 使用方法

14.3.1 service

app/service/redis.js
if(this.app.redis)判断是否有启用redis

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
'use strict';

const Service = require('egg').Service;

class RedisService extends Service {
async set(key, value, seconds) {
value = JSON.stringify(value);
if (this.app.redis) {
if (!seconds) {
await this.app.redis.set(key, value);
} else {
await this.app.redis.set(key, value, 'EX', seconds);
}
}
}

async get(key) {
if (this.app.redis) {
const data = await this.app.redis.get(key);
if (!data) return;
return JSON.parse(data);
}
}
}

module.exports = RedisService;

14.3.2 controller

app/controller/default/index.js如果没有设置redis缓存,就去请求数据,再设置缓存

1
2
3
4
5
6
7
var topNav = await this.ctx.service.cache.get('index_topNav');
if (!topNav) {
topNav = await this.ctx.model.Nav.find({
"position": 1
});
await this.ctx.service.cache.set('index_topNav', topNav, 60 * 60);
}