-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.js
80 lines (74 loc) · 1.8 KB
/
redis.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
79
80
'use strict';
const pRedis = require('redis');
const Config = require('./config');
/**
* Redis class.
*
* @type {Redis}
*/
module.exports = class Redis {
/**
* Initialize a new redis connection.
*/
constructor() {
this.db = pRedis.createClient({
host: Config.redis.host,
port: Config.redis.port,
password: Config.redis.password,
retry_strategy: (options) => {
if (options.error && options.attempt > 2) {
return false;
}
}
});
this.db.on('error', (err) => {
return false;
}).on('ready', () => {
this.db.select(Config.redis.dbIndex);
});
}
/**
* Is the given key available?
*
* @param key
* @param callback
*
* @returns {boolean}
*/
/*is(key, callback) {
this.checkAsync(key).then((result) => {
callback(result);
}).catch((err) => {
callback(null);
return false;
});
}*/
/**
* Fetch the given key.
*
* @param key
*/
get(key) {
return new Promise((resolve, reject) => {
this.db.get(key, (err, reply) => {
if( err || reply === null ) {
reject(null);
} else {
this.db.quit();
resolve(reply);
}
});
});
}
/**
*
* @param key
* @param value
* @param expiration Expiration Time Default is 1 Day = 86400 Seconds
*/
set(key, value, expiration = undefined) {
expiration = expiration !== undefined ? expiration : Config.core.cacheTime;
this.db.set(key, value, 'EX', expiration);
this.db.quit();
}
};