-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.config.js
78 lines (69 loc) · 2.23 KB
/
redis.config.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
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
const redis = require('redis')
// const debug = require('debug')('redis')
let client
module.exports = (port = 6379, host = '127.0.0.1', options = {}) => {
client = redis.createClient(port, host, options)
client.on('error', (err) => { console.log('[REDIS]: Redis connection error.' + err) })
client.on('ready', () => { console.log('[REDIS]: Redis connection ready.') })
const f = {}
// Expose the client in the return object.
f.client = client
f.connect = (success) => {
if (client && client.connected) {
client.once('end', () => {
client = redis.createClient(port, host, options)
})
client.quit()
} else {
client = redis.createClient(port, host, options)
}
client.on('ready', () => { success.call(this, client) })
}
f.disconnect = () => {
if (client) {
client.once('end', () => {
client = null
})
client.quit()
}
}
// default expire time: 5 mins.
f.setex = (key, value) => {
value = JSON.stringify(value)
if (client && client.connected) {
client.setex(key, 5 * 60, value)
console.log(`[REDIS]: Set Key:${key}---Value:${value}.`)
} else {
console.error(`[REDIS]: no connection to redis server: ${host} is alive./n`)
console.warn(`[REDIS]: try reconnect to : ${host}./n`)
f.connect(() => {
console.log(`[REDIS]: connection successfull.`)
client.setex(key, 5 * 60, value)
console.log(`[REDIS]: Set Key:${key}---Value:${value}.`)
})
}
}
f.get = (key, callback) => {
if (client && client.connected) {
client.get(key, (err, result) => {
if (err) console.error(`[REDIS]: error when get key${key}:${err}/n`)
else {
callback.call(client, JSON.parse(result))
}
})
} else {
console.error(`[REDIS]: no connection to redis server: ${host} is alive./n`)
console.warn(`[REDIS]: try reconnect to : ${host}./n`)
f.connect(() => {
console.log(`[REDIS]: connection successfull.`)
client.get(key, (err, result) => {
if (err) console.error(`[REDIS]: error when get key${key}:${err}/n`)
else {
callback.call(client, JSON.parse(result))
}
})
})
}
}
return f
}