-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcache-handler.mjs
More file actions
138 lines (119 loc) · 4.11 KB
/
cache-handler.mjs
File metadata and controls
138 lines (119 loc) · 4.11 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
132
133
134
135
136
137
138
import { createClient } from "redis";
import Redis from "ioredis";
import { PHASE_PRODUCTION_BUILD } from "next/constants.js";
import { CacheHandler } from "@fortedigital/nextjs-cache-handler";
import createLruHandler from "@fortedigital/nextjs-cache-handler/local-lru";
import createRedisHandler from "@fortedigital/nextjs-cache-handler/redis-strings";
import createCompositeHandler from "@fortedigital/nextjs-cache-handler/composite";
import { ioredisAdapter } from "@fortedigital/nextjs-cache-handler/helpers/ioredisAdapter";
import { getClientInfoTag } from "@fortedigital/nextjs-cache-handler/helpers/getClientInfoTag";
const isSingleConnectionModeEnabled = !!process.env.REDIS_SINGLE_CONNECTION;
const redisType = process.env.REDIS_TYPE || "redis"; // "redis" or "ioredis"
async function setupRedisClient() {
if (PHASE_PRODUCTION_BUILD !== process.env.NEXT_PHASE) {
let redisClient;
try {
if (redisType === "ioredis") {
console.info(`Using ioredis client...`);
const ioredisClient = new Redis(process.env.REDIS_URL, {
// Set clientInfoTag for Redis driver identification
// This helps identify the framework in CLIENT LIST output
clientInfoTag: getClientInfoTag(),
});
// Wait for connection to be ready
console.info("Connecting ioredis client...");
await new Promise((resolve, reject) => {
ioredisClient.once("ready", () => {
console.info("ioredis client connected.");
resolve();
});
ioredisClient.once("error", reject);
});
redisClient = ioredisAdapter(ioredisClient);
} else {
console.info(`Using @redis/client...`);
redisClient = createClient({
url: process.env.REDIS_URL,
pingInterval: 10000,
// Set clientInfoTag for Redis driver identification
// This helps identify the framework in CLIENT LIST output
clientInfoTag: getClientInfoTag(),
});
console.info("Connecting Redis client...");
await redisClient.connect();
console.info("Redis client connected.");
}
redisClient.on("error", (e) => {
if (process.env.NEXT_PRIVATE_DEBUG_CACHE !== undefined) {
console.warn("Redis error", e);
}
if (isSingleConnectionModeEnabled) {
global.cacheHandlerConfig = null;
global.cacheHandlerConfigPromise = null;
}
});
if (!redisClient.isReady) {
console.error("Failed to initialize caching layer.");
}
return redisClient;
} catch (error) {
console.warn("Failed to connect Redis client:", error);
if (redisClient) {
try {
redisClient.destroy();
} catch (e) {
console.error(
"Failed to quit the Redis client after failing to connect.",
e
);
}
}
}
}
return null;
}
async function createCacheConfig() {
const redisClient = await setupRedisClient();
const lruCache = createLruHandler();
if (!redisClient) {
const config = { handlers: [lruCache] };
if (isSingleConnectionModeEnabled) {
global.cacheHandlerConfigPromise = null;
global.cacheHandlerConfig = config;
}
return config;
}
const redisCacheHandler = createRedisHandler({
client: redisClient,
keyPrefix: "nextjs:",
});
const config = {
handlers: [
createCompositeHandler({
handlers: [lruCache, redisCacheHandler],
setStrategy: (ctx) => (ctx?.tags.includes("memory-cache") ? 0 : 1),
}),
],
};
if (isSingleConnectionModeEnabled) {
global.cacheHandlerConfigPromise = null;
global.cacheHandlerConfig = config;
}
return config;
}
CacheHandler.onCreation(() => {
if (isSingleConnectionModeEnabled) {
if (global.cacheHandlerConfig) {
return global.cacheHandlerConfig;
}
if (global.cacheHandlerConfigPromise) {
return global.cacheHandlerConfigPromise;
}
}
const promise = createCacheConfig();
if (isSingleConnectionModeEnabled) {
global.cacheHandlerConfigPromise = promise;
}
return promise;
});
export default CacheHandler;