|
| 1 | +import { CacheHandler } from "@neshca/cache-handler"; |
| 2 | +import createLruHandler from "@neshca/cache-handler/local-lru"; |
| 3 | +import createRedisHandler from "@neshca/cache-handler/redis-stack"; |
| 4 | +import { createClient } from "redis"; |
| 5 | + |
| 6 | +CacheHandler.onCreation(async () => { |
| 7 | + let client; |
| 8 | + |
| 9 | + try { |
| 10 | + // Create a Redis client. |
| 11 | + client = createClient({ |
| 12 | + url: process.env.REDIS_URL ?? "redis://localhost:6379", |
| 13 | + }); |
| 14 | + |
| 15 | + // Redis won't work without error handling. https://github.com/redis/node-redis?tab=readme-ov-file#events |
| 16 | + client.on("error", (error) => { |
| 17 | + if (typeof process.env.NEXT_PRIVATE_DEBUG_CACHE !== "undefined") { |
| 18 | + // Use logging with caution in production. Redis will flood your logs. Hide it behind a flag. |
| 19 | + console.error("Redis client error:", error); |
| 20 | + } |
| 21 | + }); |
| 22 | + } catch (error) { |
| 23 | + console.warn("Failed to create Redis client:", error); |
| 24 | + } |
| 25 | + |
| 26 | + if (client) { |
| 27 | + try { |
| 28 | + console.info("Connecting Redis client..."); |
| 29 | + |
| 30 | + // Wait for the client to connect. |
| 31 | + // Caveat: This will block the server from starting until the client is connected. |
| 32 | + // And there is no timeout. Make your own timeout if needed. |
| 33 | + await client.connect(); |
| 34 | + console.info("Redis client connected."); |
| 35 | + } catch (error) { |
| 36 | + console.warn("Failed to connect Redis client:", error); |
| 37 | + |
| 38 | + console.warn("Disconnecting the Redis client..."); |
| 39 | + // Try to disconnect the client to stop it from reconnecting. |
| 40 | + client |
| 41 | + .disconnect() |
| 42 | + .then(() => { |
| 43 | + console.info("Redis client disconnected."); |
| 44 | + }) |
| 45 | + .catch(() => { |
| 46 | + console.warn( |
| 47 | + "Failed to quit the Redis client after failing to connect.", |
| 48 | + ); |
| 49 | + }); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** @type {import("@neshca/cache-handler").Handler | null} */ |
| 54 | + let handler; |
| 55 | + |
| 56 | + if (client?.isReady) { |
| 57 | + // Create the `redis-stack` Handler if the client is available and connected. |
| 58 | + handler = await createRedisHandler({ |
| 59 | + client, |
| 60 | + keyPrefix: "nextjs:", |
| 61 | + timeoutMs: 1000, |
| 62 | + }); |
| 63 | + } else { |
| 64 | + // Fallback to LRU handler if Redis client is not available. |
| 65 | + // The application will still work, but the cache will be in memory only and not shared. |
| 66 | + handler = createLruHandler(); |
| 67 | + console.warn( |
| 68 | + "Falling back to LRU handler because Redis client is not available.", |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + return { |
| 73 | + handlers: [handler], |
| 74 | + }; |
| 75 | +}); |
| 76 | + |
| 77 | +export default CacheHandler; |
0 commit comments