|
| 1 | +import { createClient } from "redis"; |
| 2 | +import { PHASE_PRODUCTION_BUILD } from "next/constants.js"; |
| 3 | +import { CacheHandler } from "@fortedigital/nextjs-cache-handler"; |
| 4 | +import createLruHandler from "@fortedigital/nextjs-cache-handler/local-lru"; |
| 5 | +import createRedisHandler from "@fortedigital/nextjs-cache-handler/redis-strings"; |
| 6 | +import createCompositeHandler from "@fortedigital/nextjs-cache-handler/composite"; |
| 7 | + |
| 8 | +const isSingleConnectionModeEnabled = !!process.env.REDIS_SINGLE_CONNECTION; |
| 9 | + |
| 10 | +async function setupRedisClient() { |
| 11 | + if (PHASE_PRODUCTION_BUILD !== process.env.NEXT_PHASE) { |
| 12 | + try { |
| 13 | + const redisClient = createClient({ |
| 14 | + url: process.env.REDIS_URL, |
| 15 | + pingInterval: 10000, |
| 16 | + }); |
| 17 | + |
| 18 | + redisClient.on("error", (e) => { |
| 19 | + if (process.env.NEXT_PRIVATE_DEBUG_CACHE !== undefined) { |
| 20 | + console.warn("Redis error", e); |
| 21 | + } |
| 22 | + if (isSingleConnectionModeEnabled) { |
| 23 | + global.cacheHandlerConfig = null; |
| 24 | + global.cacheHandlerConfigPromise = null; |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + console.info("Connecting Redis client..."); |
| 29 | + await redisClient.connect(); |
| 30 | + console.info("Redis client connected."); |
| 31 | + |
| 32 | + if (!redisClient.isReady) { |
| 33 | + console.error("Failed to initialize caching layer."); |
| 34 | + } |
| 35 | + |
| 36 | + return redisClient; |
| 37 | + } catch (error) { |
| 38 | + console.warn("Failed to connect Redis client:", error); |
| 39 | + if (redisClient) { |
| 40 | + try { |
| 41 | + redisClient.destroy(); |
| 42 | + } catch (e) { |
| 43 | + console.error( |
| 44 | + "Failed to quit the Redis client after failing to connect.", |
| 45 | + e |
| 46 | + ); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return null; |
| 53 | +} |
| 54 | + |
| 55 | +async function createCacheConfig() { |
| 56 | + const redisClient = await setupRedisClient(); |
| 57 | + const lruCache = createLruHandler(); |
| 58 | + |
| 59 | + if (!redisClient) { |
| 60 | + const config = { handlers: [lruCache] }; |
| 61 | + if (isSingleConnectionModeEnabled) { |
| 62 | + global.cacheHandlerConfigPromise = null; |
| 63 | + global.cacheHandlerConfig = config; |
| 64 | + } |
| 65 | + return config; |
| 66 | + } |
| 67 | + |
| 68 | + const redisCacheHandler = createRedisHandler({ |
| 69 | + client: redisClient, |
| 70 | + keyPrefix: "nextjs:", |
| 71 | + }); |
| 72 | + |
| 73 | + const config = { |
| 74 | + handlers: [ |
| 75 | + createCompositeHandler({ |
| 76 | + handlers: [lruCache, redisCacheHandler], |
| 77 | + setStrategy: (ctx) => (ctx?.tags.includes("memory-cache") ? 0 : 1), |
| 78 | + }), |
| 79 | + ], |
| 80 | + }; |
| 81 | + |
| 82 | + if (isSingleConnectionModeEnabled) { |
| 83 | + global.cacheHandlerConfigPromise = null; |
| 84 | + global.cacheHandlerConfig = config; |
| 85 | + } |
| 86 | + |
| 87 | + return config; |
| 88 | +} |
| 89 | + |
| 90 | +CacheHandler.onCreation(() => { |
| 91 | + if (isSingleConnectionModeEnabled) { |
| 92 | + if (global.cacheHandlerConfig) { |
| 93 | + return global.cacheHandlerConfig; |
| 94 | + } |
| 95 | + if (global.cacheHandlerConfigPromise) { |
| 96 | + return global.cacheHandlerConfigPromise; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if (process.env.NODE_ENV === "development") { |
| 101 | + const config = { handlers: [createLruHandler()] }; |
| 102 | + if (isSingleConnectionModeEnabled) { |
| 103 | + global.cacheHandlerConfig = config; |
| 104 | + } |
| 105 | + return config; |
| 106 | + } |
| 107 | + |
| 108 | + const promise = createCacheConfig(); |
| 109 | + if (isSingleConnectionModeEnabled) { |
| 110 | + global.cacheHandlerConfigPromise = promise; |
| 111 | + } |
| 112 | + return promise; |
| 113 | +}); |
| 114 | + |
| 115 | +export default CacheHandler; |
0 commit comments