-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache-handler.mjs
44 lines (37 loc) · 1.03 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
import { CacheHandler } from "@neshca/cache-handler";
import createLruHandler from "@neshca/cache-handler/local-lru";
import createRedisHandler from "@neshca/cache-handler/redis-strings";
import { createClient } from "redis";
const redisURL = process.env.REDIS_URL ?? "redis://localhost:6379/0";
const client = createClient({
url: redisURL,
name: "next-cache",
});
client.on("error", (error) => {
console.error({ title: "Redis Error", err: error, msg: error?.message });
});
CacheHandler.onCreation(async ({ buildId }) => {
let redisHandler;
if (buildId) {
await client.connect();
const keyPrefix = `next-cache-${buildId}:`;
redisHandler = createRedisHandler({
client,
timeoutMs: 5000,
keyPrefix,
});
console.info({
buildId,
redisURL,
connection: "connected",
isReady: client.isReady,
isOpen: client.isOpen,
keyPrefix,
});
}
const localHandler = createLruHandler();
return {
handlers: [redisHandler, localHandler],
};
});
export default CacheHandler;