-
Notifications
You must be signed in to change notification settings - Fork 3
/
cache-handler.mjs
56 lines (45 loc) · 1.34 KB
/
cache-handler.mjs
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
import { CacheHandler } from "@neshca/cache-handler"
import createLruHandler from "@neshca/cache-handler/local-lru"
import createRedisHandler from "@neshca/cache-handler/redis-stack"
import { createClient } from "redis"
CacheHandler.onCreation(async () => {
let client
try {
client = createClient({
url: process.env.REDIS_URL ?? "redis://localhost:6379",
})
client.on("error", (error) => {
console.error("Redis error:", error.message)
})
} catch (error) {
console.warn("Failed to create Redis client:", error)
}
let redisHandler
if (process.env.REDIS_AVAILABLE) {
try {
console.info("Connecting Redis client...")
await client.connect()
console.info("Redis client connected.")
redisHandler = await createRedisHandler({
client,
timeoutMs: 5000,
})
} catch (error) {
console.warn("Failed to connect Redis client:", error)
console.warn("Disconnecting the Redis client...")
client
.disconnect()
.then(() => {
console.info("Redis client disconnected.")
})
.catch(() => {
console.warn("Failed to quit the Redis client after failing to connect.")
})
}
}
const localHandler = createLruHandler()
return {
handlers: [redisHandler, localHandler],
}
})
export default CacheHandler