redis.js
1.22 KB
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
const _ = require('lodash');
const redis = require('redis');
const bluebird = require('bluebird');
const config = require('../config/common');
const monitor = global.yoho.monitorSender;
let client;
const timeout = 200; // redis 操作超时时间
try {
client = redis.createClient(config.redis.connect);
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
const monitorType = _.get(monitor, 'type.REDIS');
client.all = args => {
if (!client.ready) {
if (Array.isArray(args)) {
return Promise.resolve(_.fill(args, false));
} else {
return Promise.resolve(false);
}
}
return client.multi.call(client, args).execAsync().timeout(timeout).then(res => {
monitor && monitor.tallySuccess(monitorType);
return res;
}).catch((e)=>{
monitor && monitor.tallyFail(monitorType, e);
return false;
});
};
client.on('error', function() {
global.yoho.redis = '';
});
client.on('connect', function() {
global.yoho.redis = client;
});
} catch (e) {
global.yoho.redis = '';
}
module.exports = client;