-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredisClient.js
More file actions
131 lines (110 loc) · 2.62 KB
/
redisClient.js
File metadata and controls
131 lines (110 loc) · 2.62 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import dotenv from "dotenv";
import { createClient } from "redis";
import { log } from "./utils/logger.js";
dotenv.config();
const clientOptions = {
...(process.env.REDIS_URL ? { url: process.env.REDIS_URL } : {}),
socket: {
reconnectStrategy: false,
connectTimeout: 1000,
},
};
const client = createClient(clientOptions);
let redisReady = false;
let connectionAttempt;
client.on("ready", () => {
redisReady = true;
log.info("Redis connection is ready.");
});
client.on("end", () => {
redisReady = false;
log.warn("Redis connection closed.");
});
client.on("error", (error) => {
redisReady = false;
log.error(`Redis error: ${error.message}`);
connectionAttempt = null;
});
export async function connectRedis() {
if (connectionAttempt) {
return connectionAttempt;
}
const spinner = log.spinner("Connecting to Redis...");
spinner.start();
connectionAttempt = client
.connect()
.then(() => {
redisReady = true;
spinner.succeed("Connected to Redis.");
return client;
})
.catch((error) => {
redisReady = false;
connectionAttempt = null;
spinner.fail("Redis is unavailable. Continuing without cache.");
log.warn(`Redis cache disabled: ${error.message}`);
return null;
});
return connectionAttempt;
}
export function isRedisReady() {
return redisReady;
}
export async function getCacheValue(key) {
if (!redisReady) {
return null;
}
try {
return await client.get(key);
} catch (error) {
redisReady = false;
log.warn(`Failed to read Redis cache key "${key}": ${error.message}`);
return null;
}
}
export async function setCacheValue(key, ttlSeconds, value) {
if (!redisReady) {
return;
}
try {
await client.setEx(key, ttlSeconds, value);
} catch (error) {
redisReady = false;
log.warn(`Failed to write Redis cache key "${key}": ${error.message}`);
}
}
export async function deleteCacheKey(key) {
if (!redisReady) {
return;
}
try {
await client.del(key);
} catch (error) {
redisReady = false;
log.warn(`Failed to delete Redis cache key "${key}": ${error.message}`);
}
}
export async function deleteCacheByPrefix(prefix) {
if (!redisReady) {
return;
}
try {
const keys = [];
for await (const key of client.scanIterator({
MATCH: `${prefix}*`,
COUNT: 100,
})) {
keys.push(key);
}
if (keys.length > 0) {
await client.del(keys);
}
} catch (error) {
redisReady = false;
log.warn(
`Failed to delete Redis cache prefix "${prefix}": ${error.message}`,
);
}
}
await connectRedis();
export default client;