forked from JohnBatts418/bullq-experiment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.ts
54 lines (44 loc) · 1.46 KB
/
redis.ts
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
import Redis from 'ioredis';
export const REDIS_URL = '127.0.0.1:6379';
const REDIS_PASSWORD = undefined;
export const REDIS_OPTIONS: Redis.RedisOptions = {
password: REDIS_PASSWORD,
...(REDIS_URL.startsWith('rediss://') ? { tls: {} } : null),
retryStrategy: times => {
// reconnect after
return Math.min(times * 50, 2000);
},
};
export const client = new Redis(REDIS_URL, REDIS_OPTIONS);
client.on('ready', (_event: any) => {
console.info({}, 'RedisDB is connected');
});
client.on('error', (err: any) => {
console.warn({ err }, 'Redis connection error');
});
client.on('reconnecting', (err: any) => {
console.warn({ err }, 'Redis attempting to reconnect');
});
export const redisCommands = {
get: client.get.bind(client),
set: client.set.bind(client),
setex: client.setex.bind(client),
sadd: client.sadd.bind(client),
srem: client.srem.bind(client),
smembers: client.smembers.bind(client),
hset: client.hset.bind(client),
hgetall: client.hgetall.bind(client),
hget: client.hget.bind(client),
sismember: client.sismember.bind(client),
expire: client.expire.bind(client),
exists: async function (...keys: Redis.KeyType[]) {
return (await client.exists(...keys)) === 1;
}.bind(client),
del: client.del.bind(client),
rpush: client.rpush.bind(client),
incr: client.incr.bind(client),
zscore: client.zscore.bind(client),
zadd: client.zadd.bind(client),
keys: client.keys.bind(client),
flushdb: client.flushdb.bind(client),
};