diff --git a/packages/nextjs-cache-handler/src/handlers/cache-handler.ts b/packages/nextjs-cache-handler/src/handlers/cache-handler.ts index 69615f8..039075a 100644 --- a/packages/nextjs-cache-handler/src/handlers/cache-handler.ts +++ b/packages/nextjs-cache-handler/src/handlers/cache-handler.ts @@ -649,6 +649,30 @@ export class CacheHandler implements NextCacheHandler { "Successfully created CacheHandler configuration.", ); } + + const prepareResults = await Promise.allSettled( + handlersList.map((handler) => handler.prepare?.()), + ); + + if (CacheHandler.#debug) { + prepareResults.forEach((result, index) => { + if (result.status === "rejected") { + console.warn( + "[CacheHandler] [handler: %s] [method: %s] %s", + handlersList[index]?.name ?? `unknown-${index}`, + "prepare", + `Error: ${result.reason}`, + ); + } else if (result.status === "fulfilled" && result.value !== undefined) { + console.info( + "[CacheHandler] [handler: %s] [method: %s] %s", + handlersList[index]?.name ?? `unknown-${index}`, + "prepare", + "Successfully completed preparation tasks.", + ); + } + }); + } } /** diff --git a/packages/nextjs-cache-handler/src/handlers/cache-handler.types.ts b/packages/nextjs-cache-handler/src/handlers/cache-handler.types.ts index 0cb9533..7f885b1 100644 --- a/packages/nextjs-cache-handler/src/handlers/cache-handler.types.ts +++ b/packages/nextjs-cache-handler/src/handlers/cache-handler.types.ts @@ -173,6 +173,15 @@ export type Handler = { * @returns A Promise that resolves when the cache entry has been successfully deleted. */ delete?: (key: string) => Promise; + + /** + * Prepares the handler for use, such as cleaning up expired entries or stale resources. + * This method is optional and will be automatically called by the `CacheHandler` class after handlers are configured. + * Handlers can use this to perform one-time setup tasks, remove stale entries, or prepare internal state. + * + * @returns A Promise that resolves when the preparation tasks have been completed. + */ + prepare?: () => Promise; }; /** * Represents the parameters for Time-to-Live (TTL) configuration. diff --git a/packages/nextjs-cache-handler/src/handlers/composite.ts b/packages/nextjs-cache-handler/src/handlers/composite.ts index df95231..97ff9b3 100644 --- a/packages/nextjs-cache-handler/src/handlers/composite.ts +++ b/packages/nextjs-cache-handler/src/handlers/composite.ts @@ -50,5 +50,9 @@ export default function createHandler({ async delete(key) { await Promise.all(handlers.map((handler) => handler.delete?.(key))); }, + + async prepare() { + await Promise.all(handlers.map((handler) => handler.prepare?.())); + }, }; } diff --git a/packages/nextjs-cache-handler/src/handlers/redis-strings.ts b/packages/nextjs-cache-handler/src/handlers/redis-strings.ts index 3379e7c..64ad40f 100644 --- a/packages/nextjs-cache-handler/src/handlers/redis-strings.ts +++ b/packages/nextjs-cache-handler/src/handlers/redis-strings.ts @@ -333,5 +333,8 @@ export default function createHandler({ .hDel(keyPrefix + sharedTagsTtlKey, key), ]); }, + async prepare() { + await revalidateSharedKeys(); + }, }; }