Skip to content

Commit

Permalink
refactor: improve Redis client error handling and fallback logic in c…
Browse files Browse the repository at this point in the history
…ache handler
  • Loading branch information
dvaJi committed Oct 11, 2024
1 parent 83635f7 commit 5d980b2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
55 changes: 28 additions & 27 deletions cache-handler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@ import createRedisHandler from "@neshca/cache-handler/redis-stack";

CacheHandler.onCreation(async () => {
let client;
// use redis client during build could cause issue https://github.com/caching-tools/next-shared-cache/issues/284#issuecomment-1919145094
if (PHASE_PRODUCTION_BUILD !== process.env.NEXT_PHASE) {
try {
// Create a Redis client.
client = createClient({
url: process.env.REDIS_URL ?? "redis://localhost:6379",
});

try {
// Create a Redis client.
client = createClient({
url: process.env.REDIS_URL ?? "redis://localhost:6379",
});

// Redis won't work without error handling. https://github.com/redis/node-redis?tab=readme-ov-file#events
client.on("error", (error) => {
if (typeof process.env.NEXT_PRIVATE_DEBUG_CACHE !== "undefined") {
// Use logging with caution in production. Redis will flood your logs. Hide it behind a flag.
console.error("Redis client error:", error);
}
});
} catch (error) {
console.warn("Failed to create Redis client:", error);
// Redis won't work without error handling.
// NB do not throw exceptions in the redis error listener,
// because it will prevent reconnection after a socket exception.
client.on("error", (e) => {
if (typeof process.env.NEXT_PRIVATE_DEBUG_CACHE !== "undefined") {
console.warn("Redis error", e);
}
});
} catch (error) {
console.warn("Failed to create Redis client:", error);
}
}

if (client) {
Expand All @@ -45,33 +48,31 @@ CacheHandler.onCreation(async () => {
})
.catch(() => {
console.warn(
"Failed to quit the Redis client after failing to connect."
"Failed to quit the Redis client after failing to connect.",
);
});
}
}

/** @type {import("@neshca/cache-handler").Handler | null} */
let handler;

let redisHandler = null;
if (client?.isReady) {
// Create the `redis-stack` Handler if the client is available and connected.
handler = await createRedisHandler({
redisHandler = await createRedisHandler({
client,
keyPrefix: "prefix:",
timeoutMs: 1000,
});
} else {
// Fallback to LRU handler if Redis client is not available.
// The application will still work, but the cache will be in memory only and not shared.
handler = createLruHandler();
console.warn(
"Falling back to LRU handler because Redis client is not available."
);
}
// Fallback to LRU handler if Redis client is not available.
// The application will still work, but the cache will be in memory only and not shared.
const LRUHandler = createLruHandler();
console.warn(
"Falling back to LRU handler because Redis client is not available.",
);

return {
handlers: [handler],
handlers: [redisHandler, LRUHandler],
};
});

Expand Down
8 changes: 4 additions & 4 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ const nextConfig = {
],
eslint: { ignoreDuringBuilds: !!process.env.CI },
typescript: { ignoreBuildErrors: !!process.env.CI },
// cacheHandler:
// !isDev && process.env.REDIS_URL
// ? require.resolve("./cache-handler.mjs")
// : undefined,
cacheHandler:
!isDev && process.env.REDIS_URL
? require.resolve("./cache-handler.mjs")
: undefined,
experimental: {
instrumentationHook: true,
},
Expand Down

0 comments on commit 5d980b2

Please sign in to comment.