-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis.ts
More file actions
47 lines (39 loc) · 1.1 KB
/
Copy pathredis.ts
File metadata and controls
47 lines (39 loc) · 1.1 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
import Redis, { RedisOptions } from "ioredis";
import { redis } from "./configuration";
function getRedisConfiguration(): {
port: string | undefined;
host: string | undefined;
password: string | undefined;
} {
return redis;
}
export function createRedisInstance(config = getRedisConfiguration()) {
try {
const options: RedisOptions = {
host: config.host,
lazyConnect: true,
showFriendlyErrorStack: true,
enableAutoPipelining: true,
maxRetriesPerRequest: 0,
retryStrategy: (times: number) => {
if (times > 3) {
throw new Error(`[Redis] Could not connect after ${times} attempts`);
}
return Math.min(times * 200, 1000);
},
};
if (config.port) {
options.port = parseInt(config.port);
}
if (config.password) {
options.password = config.password;
}
const redis = new Redis(options);
redis.on("error", (error: unknown) => {
console.warn("[Redis] Error connecting", error);
});
return redis;
} catch (e) {
throw new Error("[Redis] Could not create a Redis instance");
}
}