-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.js
executable file
·38 lines (30 loc) · 897 Bytes
/
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
// module that connects to Redis client
import { createClient } from 'redis';
import { promisify } from 'util';
class RedisClient {
constructor() {
this.client = createClient();
this.isClientConnected = true;
this.client.on('error', (err) => {
console.error('Redis client failed to connect:', err.message || err.toString());
this.isClientConnected = false;
});
this.client.on('connect', () => {
this.isClientConnected = true;
});
}
isAlive() {
return this.isClientConnected;
}
async get(key) {
return promisify(this.client.get).bind(this.client)(key);
}
async set(key, value, duration) {
await promisify(this.client.setex).bind(this.client)(key, duration, value);
}
async del(key) {
await promisify(this.client.del).bind(this.client)(key);
}
}
const redisClient = new RedisClient();
export default redisClient;