-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathredis.js
More file actions
94 lines (81 loc) · 2.08 KB
/
redis.js
File metadata and controls
94 lines (81 loc) · 2.08 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import Redis from 'ioredis';;
export const client = new Redis({
host: process.env.REDIS_HOST || '127.0.0.1',
port: process.env.REDIS_PORT || 6379,
password: process.env.REDIS_PASS || '',
db: 0,
});
export const lockQueueClient = new Redis({
host: process.env.REDIS_HOST2 || '127.0.0.1',
port: process.env.REDIS_PORT2 || 6379,
password: process.env.REDIS_PASS2 || '',
db: 1,
});
export const omeClient = process.env.REDIS_HOST3
? new Redis({
host: process.env.REDIS_HOST3 || '127.0.0.1',
port: process.env.REDIS_PORT3 || 6379,
password: process.env.REDIS_PASS3 || '',
db: 0,
}) : null;
export function close () {
client.quit();
lockQueueClient.quit();
omeClient?.quit();
}
//set value with expiry
export function setex (key, expiry, value) {
client.set(key, value)
.then(() => client.expire(key, expiry));
}
//get a value with key
export function get (key) {
return client.get(key).then(res => { return JSON.parse(res); });
}
//get a hash value
export function hgetall (key) {
return client.hgetall(key).then(res => { return res; });
}
//get a hash value
export function hget (key, hash) {
return client.hget(key, hash).then(res => { return JSON.parse(res); });
}
//set a hash value
export function hset (key, hash, value) {
return client.hset(key, hash, JSON.stringify(value));
}
//delete a hash
export function hdel (key, hash) {
return client.hdel(key, hash);
}
//set a value on key
export function set (key, value) {
return client.set(key, JSON.stringify(value));
}
//delete value with key
export function del (keyOrKeys) {
if (Array.isArray(keyOrKeys)) {
return client.del(...keyOrKeys);
} else {
return client.del(keyOrKeys);
}
}
export function getKeysPattern (redisClient, pattern) {
return new Promise((resolve, reject) => {
const stream = redisClient.scanStream({
match: pattern,
count: 20,
});
let allKeys = [];
stream.on('data', (keys) => {
if (!keys || keys.length === 0) { return; }
allKeys = allKeys.concat(keys);
});
stream.on('end', async () => {
resolve(allKeys);
});
stream.on('error', (err) => {
reject(err);
});
});
}