-
Notifications
You must be signed in to change notification settings - Fork 26
/
cache.js
36 lines (31 loc) · 898 Bytes
/
cache.js
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
/*
* @Author: Sky.Sun
* @Date: 2018-02-07 16:36:29
* @Last Modified by: Sky.Sun
* @Last Modified time: 2019-05-14 10:33:53
*/
const Redis = require('ioredis');
const configPath = require('./getConfigPath')();
const config = require(configPath);
const redisConnect = config.db.redisConnect;
const serverlog = require('serverlog-node');
const logger = serverlog.getLogger('redis');
let redis;
if (Array.isArray(redisConnect)) {
// 如果配成数组,则认为是集群模式
redis = new Redis.Cluster(redisConnect, {
enableOfflineQueue: false
});
} else {
redis = new Redis('redis://127.0.0.1:6379');
}
redis.on('connect', () => {
logger.info('Redis connected!');
});
redis.on('error', err => {
logger.error('Redis connect error:', err);
});
redis.on('end', () => {
logger.error('Redis connection closed!');
});
module.exports.cacheClient = redis;