Newer
Older
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
const Redis = require("ioredis");
const printError = require('./logs.helpers.js').printError;
const printLog = require('./logs.helpers.js').printLog;
module.exports.setRedisValue = async (redisSentinelHost, redisSentinelPort, redisGroupName, key, value, ttl) => {
const redisClient = new Redis({
sentinels: [{
host: redisSentinelHost,
port: redisSentinelPort
},],
name: redisGroupName,
});
redisClient.on('error', (error) => {
printError('Redis client', error);
redisClient.disconnect();
});
// Set key value with expiration time in seconds
const res = await redisClient.set(key, value, 'EX', ttl).catch((error) => {
redisClient.disconnect();
printError('Redis client', 'Couldn\'t set redis key/value (with ttl).');
printError('Redis client', error);
return false;
});
printLog('Redis client', 'Done setting key/value pair');
redisClient.disconnect();
return res ? true : false;
};
module.exports.getRedisValue = async (redisSentinelHost, redisSentinelPort, redisGroupName, key) => {
const redisClient = new Redis({
sentinels: [{
host: redisSentinelHost,
port: redisSentinelPort
},],
name: redisGroupName,
});
redisClient.on('error', (error) => {
printError('Redis client', error);
redisClient.disconnect();
});
const res = await redisClient.get(key).catch((error) => {
redisClient.disconnect();
printError('Redis client', 'Couldn\'t get redis value.');
printError('Redis client', error);
return false;
});
printLog(`Redis client`, `Value found ${res}`);
redisClient.disconnect();
return res ? res : null;
};