From dd591daab9539e6ba96da1d1c493a1e771ba272d Mon Sep 17 00:00:00 2001 From: Tilman Marquart Date: Tue, 10 Jun 2025 17:22:11 +0200 Subject: [PATCH 01/15] feat: improve error handling --- README.md | 35 +- src/RedisStringsHandler.ts | 873 ++++++++++++++++++++++--------------- src/SyncedMap.ts | 17 +- 3 files changed, 550 insertions(+), 375 deletions(-) diff --git a/README.md b/README.md index ad2210b..60f622e 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,10 @@ Furthermore there exists the DEBUG_CACHE_HANDLER environment variable to enable There exists also the SKIP_KEYSPACE_CONFIG_CHECK environment variable to skip the check for the keyspace configuration. This is useful if you are using redis in a cloud environment that forbids access to config commands. If you set SKIP_KEYSPACE_CONFIG_CHECK=true the check will be skipped and the keyspace configuration will be assumed to be correct (e.g. notify-keyspace-events Exe). +KILL_CONTAINER_ON_ERROR_THRESHOLD: Optional environment variable that defines how many Redis client errors should occur before the process exits with code 1. This is useful in container environments like Kubernetes where you want the container to restart if Redis connectivity issues persist. Set to 0 (default) to disable this feature. For example, setting KILL_CONTAINER_ON_ERROR_THRESHOLD=10 will exit the process after 10 Redis client errors, allowing the container orchestrator to restart the container. + +REDIS_COMMAND_TIMEOUT_MS: Optional environment variable that sets the timeout in milliseconds for Redis commands. If not set, defaults to 5000ms (5 seconds). The value is parsed as an integer, and if parsing fails, falls back to the 5000ms default. + ### Option A: minimum implementation with default options extend `next.config.js` with: @@ -119,21 +123,22 @@ A working example of above can be found in the `test/integration/next-app-custom ## Available Options -| Option | Description | Default Value | -| ---------------------- | ----------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| redisUrl | Redis connection url | `process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST ? redis://${process.env.REDISHOST}:${process.env.REDISPORT} : 'redis://localhost:6379'` | -| database | Redis database number to use. Uses DB 0 for production, DB 1 otherwise | `process.env.VERCEL_ENV === 'production' ? 0 : 1` | -| keyPrefix | Prefix added to all Redis keys | `process.env.VERCEL_URL \|\| 'UNDEFINED_URL_'` | -| sharedTagsKey | Key used to store shared tags hash map in Redis | `'__sharedTags__'` | -| timeoutMs | Timeout in milliseconds for Redis operations | `5000` | -| revalidateTagQuerySize | Number of entries to query in one batch during full sync of shared tags hash map | `250` | -| avgResyncIntervalMs | Average interval in milliseconds between tag map full re-syncs | `3600000` (1 hour) | -| redisGetDeduplication | Enable deduplication of Redis get requests via internal in-memory cache. | `true` | -| inMemoryCachingTime | Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely. | `10000` | -| defaultStaleAge | Default stale age in seconds for cached items | `1209600` (14 days) | -| estimateExpireAge | Function to calculate expire age (redis TTL value) from stale age | Production: `staleAge * 2`
Other: `staleAge * 1.2` | -| socketOptions | Redis client socket options for TLS/SSL configuration (e.g., `{ tls: true, rejectUnauthorized: false }`) | `undefined` | -| clientOptions | Additional Redis client options (e.g., username, password) | `undefined` | +| Option | Description | Default Value | +| ----------------------------- | ----------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| redisUrl | Redis connection url | `process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST ? redis://${process.env.REDISHOST}:${process.env.REDISPORT} : 'redis://localhost:6379'` | +| database | Redis database number to use. Uses DB 0 for production, DB 1 otherwise | `process.env.VERCEL_ENV === 'production' ? 0 : 1` | +| keyPrefix | Prefix added to all Redis keys | `process.env.VERCEL_URL \|\| 'UNDEFINED_URL_'` | +| sharedTagsKey | Key used to store shared tags hash map in Redis | `'__sharedTags__'` | +| timeoutMs | Timeout in milliseconds for Redis operations | `Number.parseInt(process.env.REDIS_COMMAND_TIMEOUT_MS) ?? 5_000 : 5_000` | +| revalidateTagQuerySize | Number of entries to query in one batch during full sync of shared tags hash map | `250` | +| avgResyncIntervalMs | Average interval in milliseconds between tag map full re-syncs | `3600000` (1 hour) | +| redisGetDeduplication | Enable deduplication of Redis get requests via internal in-memory cache. | `true` | +| inMemoryCachingTime | Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely. | `10000` | +| defaultStaleAge | Default stale age in seconds for cached items | `1209600` (14 days) | +| estimateExpireAge | Function to calculate expire age (redis TTL value) from stale age | Production: `staleAge * 2`
Other: `staleAge * 1.2` | +| socketOptions | Redis client socket options for TLS/SSL configuration (e.g., `{ tls: true, rejectUnauthorized: false }`) | `{ connectTimeout: timeoutMs }` | +| clientOptions | Additional Redis client options (e.g., username, password) | `undefined` | +| killContainerOnErrorThreshold | Number of consecutive errors before the container is killed. Set to 0 to disable. | `Number.parseInt(process.env.KILL_CONTAINER_ON_ERROR_THRESHOLD) ?? 0 : 0` | ## TLS Configuration diff --git a/src/RedisStringsHandler.ts b/src/RedisStringsHandler.ts index a995507..846b548 100644 --- a/src/RedisStringsHandler.ts +++ b/src/RedisStringsHandler.ts @@ -60,6 +60,10 @@ export type CreateRedisStringsHandlerOptions = { * @default Production: staleAge * 2, Other: staleAge * 1.2 */ estimateExpireAge?: (staleAge: number) => number; + /** Kill container on Redis client error if error threshold is reached + * @default 0 (0 means no error threshold) + */ + killContainerOnErrorThreshold?: number; /** Additional Redis client socket options * @example { tls: true, rejectUnauthorized: false } */ @@ -84,6 +88,7 @@ export function getTimeoutRedisCommandOptions( return commandOptions({ signal: AbortSignal.timeout(timeoutMs) }); } +let killContainerOnErrorCount: number = 0; export default class RedisStringsHandler { private client: Client; private sharedTagsMap: SyncedMap; @@ -103,6 +108,7 @@ export default class RedisStringsHandler { private inMemoryCachingTime: number; private defaultStaleAge: number; private estimateExpireAge: (staleAge: number) => number; + private killContainerOnErrorThreshold: number; constructor({ redisUrl = process.env.REDIS_URL @@ -113,7 +119,9 @@ export default class RedisStringsHandler { database = process.env.VERCEL_ENV === 'production' ? 0 : 1, keyPrefix = process.env.VERCEL_URL || 'UNDEFINED_URL_', sharedTagsKey = '__sharedTags__', - timeoutMs = 5_000, + timeoutMs = process.env.REDIS_COMMAND_TIMEOUT_MS + ? (Number.parseInt(process.env.REDIS_COMMAND_TIMEOUT_MS) ?? 5_000) + : 5_000, revalidateTagQuerySize = 250, avgResyncIntervalMs = 60 * 60 * 1_000, redisGetDeduplication = true, @@ -121,111 +129,189 @@ export default class RedisStringsHandler { defaultStaleAge = 60 * 60 * 24 * 14, estimateExpireAge = (staleAge) => process.env.VERCEL_ENV === 'production' ? staleAge * 2 : staleAge * 1.2, + killContainerOnErrorThreshold = process.env + .KILL_CONTAINER_ON_ERROR_THRESHOLD + ? (Number.parseInt(process.env.KILL_CONTAINER_ON_ERROR_THRESHOLD) ?? 0) + : 0, socketOptions, clientOptions, }: CreateRedisStringsHandlerOptions) { - this.keyPrefix = keyPrefix; - this.timeoutMs = timeoutMs; - this.redisGetDeduplication = redisGetDeduplication; - this.inMemoryCachingTime = inMemoryCachingTime; - this.defaultStaleAge = defaultStaleAge; - this.estimateExpireAge = estimateExpireAge; - try { - // Create Redis client with properly typed configuration - this.client = createClient({ - url: redisUrl, - ...(database !== 0 ? { database } : {}), - ...(socketOptions ? { socket: socketOptions } : {}), - ...(clientOptions || {}), + this.keyPrefix = keyPrefix; + this.timeoutMs = timeoutMs; + this.redisGetDeduplication = redisGetDeduplication; + this.inMemoryCachingTime = inMemoryCachingTime; + this.defaultStaleAge = defaultStaleAge; + this.estimateExpireAge = estimateExpireAge; + this.killContainerOnErrorThreshold = killContainerOnErrorThreshold; + + try { + // Create Redis client with properly typed configuration + this.client = createClient({ + url: redisUrl, + pingInterval: 5000, // Useful with Redis deployments that do not use TCP Keep-Alive. Restarts the connection if it is idle for too long. + ...(database !== 0 ? { database } : {}), + ...(socketOptions + ? { socket: { connectTimeout: timeoutMs, ...socketOptions } } + : { connectTimeout: timeoutMs }), + ...(clientOptions || {}), + }); + + this.client.on('error', (error) => { + console.error( + 'Redis client error', + error, + killContainerOnErrorCount++, + ); + setTimeout( + () => + this.client.connect().catch((error) => { + console.error( + 'Failed to reconnect Redis client after connection loss:', + error, + ); + }), + 1000, + ); + if ( + this.killContainerOnErrorThreshold > 0 && + killContainerOnErrorCount >= this.killContainerOnErrorThreshold + ) { + console.error( + 'Redis client error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)', + error, + killContainerOnErrorCount++, + ); + this.client.disconnect(); + this.client.quit(); + setTimeout(() => { + process.exit(1); + }, 500); + } + }); + + this.client + .connect() + .then(() => { + console.info('Redis client connected.'); + }) + .catch(() => { + this.client.connect().catch((error) => { + console.error('Failed to connect Redis client:', error); + this.client.disconnect(); + throw error; + }); + }); + } catch (error: unknown) { + console.error('Failed to initialize Redis client'); + throw error; + } + + const filterKeys = (key: string): boolean => + key !== REVALIDATED_TAGS_KEY && key !== sharedTagsKey; + + this.sharedTagsMap = new SyncedMap({ + client: this.client, + keyPrefix, + redisKey: sharedTagsKey, + database, + timeoutMs, + querySize: revalidateTagQuerySize, + filterKeys, + resyncIntervalMs: + avgResyncIntervalMs - + avgResyncIntervalMs / 10 + + Math.random() * (avgResyncIntervalMs / 10), }); - this.client.on('error', (error) => { - console.error('Redis client error', error); + this.revalidatedTagsMap = new SyncedMap({ + client: this.client, + keyPrefix, + redisKey: REVALIDATED_TAGS_KEY, + database, + timeoutMs, + querySize: revalidateTagQuerySize, + filterKeys, + resyncIntervalMs: + avgResyncIntervalMs + + avgResyncIntervalMs / 10 + + Math.random() * (avgResyncIntervalMs / 10), }); - this.client - .connect() - .then(() => { - console.info('Redis client connected.'); - }) - .catch(() => { - this.client.connect().catch((error) => { - console.error('Failed to connect Redis client:', error); - this.client.disconnect(); - throw error; - }); - }); - } catch (error: unknown) { - console.error('Failed to initialize Redis client'); + this.inMemoryDeduplicationCache = new SyncedMap({ + client: this.client, + keyPrefix, + redisKey: 'inMemoryDeduplicationCache', + database, + timeoutMs, + querySize: revalidateTagQuerySize, + filterKeys, + customizedSync: { + withoutRedisHashmap: true, + withoutSetSync: true, + }, + }); + + const redisGet: Client['get'] = this.client.get.bind(this.client); + this.redisDeduplicationHandler = new DeduplicatedRequestHandler( + redisGet, + inMemoryCachingTime, + this.inMemoryDeduplicationCache, + ); + this.redisGet = redisGet; + this.deduplicatedRedisGet = + this.redisDeduplicationHandler.deduplicatedFunction; + } catch (error) { + console.error( + 'RedisStringsHandler constructor error', + error, + killContainerOnErrorCount++, + ); + if ( + killContainerOnErrorThreshold > 0 && + killContainerOnErrorCount >= killContainerOnErrorThreshold + ) { + console.error( + 'RedisStringsHandler constructor error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)', + error, + killContainerOnErrorCount++, + ); + process.exit(1); + } throw error; } - - const filterKeys = (key: string): boolean => - key !== REVALIDATED_TAGS_KEY && key !== sharedTagsKey; - - this.sharedTagsMap = new SyncedMap({ - client: this.client, - keyPrefix, - redisKey: sharedTagsKey, - database, - timeoutMs, - querySize: revalidateTagQuerySize, - filterKeys, - resyncIntervalMs: - avgResyncIntervalMs - - avgResyncIntervalMs / 10 + - Math.random() * (avgResyncIntervalMs / 10), - }); - - this.revalidatedTagsMap = new SyncedMap({ - client: this.client, - keyPrefix, - redisKey: REVALIDATED_TAGS_KEY, - database, - timeoutMs, - querySize: revalidateTagQuerySize, - filterKeys, - resyncIntervalMs: - avgResyncIntervalMs + - avgResyncIntervalMs / 10 + - Math.random() * (avgResyncIntervalMs / 10), - }); - - this.inMemoryDeduplicationCache = new SyncedMap({ - client: this.client, - keyPrefix, - redisKey: 'inMemoryDeduplicationCache', - database, - timeoutMs, - querySize: revalidateTagQuerySize, - filterKeys, - customizedSync: { - withoutRedisHashmap: true, - withoutSetSync: true, - }, - }); - - const redisGet: Client['get'] = this.client.get.bind(this.client); - this.redisDeduplicationHandler = new DeduplicatedRequestHandler( - redisGet, - inMemoryCachingTime, - this.inMemoryDeduplicationCache, - ); - this.redisGet = redisGet; - this.deduplicatedRedisGet = - this.redisDeduplicationHandler.deduplicatedFunction; } resetRequestCache(): void {} + private clientReadyCalls = 0; + private async assertClientIsReady(): Promise { - await Promise.all([ - this.sharedTagsMap.waitUntilReady(), - this.revalidatedTagsMap.waitUntilReady(), + if (this.clientReadyCalls > 10) { + throw new Error( + 'assertClientIsReady called more than 10 times without being ready.', + ); + } + await Promise.race([ + Promise.all([ + this.sharedTagsMap.waitUntilReady(), + this.revalidatedTagsMap.waitUntilReady(), + ]), + new Promise((_, reject) => + setTimeout(() => { + reject( + new Error( + 'assertClientIsReady: Timeout waiting for Redis maps to be ready', + ), + ); + }, this.timeoutMs * 5), + ), ]); + this.clientReadyCalls = 0; if (!this.client.isReady) { - throw new Error('Redis client is not ready yet or connection is lost.'); + throw new Error( + 'assertClientIsReady: Redis client is not ready yet or connection is lost.', + ); } } @@ -247,139 +333,170 @@ export default class RedisStringsHandler { isFallback: boolean; }, ): Promise { - if ( - ctx.kind !== 'APP_ROUTE' && - ctx.kind !== 'APP_PAGE' && - ctx.kind !== 'FETCH' - ) { - console.warn( - 'RedisStringsHandler.get() called with', - key, - ctx, - ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ', - (ctx as { kind: string })?.kind, - ); - } - - debug('green', 'RedisStringsHandler.get() called with', key, ctx); - await this.assertClientIsReady(); - - const clientGet = this.redisGetDeduplication - ? this.deduplicatedRedisGet(key) - : this.redisGet; - const serializedCacheEntry = await clientGet( - getTimeoutRedisCommandOptions(this.timeoutMs), - this.keyPrefix + key, - ); - - debug( - 'green', - 'RedisStringsHandler.get() finished with result (serializedCacheEntry)', - serializedCacheEntry?.substring(0, 200), - ); + try { + if ( + ctx.kind !== 'APP_ROUTE' && + ctx.kind !== 'APP_PAGE' && + ctx.kind !== 'FETCH' + ) { + console.warn( + 'RedisStringsHandler.get() called with', + key, + ctx, + ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ', + (ctx as { kind: string })?.kind, + ); + } - if (!serializedCacheEntry) { - return null; - } + debug('green', 'RedisStringsHandler.get() called with', key, ctx); + await this.assertClientIsReady(); - const cacheEntry: CacheEntry | null = JSON.parse( - serializedCacheEntry, - bufferReviver, - ); + const clientGet = this.redisGetDeduplication + ? this.deduplicatedRedisGet(key) + : this.redisGet; + const serializedCacheEntry = await clientGet( + getTimeoutRedisCommandOptions(this.timeoutMs), + this.keyPrefix + key, + ); - debug( - 'green', - 'RedisStringsHandler.get() finished with result (cacheEntry)', - JSON.stringify(cacheEntry).substring(0, 200), - ); + debug( + 'green', + 'RedisStringsHandler.get() finished with result (serializedCacheEntry)', + serializedCacheEntry?.substring(0, 200), + ); - if (!cacheEntry) { - return null; - } + if (!serializedCacheEntry) { + return null; + } - if (!cacheEntry?.tags) { - console.warn( - 'RedisStringsHandler.get() called with', - key, - ctx, - 'cacheEntry is mall formed (missing tags)', - ); - } - if (!cacheEntry?.value) { - console.warn( - 'RedisStringsHandler.get() called with', - key, - ctx, - 'cacheEntry is mall formed (missing value)', + const cacheEntry: CacheEntry | null = JSON.parse( + serializedCacheEntry, + bufferReviver, ); - } - if (!cacheEntry?.lastModified) { - console.warn( - 'RedisStringsHandler.get() called with', - key, - ctx, - 'cacheEntry is mall formed (missing lastModified)', + + debug( + 'green', + 'RedisStringsHandler.get() finished with result (cacheEntry)', + JSON.stringify(cacheEntry).substring(0, 200), ); - } - if (ctx.kind === 'FETCH') { - const combinedTags = new Set([ - ...(ctx?.softTags || []), - ...(ctx?.tags || []), - ]); + if (!cacheEntry) { + return null; + } - if (combinedTags.size === 0) { - return cacheEntry; + if (!cacheEntry?.tags) { + console.warn( + 'RedisStringsHandler.get() called with', + key, + ctx, + 'cacheEntry is mall formed (missing tags)', + ); + } + if (!cacheEntry?.value) { + console.warn( + 'RedisStringsHandler.get() called with', + key, + ctx, + 'cacheEntry is mall formed (missing value)', + ); + } + if (!cacheEntry?.lastModified) { + console.warn( + 'RedisStringsHandler.get() called with', + key, + ctx, + 'cacheEntry is mall formed (missing lastModified)', + ); } - // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route). See revalidateTag() for more information - // - // This code checks if any of the cache tags associated with this entry (normally the internal tag of the parent page/api route containing the fetch request) - // have been revalidated since the entry was last modified. If any tag was revalidated more recently than the entry's - // lastModified timestamp, then the cached content is considered stale (therefore return null) and should be removed. - for (const tag of combinedTags) { - // Get the last revalidation time for this tag from our revalidatedTagsMap - const revalidationTime = this.revalidatedTagsMap.get(tag); - - // If we have a revalidation time for this tag and it's more recent than when - // this cache entry was last modified, the entry is stale - if (revalidationTime && revalidationTime > cacheEntry.lastModified) { - const redisKey = this.keyPrefix + key; - - // We don't await this cleanup since it can happen asynchronously in the background. - // The cache entry is already considered invalid at this point. - this.client - .unlink(getTimeoutRedisCommandOptions(this.timeoutMs), redisKey) - .catch((err) => { - // If the first unlink fails, only log the error - // Never implement a retry here as the cache entry will be updated directly after this get request - console.error( - 'Error occurred while unlinking stale data. Error was:', - err, - ); - }) - .finally(async () => { - // Clean up our tag tracking maps after the Redis key is removed - await this.sharedTagsMap.delete(key); - await this.revalidatedTagsMap.delete(tag); - }); + if (ctx.kind === 'FETCH') { + const combinedTags = new Set([ + ...(ctx?.softTags || []), + ...(ctx?.tags || []), + ]); - debug( - 'green', - 'RedisStringsHandler.get() found revalidation time for tag. Cache entry is stale and will be deleted and "null" will be returned.', - tag, - redisKey, - revalidationTime, - cacheEntry, - ); + if (combinedTags.size === 0) { + return cacheEntry; + } - // Return null to indicate no valid cache entry was found - return null; + // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route). See revalidateTag() for more information + // + // This code checks if any of the cache tags associated with this entry (normally the internal tag of the parent page/api route containing the fetch request) + // have been revalidated since the entry was last modified. If any tag was revalidated more recently than the entry's + // lastModified timestamp, then the cached content is considered stale (therefore return null) and should be removed. + for (const tag of combinedTags) { + // Get the last revalidation time for this tag from our revalidatedTagsMap + const revalidationTime = this.revalidatedTagsMap.get(tag); + + // If we have a revalidation time for this tag and it's more recent than when + // this cache entry was last modified, the entry is stale + if (revalidationTime && revalidationTime > cacheEntry.lastModified) { + const redisKey = this.keyPrefix + key; + + // We don't await this cleanup since it can happen asynchronously in the background. + // The cache entry is already considered invalid at this point. + this.client + .unlink(getTimeoutRedisCommandOptions(this.timeoutMs), redisKey) + .catch((err) => { + // If the first unlink fails, only log the error + // Never implement a retry here as the cache entry will be updated directly after this get request + console.error( + 'Error occurred while unlinking stale data. Error was:', + err, + ); + }) + .finally(async () => { + // Clean up our tag tracking maps after the Redis key is removed + await this.sharedTagsMap.delete(key); + await this.revalidatedTagsMap.delete(tag); + }); + + debug( + 'green', + 'RedisStringsHandler.get() found revalidation time for tag. Cache entry is stale and will be deleted and "null" will be returned.', + tag, + redisKey, + revalidationTime, + cacheEntry, + ); + + // Return null to indicate no valid cache entry was found + return null; + } } } - } - return cacheEntry; + return cacheEntry; + } catch (error) { + // This catch block is necessary to handle any errors that may occur during: + // 1. Redis operations (get, unlink) + // 2. JSON parsing of cache entries + // 3. Tag validation and cleanup + // If any error occurs, we return null to indicate no valid cache entry was found, + // allowing the application to regenerate the content rather than crash + console.error( + 'RedisStringsHandler.get() Error occurred while getting cache entry. Returning null so site can continue to serve content while cache is disabled. The original error was:', + error, + killContainerOnErrorCount++, + ); + + if ( + this.killContainerOnErrorThreshold > 0 && + killContainerOnErrorCount >= this.killContainerOnErrorThreshold + ) { + console.error( + 'RedisStringsHandler get() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)', + error, + killContainerOnErrorCount, + ); + this.client.disconnect(); + this.client.quit(); + setTimeout(() => { + process.exit(1); + }, 500); + } + return null; + } } public async set( key: string, @@ -425,178 +542,226 @@ export default class RedisStringsHandler { cacheControl?: { revalidate: 5; expire: undefined }; // Version 15.0.3 }, ) { - if ( - data.kind !== 'APP_ROUTE' && - data.kind !== 'APP_PAGE' && - data.kind !== 'FETCH' - ) { - console.warn( - 'RedisStringsHandler.set() called with', + try { + if ( + data.kind !== 'APP_ROUTE' && + data.kind !== 'APP_PAGE' && + data.kind !== 'FETCH' + ) { + console.warn( + 'RedisStringsHandler.set() called with', + key, + ctx, + data, + ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ', + (data as { kind: string })?.kind, + ); + } + + await this.assertClientIsReady(); + + if (data.kind === 'APP_PAGE' || data.kind === 'APP_ROUTE') { + const tags = data.headers['x-next-cache-tags']?.split(','); + ctx.tags = [...(ctx.tags || []), ...(tags || [])]; + } + + // Constructing and serializing the value for storing it in redis + const cacheEntry: CacheEntry = { + lastModified: Date.now(), + tags: ctx?.tags || [], + value: data, + }; + const serializedCacheEntry = JSON.stringify(cacheEntry, bufferReplacer); + + // pre seed data into deduplicated get client. This will reduce redis load by not requesting + // the same value from redis which was just set. + if (this.redisGetDeduplication) { + this.redisDeduplicationHandler.seedRequestReturn( + key, + serializedCacheEntry, + ); + } + + // TODO: implement expiration based on cacheControl.expire argument, -> probably relevant for cacheLife and "use cache" etc.: https://nextjs.org/docs/app/api-reference/functions/cacheLife + // Constructing the expire time for the cache entry + const revalidate = ctx.revalidate || ctx.cacheControl?.revalidate; + const expireAt = + revalidate && Number.isSafeInteger(revalidate) && revalidate > 0 + ? this.estimateExpireAge(revalidate) + : this.estimateExpireAge(this.defaultStaleAge); + + // Setting the cache entry in redis + const options = getTimeoutRedisCommandOptions(this.timeoutMs); + const setOperation: Promise = this.client.set( + options, + this.keyPrefix + key, + serializedCacheEntry, + { + EX: expireAt, + }, + ); + + debug( + 'blue', + 'RedisStringsHandler.set() will set the following serializedCacheEntry', + this.keyPrefix, key, - ctx, data, - ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ', - (data as { kind: string })?.kind, + ctx, + serializedCacheEntry?.substring(0, 200), + expireAt, ); - } - await this.assertClientIsReady(); - - if (data.kind === 'APP_PAGE' || data.kind === 'APP_ROUTE') { - const tags = data.headers['x-next-cache-tags']?.split(','); - ctx.tags = [...(ctx.tags || []), ...(tags || [])]; - } + // Setting the tags for the cache entry in the sharedTagsMap (locally stored hashmap synced via redis) + let setTagsOperation: Promise | undefined; + if (ctx.tags && ctx.tags.length > 0) { + const currentTags = this.sharedTagsMap.get(key); + const currentIsSameAsNew = + currentTags?.length === ctx.tags.length && + currentTags.every((v) => ctx.tags!.includes(v)) && + ctx.tags.every((v) => currentTags.includes(v)); + + if (!currentIsSameAsNew) { + setTagsOperation = this.sharedTagsMap.set( + key, + structuredClone(ctx.tags) as string[], + ); + } + } - // Constructing and serializing the value for storing it in redis - const cacheEntry: CacheEntry = { - lastModified: Date.now(), - tags: ctx?.tags || [], - value: data, - }; - const serializedCacheEntry = JSON.stringify(cacheEntry, bufferReplacer); - - // pre seed data into deduplicated get client. This will reduce redis load by not requesting - // the same value from redis which was just set. - if (this.redisGetDeduplication) { - this.redisDeduplicationHandler.seedRequestReturn( + debug( + 'blue', + 'RedisStringsHandler.set() will set the following sharedTagsMap', key, - serializedCacheEntry, + ctx.tags as string[], ); - } - // TODO: implement expiration based on cacheControl.expire argument, -> probably relevant for cacheLife and "use cache" etc.: https://nextjs.org/docs/app/api-reference/functions/cacheLife - // Constructing the expire time for the cache entry - const revalidate = ctx.revalidate || ctx.cacheControl?.revalidate; - const expireAt = - revalidate && Number.isSafeInteger(revalidate) && revalidate > 0 - ? this.estimateExpireAge(revalidate) - : this.estimateExpireAge(this.defaultStaleAge); - - // Setting the cache entry in redis - const options = getTimeoutRedisCommandOptions(this.timeoutMs); - const setOperation: Promise = this.client.set( - options, - this.keyPrefix + key, - serializedCacheEntry, - { - EX: expireAt, - }, - ); - - debug( - 'blue', - 'RedisStringsHandler.set() will set the following serializedCacheEntry', - this.keyPrefix, - key, - data, - ctx, - serializedCacheEntry?.substring(0, 200), - expireAt, - ); - - // Setting the tags for the cache entry in the sharedTagsMap (locally stored hashmap synced via redis) - let setTagsOperation: Promise | undefined; - if (ctx.tags && ctx.tags.length > 0) { - const currentTags = this.sharedTagsMap.get(key); - const currentIsSameAsNew = - currentTags?.length === ctx.tags.length && - currentTags.every((v) => ctx.tags!.includes(v)) && - ctx.tags.every((v) => currentTags.includes(v)); - - if (!currentIsSameAsNew) { - setTagsOperation = this.sharedTagsMap.set( - key, - structuredClone(ctx.tags) as string[], + await Promise.all([setOperation, setTagsOperation]); + } catch (error) { + console.error( + 'RedisStringsHandler.set() Error occurred while setting cache entry. The original error was:', + error, + killContainerOnErrorCount++, + ); + if ( + this.killContainerOnErrorThreshold > 0 && + killContainerOnErrorCount >= this.killContainerOnErrorThreshold + ) { + console.error( + 'RedisStringsHandler set() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)', + error, + killContainerOnErrorCount, ); + this.client.disconnect(); + this.client.quit(); + setTimeout(() => { + process.exit(1); + }, 500); } + throw error; } - - debug( - 'blue', - 'RedisStringsHandler.set() will set the following sharedTagsMap', - key, - ctx.tags as string[], - ); - - await Promise.all([setOperation, setTagsOperation]); } // eslint-disable-next-line @typescript-eslint/no-explicit-any public async revalidateTag(tagOrTags: string | string[], ...rest: any[]) { - debug( - 'red', - 'RedisStringsHandler.revalidateTag() called with', - tagOrTags, - rest, - ); - const tags = new Set([tagOrTags || []].flat()); - await this.assertClientIsReady(); - - // find all keys that are related to this tag - const keysToDelete: Set = new Set(); - - for (const tag of tags) { - // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route) - // - // Invalidation logic for fetch requests that are related to a invalidated page. - // revalidateTag is called for the page tag (_N_T_...) and the fetch request needs to be invalidated as well - // unfortunately this is not possible since the revalidateTag is not called with any data that would allow us to find the cache entry of the fetch request - // in case of a fetch request get method call, the get method of the cache handler is called with some information about the pages/routes the fetch request is inside - // therefore we only mark the page/route as stale here (with help of the revalidatedTagsMap) - // and delete the cache entry of the fetch request on the next request to the get function - if (tag.startsWith(NEXT_CACHE_IMPLICIT_TAG_ID)) { - const now = Date.now(); - debug( - 'red', - 'RedisStringsHandler.revalidateTag() set revalidation time for tag', - tag, - 'to', - now, - ); - await this.revalidatedTagsMap.set(tag, now); + try { + debug( + 'red', + 'RedisStringsHandler.revalidateTag() called with', + tagOrTags, + rest, + ); + const tags = new Set([tagOrTags || []].flat()); + await this.assertClientIsReady(); + + // find all keys that are related to this tag + const keysToDelete: Set = new Set(); + + for (const tag of tags) { + // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route) + // + // Invalidation logic for fetch requests that are related to a invalidated page. + // revalidateTag is called for the page tag (_N_T_...) and the fetch request needs to be invalidated as well + // unfortunately this is not possible since the revalidateTag is not called with any data that would allow us to find the cache entry of the fetch request + // in case of a fetch request get method call, the get method of the cache handler is called with some information about the pages/routes the fetch request is inside + // therefore we only mark the page/route as stale here (with help of the revalidatedTagsMap) + // and delete the cache entry of the fetch request on the next request to the get function + if (tag.startsWith(NEXT_CACHE_IMPLICIT_TAG_ID)) { + const now = Date.now(); + debug( + 'red', + 'RedisStringsHandler.revalidateTag() set revalidation time for tag', + tag, + 'to', + now, + ); + await this.revalidatedTagsMap.set(tag, now); + } } - } - // Scan the whole sharedTagsMap for keys that are dependent on any of the revalidated tags - for (const [key, sharedTags] of this.sharedTagsMap.entries()) { - if (sharedTags.some((tag) => tags.has(tag))) { - keysToDelete.add(key); + // Scan the whole sharedTagsMap for keys that are dependent on any of the revalidated tags + for (const [key, sharedTags] of this.sharedTagsMap.entries()) { + if (sharedTags.some((tag) => tags.has(tag))) { + keysToDelete.add(key); + } } - } - debug( - 'red', - 'RedisStringsHandler.revalidateTag() found', - keysToDelete, - 'keys to delete', - ); + debug( + 'red', + 'RedisStringsHandler.revalidateTag() found', + keysToDelete, + 'keys to delete', + ); - // exit early if no keys are related to this tag - if (keysToDelete.size === 0) { - return; - } + // exit early if no keys are related to this tag + if (keysToDelete.size === 0) { + return; + } - // prepare deletion of all keys in redis that are related to this tag - const redisKeys = Array.from(keysToDelete); - const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key); - const options = getTimeoutRedisCommandOptions(this.timeoutMs); - const deleteKeysOperation = this.client.unlink(options, fullRedisKeys); + // prepare deletion of all keys in redis that are related to this tag + const redisKeys = Array.from(keysToDelete); + const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key); + const options = getTimeoutRedisCommandOptions(this.timeoutMs); + const deleteKeysOperation = this.client.unlink(options, fullRedisKeys); - // also delete entries from in-memory deduplication cache if they get revalidated - if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) { - for (const key of keysToDelete) { - this.inMemoryDeduplicationCache.delete(key); + // also delete entries from in-memory deduplication cache if they get revalidated + if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) { + for (const key of keysToDelete) { + this.inMemoryDeduplicationCache.delete(key); + } } - } - // prepare deletion of entries from shared tags map if they get revalidated so that the map will not grow indefinitely - const deleteTagsOperation = this.sharedTagsMap.delete(redisKeys); + // prepare deletion of entries from shared tags map if they get revalidated so that the map will not grow indefinitely + const deleteTagsOperation = this.sharedTagsMap.delete(redisKeys); - // execute keys and tag maps deletion - await Promise.all([deleteKeysOperation, deleteTagsOperation]); - debug( - 'red', - 'RedisStringsHandler.revalidateTag() finished delete operations', - ); + // execute keys and tag maps deletion + await Promise.all([deleteKeysOperation, deleteTagsOperation]); + debug( + 'red', + 'RedisStringsHandler.revalidateTag() finished delete operations', + ); + } catch (error) { + console.error( + 'RedisStringsHandler.revalidateTag() Error occurred while revalidating tags. The original error was:', + error, + killContainerOnErrorCount++, + ); + if ( + this.killContainerOnErrorThreshold > 0 && + killContainerOnErrorCount >= this.killContainerOnErrorThreshold + ) { + console.error( + 'RedisStringsHandler revalidateTag() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)', + error, + killContainerOnErrorCount, + ); + this.client.disconnect(); + this.client.quit(); + setTimeout(() => { + process.exit(1); + }, 500); + } + throw error; + } } } diff --git a/src/SyncedMap.ts b/src/SyncedMap.ts index 8770165..3c0777a 100644 --- a/src/SyncedMap.ts +++ b/src/SyncedMap.ts @@ -202,7 +202,11 @@ export class SyncedMap { try { await this.subscriberClient.connect().catch(async () => { - await this.subscriberClient.connect(); + console.error('Failed to connect subscriber client. Retrying...'); + await this.subscriberClient.connect().catch((error) => { + console.error('Failed to connect subscriber client.', error); + throw error; + }); }); // Check if keyspace event configuration is set correctly @@ -214,7 +218,9 @@ export class SyncedMap { )?.['notify-keyspace-events']; if (!keyspaceEventConfig.includes('E')) { throw new Error( - "Keyspace event configuration has to include 'E' for Keyevent events, published with __keyevent@__ prefix. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`", + 'Keyspace event configuration is set to "' + + keyspaceEventConfig + + "\" but has to include 'E' for Keyevent events, published with __keyevent@__ prefix. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`", ); } if ( @@ -225,7 +231,9 @@ export class SyncedMap { ) ) { throw new Error( - "Keyspace event configuration has to include 'A' or 'x' and 'e' for expired and evicted events. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`", + 'Keyspace event configuration is set to "' + + keyspaceEventConfig + + "\" but has to include 'A' or 'x' and 'e' for expired and evicted events. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`", ); } } @@ -316,9 +324,6 @@ export class SyncedMap { await Promise.all(operations); } - // /api/revalidated-fetch - // true - public async delete( keys: string[] | string, withoutSyncMessage = false, From 4764eff399fdc82a623a6950934881ace4011daa Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Tue, 10 Jun 2025 20:19:08 +0000 Subject: [PATCH 02/15] chore(release): 1.8.0-beta.1 [skip ci] --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d44a77..8c58463 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# [1.8.0-beta.1](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.7.1...v1.8.0-beta.1) (2025-06-10) + + +### Features + +* improve error handling ([dd591da](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/dd591daab9539e6ba96da1d1c493a1e771ba272d)) + ## [1.7.1](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.7.0...v1.7.1) (2025-06-10) ### Bug Fixes diff --git a/package.json b/package.json index 62fd1c4..78f3fa7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@trieb.work/nextjs-turbo-redis-cache", - "version": "1.7.1", + "version": "1.8.0-beta.1", "repository": { "type": "git", "url": "https://github.com/trieb-work/nextjs-turbo-redis-cache.git" From 4dceda15a0ae8ca3c5fcdddf861263ef7ca237ce Mon Sep 17 00:00:00 2001 From: Tilman Marquart Date: Wed, 11 Jun 2025 15:47:10 +0200 Subject: [PATCH 03/15] fix: remove connect timeout --- src/RedisStringsHandler.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/RedisStringsHandler.ts b/src/RedisStringsHandler.ts index 6e57b6e..5cff6df 100644 --- a/src/RedisStringsHandler.ts +++ b/src/RedisStringsHandler.ts @@ -149,11 +149,9 @@ export default class RedisStringsHandler { // Create Redis client with properly typed configuration this.client = createClient({ url: redisUrl, - pingInterval: 5000, // Useful with Redis deployments that do not use TCP Keep-Alive. Restarts the connection if it is idle for too long. + pingInterval: 10_000, // Useful with Redis deployments that do not use TCP Keep-Alive. Restarts the connection if it is idle for too long. ...(database !== 0 ? { database } : {}), - ...(socketOptions - ? { socket: { connectTimeout: timeoutMs, ...socketOptions } } - : { connectTimeout: timeoutMs }), + ...(socketOptions ? { socket: { ...socketOptions } } : {}), ...(clientOptions || {}), }); From ab834ec05665c9e15161916f73e0e6581c807005 Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Wed, 11 Jun 2025 13:47:43 +0000 Subject: [PATCH 04/15] chore(release): 1.8.0-beta.2 [skip ci] --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c58463..778f9c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# [1.8.0-beta.2](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.8.0-beta.1...v1.8.0-beta.2) (2025-06-11) + + +### Bug Fixes + +* remove connect timeout ([4dceda1](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/4dceda15a0ae8ca3c5fcdddf861263ef7ca237ce)) + # [1.8.0-beta.1](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.7.1...v1.8.0-beta.1) (2025-06-10) diff --git a/package.json b/package.json index 78f3fa7..d89a908 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@trieb.work/nextjs-turbo-redis-cache", - "version": "1.8.0-beta.1", + "version": "1.8.0-beta.2", "repository": { "type": "git", "url": "https://github.com/trieb-work/nextjs-turbo-redis-cache.git" From 4c8a0d7f82573bbaab63bdd380063244c641735f Mon Sep 17 00:00:00 2001 From: Tilman Marquart Date: Thu, 12 Jun 2025 12:27:01 +0200 Subject: [PATCH 05/15] feat: add redis commands debug logging --- docker/redis.conf | 1 + src/RedisStringsHandler.ts | 53 +++++++++++++++++++++++------ src/SyncedMap.ts | 69 +++++++++++++++++++++++++++++++++----- 3 files changed, 104 insertions(+), 19 deletions(-) create mode 100644 docker/redis.conf diff --git a/docker/redis.conf b/docker/redis.conf new file mode 100644 index 0000000..81abdee --- /dev/null +++ b/docker/redis.conf @@ -0,0 +1 @@ +notify-keyspace-events Exe \ No newline at end of file diff --git a/src/RedisStringsHandler.ts b/src/RedisStringsHandler.ts index 5cff6df..ffffe20 100644 --- a/src/RedisStringsHandler.ts +++ b/src/RedisStringsHandler.ts @@ -13,6 +13,16 @@ export type CacheEntry = { tags: string[]; }; +export function redisErrorHandler>( + debugInfo: string, + redisCommandResult: T, +): T { + return redisCommandResult.catch((error) => { + console.error('Redis command error', debugInfo, error); + throw error; + }) as T; +} + export type CreateRedisStringsHandlerOptions = { /** Redis redisUrl to use. * @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST @@ -352,9 +362,19 @@ export default class RedisStringsHandler { const clientGet = this.redisGetDeduplication ? this.deduplicatedRedisGet(key) : this.redisGet; - const serializedCacheEntry = await clientGet( - getTimeoutRedisCommandOptions(this.timeoutMs), - this.keyPrefix + key, + const serializedCacheEntry = await redisErrorHandler( + 'RedisStringsHandler.get(), operation: get' + + (this.redisGetDeduplication ? 'deduplicated' : '') + + this.timeoutMs + + 'ms' + + ' ' + + this.keyPrefix + + ' ' + + key, + clientGet( + getTimeoutRedisCommandOptions(this.timeoutMs), + this.keyPrefix + key, + ), ); debug( @@ -595,13 +615,17 @@ export default class RedisStringsHandler { // Setting the cache entry in redis const options = getTimeoutRedisCommandOptions(this.timeoutMs); - const setOperation: Promise = this.client.set( - options, - this.keyPrefix + key, - serializedCacheEntry, - { + const setOperation: Promise = redisErrorHandler( + 'RedisStringsHandler.set(), operation: set' + + this.timeoutMs + + 'ms' + + ' ' + + this.keyPrefix + + ' ' + + key, + this.client.set(options, this.keyPrefix + key, serializedCacheEntry, { EX: expireAt, - }, + }), ); debug( @@ -725,7 +749,16 @@ export default class RedisStringsHandler { const redisKeys = Array.from(keysToDelete); const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key); const options = getTimeoutRedisCommandOptions(this.timeoutMs); - const deleteKeysOperation = this.client.unlink(options, fullRedisKeys); + const deleteKeysOperation = redisErrorHandler( + 'RedisStringsHandler.revalidateTag(), operation: unlink' + + this.timeoutMs + + 'ms' + + ' ' + + this.keyPrefix + + ' ' + + fullRedisKeys, + this.client.unlink(options, fullRedisKeys), + ); // also delete entries from in-memory deduplication cache if they get revalidated if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) { diff --git a/src/SyncedMap.ts b/src/SyncedMap.ts index 3c0777a..688a0ae 100644 --- a/src/SyncedMap.ts +++ b/src/SyncedMap.ts @@ -1,5 +1,9 @@ // SyncedMap.ts -import { Client, getTimeoutRedisCommandOptions } from './RedisStringsHandler'; +import { + Client, + getTimeoutRedisCommandOptions, + redisErrorHandler, +} from './RedisStringsHandler'; import { debugVerbose, debug } from './utils/debug'; type CustomizedSync = { @@ -304,11 +308,22 @@ export class SyncedMap { if (!this.customizedSync?.withoutRedisHashmap) { const options = getTimeoutRedisCommandOptions(this.timeoutMs); operations.push( - this.client.hSet( - options, - this.keyPrefix + this.redisKey, - key as unknown as string, - JSON.stringify(value), + redisErrorHandler( + 'SyncedMap.set(), operation: hSet ' + + this.syncChannel + + ' ' + + this.timeoutMs + + 'ms' + + ' ' + + this.keyPrefix + + ' ' + + key, + this.client.hSet( + options, + this.keyPrefix + this.redisKey, + key as unknown as string, + JSON.stringify(value), + ), ), ); } @@ -319,7 +334,18 @@ export class SyncedMap { value, }; operations.push( - this.client.publish(this.syncChannel, JSON.stringify(insertMessage)), + redisErrorHandler( + 'SyncedMap.set(), operation: publish ' + + this.syncChannel + + ' ' + + this.timeoutMs + + 'ms' + + ' ' + + this.keyPrefix + + ' ' + + key, + this.client.publish(this.syncChannel, JSON.stringify(insertMessage)), + ), ); await Promise.all(operations); } @@ -345,7 +371,18 @@ export class SyncedMap { if (!this.customizedSync?.withoutRedisHashmap) { const options = getTimeoutRedisCommandOptions(this.timeoutMs); operations.push( - this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray), + redisErrorHandler( + 'SyncedMap.delete(), operation: hDel ' + + this.syncChannel + + ' ' + + this.timeoutMs + + 'ms' + + ' ' + + this.keyPrefix + + ' ' + + keysArray, + this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray), + ), ); } @@ -355,7 +392,21 @@ export class SyncedMap { keys: keysArray, }; operations.push( - this.client.publish(this.syncChannel, JSON.stringify(deletionMessage)), + redisErrorHandler( + 'SyncedMap.delete(), operation: publish ' + + this.syncChannel + + ' ' + + this.timeoutMs + + 'ms' + + ' ' + + this.keyPrefix + + ' ' + + keysArray, + this.client.publish( + this.syncChannel, + JSON.stringify(deletionMessage), + ), + ), ); } From 2ee9b311e0afaafdb2aeda762abaf5103757ee49 Mon Sep 17 00:00:00 2001 From: Tilman Marquart Date: Thu, 12 Jun 2025 13:39:10 +0200 Subject: [PATCH 06/15] chore: gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index d9ae61c..fe4eed1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ dist coverage .DS_Store .idea +debug \ No newline at end of file From 5a9c2c46d52928e05ca69e4de76327e2996c10cb Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Thu, 12 Jun 2025 11:39:48 +0000 Subject: [PATCH 07/15] chore(release): 1.8.0-beta.3 [skip ci] --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 778f9c1..1d0544e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# [1.8.0-beta.3](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.8.0-beta.2...v1.8.0-beta.3) (2025-06-12) + + +### Features + +* add redis commands debug logging ([4c8a0d7](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/4c8a0d7f82573bbaab63bdd380063244c641735f)) + # [1.8.0-beta.2](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.8.0-beta.1...v1.8.0-beta.2) (2025-06-11) diff --git a/package.json b/package.json index d89a908..3b34e26 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@trieb.work/nextjs-turbo-redis-cache", - "version": "1.8.0-beta.2", + "version": "1.8.0-beta.3", "repository": { "type": "git", "url": "https://github.com/trieb-work/nextjs-turbo-redis-cache.git" From 92508b6697c87cad4a8720fb5380f41fe9ec2257 Mon Sep 17 00:00:00 2001 From: Tilman Marquart Date: Thu, 12 Jun 2025 17:00:49 +0200 Subject: [PATCH 08/15] fix: improve logs --- src/RedisStringsHandler.ts | 22 +++++++++++++++++++++- src/SyncedMap.ts | 4 +++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/RedisStringsHandler.ts b/src/RedisStringsHandler.ts index ffffe20..06bc640 100644 --- a/src/RedisStringsHandler.ts +++ b/src/RedisStringsHandler.ts @@ -17,12 +17,32 @@ export function redisErrorHandler>( debugInfo: string, redisCommandResult: T, ): T { + const beforeTimestamp = performance.now(); return redisCommandResult.catch((error) => { - console.error('Redis command error', debugInfo, error); + console.error( + 'Redis command error', + (performance.now() - beforeTimestamp).toFixed(2), + 'ms', + debugInfo, + error, + ); throw error; }) as T; } +// This is a test to check if the event loop is lagging. Increase CPU +setInterval(() => { + const start = performance.now(); + setImmediate(() => { + const duration = performance.now() - start; + if (duration > 100) { + console.warn( + `RedisStringsHandler detected an event loop lag of: ${duration.toFixed(2)}ms`, + ); + } + }); +}, 1000); + export type CreateRedisStringsHandlerOptions = { /** Redis redisUrl to use. * @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST diff --git a/src/SyncedMap.ts b/src/SyncedMap.ts index 688a0ae..a2adca6 100644 --- a/src/SyncedMap.ts +++ b/src/SyncedMap.ts @@ -369,7 +369,7 @@ export class SyncedMap { } if (!this.customizedSync?.withoutRedisHashmap) { - const options = getTimeoutRedisCommandOptions(this.timeoutMs); + const options = getTimeoutRedisCommandOptions(this.timeoutMs * 10); operations.push( redisErrorHandler( 'SyncedMap.delete(), operation: hDel ' + @@ -380,6 +380,8 @@ export class SyncedMap { ' ' + this.keyPrefix + ' ' + + this.redisKey + + ' ' + keysArray, this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray), ), From 3a63820e3ad3ee0c8b7dc4e47f66e1e938d3d1a8 Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Thu, 12 Jun 2025 15:01:32 +0000 Subject: [PATCH 09/15] chore(release): 1.8.0-beta.4 [skip ci] --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d0544e..b1caa06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# [1.8.0-beta.4](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.8.0-beta.3...v1.8.0-beta.4) (2025-06-12) + + +### Bug Fixes + +* improve logs ([92508b6](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/92508b6697c87cad4a8720fb5380f41fe9ec2257)) + # [1.8.0-beta.3](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.8.0-beta.2...v1.8.0-beta.3) (2025-06-12) diff --git a/package.json b/package.json index 3b34e26..91a1307 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@trieb.work/nextjs-turbo-redis-cache", - "version": "1.8.0-beta.3", + "version": "1.8.0-beta.4", "repository": { "type": "git", "url": "https://github.com/trieb-work/nextjs-turbo-redis-cache.git" From 56c82c3dd62257e8686fedc25dea45a0b7fec18e Mon Sep 17 00:00:00 2001 From: Tilman Marquart Date: Thu, 12 Jun 2025 17:35:55 +0200 Subject: [PATCH 10/15] fix: scan+hscan logging --- src/SyncedMap.ts | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/SyncedMap.ts b/src/SyncedMap.ts index a2adca6..61ecc63 100644 --- a/src/SyncedMap.ts +++ b/src/SyncedMap.ts @@ -89,11 +89,26 @@ export class SyncedMap { try { do { - const remoteItems = await this.client.hScan( - getTimeoutRedisCommandOptions(this.timeoutMs), - this.keyPrefix + this.redisKey, - cursor, - hScanOptions, + const remoteItems = await redisErrorHandler( + 'SyncedMap.initialSync(), operation: hScan ' + + this.syncChannel + + ' ' + + this.timeoutMs + + 'ms' + + ' ' + + this.keyPrefix + + ' ' + + this.redisKey + + ' ' + + cursor + + ' ' + + this.querySize, + this.client.hScan( + getTimeoutRedisCommandOptions(this.timeoutMs), + this.keyPrefix + this.redisKey, + cursor, + hScanOptions, + ), ); for (const { field, value } of remoteItems.tuples) { if (this.filterKeys(field)) { @@ -118,10 +133,17 @@ export class SyncedMap { let remoteKeys: string[] = []; try { do { - const remoteKeysPortion = await this.client.scan( - getTimeoutRedisCommandOptions(this.timeoutMs), - cursor, - scanOptions, + const remoteKeysPortion = await redisErrorHandler( + 'SyncedMap.cleanupKeysNotInRedis(), operation: scan ' + + this.timeoutMs + + 'ms' + + ' ' + + this.keyPrefix, + this.client.scan( + getTimeoutRedisCommandOptions(this.timeoutMs), + cursor, + scanOptions, + ), ); remoteKeys = remoteKeys.concat(remoteKeysPortion.keys); cursor = remoteKeysPortion.cursor; From 275176d89f62b4d52d7e48818ecfe2e93bb8baef Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Thu, 12 Jun 2025 15:36:50 +0000 Subject: [PATCH 11/15] chore(release): 1.8.0-beta.5 [skip ci] --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1caa06..7dcdcbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# [1.8.0-beta.5](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.8.0-beta.4...v1.8.0-beta.5) (2025-06-12) + + +### Bug Fixes + +* scan+hscan logging ([56c82c3](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/56c82c3dd62257e8686fedc25dea45a0b7fec18e)) + # [1.8.0-beta.4](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.8.0-beta.3...v1.8.0-beta.4) (2025-06-12) diff --git a/package.json b/package.json index 91a1307..79162b3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@trieb.work/nextjs-turbo-redis-cache", - "version": "1.8.0-beta.4", + "version": "1.8.0-beta.5", "repository": { "type": "git", "url": "https://github.com/trieb-work/nextjs-turbo-redis-cache.git" From 02deb649fce40085495c6fec5e8750cba42d2428 Mon Sep 17 00:00:00 2001 From: Tilman Marquart Date: Fri, 13 Jun 2025 14:07:56 +0200 Subject: [PATCH 12/15] feat: remove general timeoutMs and replace it with getTimeoutMs (for faster non-blocking rendering) --- README.md | 32 ++++++++++---------- src/RedisStringsHandler.ts | 60 +++++++++++++++----------------------- src/SyncedMap.ts | 39 ++----------------------- 3 files changed, 42 insertions(+), 89 deletions(-) diff --git a/README.md b/README.md index 60f622e..9581fb9 100644 --- a/README.md +++ b/README.md @@ -123,22 +123,22 @@ A working example of above can be found in the `test/integration/next-app-custom ## Available Options -| Option | Description | Default Value | -| ----------------------------- | ----------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| redisUrl | Redis connection url | `process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST ? redis://${process.env.REDISHOST}:${process.env.REDISPORT} : 'redis://localhost:6379'` | -| database | Redis database number to use. Uses DB 0 for production, DB 1 otherwise | `process.env.VERCEL_ENV === 'production' ? 0 : 1` | -| keyPrefix | Prefix added to all Redis keys | `process.env.VERCEL_URL \|\| 'UNDEFINED_URL_'` | -| sharedTagsKey | Key used to store shared tags hash map in Redis | `'__sharedTags__'` | -| timeoutMs | Timeout in milliseconds for Redis operations | `Number.parseInt(process.env.REDIS_COMMAND_TIMEOUT_MS) ?? 5_000 : 5_000` | -| revalidateTagQuerySize | Number of entries to query in one batch during full sync of shared tags hash map | `250` | -| avgResyncIntervalMs | Average interval in milliseconds between tag map full re-syncs | `3600000` (1 hour) | -| redisGetDeduplication | Enable deduplication of Redis get requests via internal in-memory cache. | `true` | -| inMemoryCachingTime | Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely. | `10000` | -| defaultStaleAge | Default stale age in seconds for cached items | `1209600` (14 days) | -| estimateExpireAge | Function to calculate expire age (redis TTL value) from stale age | Production: `staleAge * 2`
Other: `staleAge * 1.2` | -| socketOptions | Redis client socket options for TLS/SSL configuration (e.g., `{ tls: true, rejectUnauthorized: false }`) | `{ connectTimeout: timeoutMs }` | -| clientOptions | Additional Redis client options (e.g., username, password) | `undefined` | -| killContainerOnErrorThreshold | Number of consecutive errors before the container is killed. Set to 0 to disable. | `Number.parseInt(process.env.KILL_CONTAINER_ON_ERROR_THRESHOLD) ?? 0 : 0` | +| Option | Description | Default Value | +| ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| redisUrl | Redis connection url | `process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST ? redis://${process.env.REDISHOST}:${process.env.REDISPORT} : 'redis://localhost:6379'` | +| database | Redis database number to use. Uses DB 0 for production, DB 1 otherwise | `process.env.VERCEL_ENV === 'production' ? 0 : 1` | +| keyPrefix | Prefix added to all Redis keys | `process.env.VERCEL_URL \|\| 'UNDEFINED_URL_'` | +| sharedTagsKey | Key used to store shared tags hash map in Redis | `'__sharedTags__'` | +| getTimeoutMs | Timeout in milliseconds for time critical Redis operations. If Redis get is not fulfilled within this time, returns null to avoid blocking site rendering. | `process.env.REDIS_COMMAND_TIMEOUT_MS ? (Number.parseInt(process.env.REDIS_COMMAND_TIMEOUT_MS) ?? 500) : 500` | +| revalidateTagQuerySize | Number of entries to query in one batch during full sync of shared tags hash map | `250` | +| avgResyncIntervalMs | Average interval in milliseconds between tag map full re-syncs | `3600000` (1 hour) | +| redisGetDeduplication | Enable deduplication of Redis get requests via internal in-memory cache. | `true` | +| inMemoryCachingTime | Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely. | `10000` | +| defaultStaleAge | Default stale age in seconds for cached items | `1209600` (14 days) | +| estimateExpireAge | Function to calculate expire age (redis TTL value) from stale age | Production: `staleAge * 2`
Other: `staleAge * 1.2` | +| socketOptions | Redis client socket options for TLS/SSL configuration (e.g., `{ tls: true, rejectUnauthorized: false }`) | `{ connectTimeout: timeoutMs }` | +| clientOptions | Additional Redis client options (e.g., username, password) | `undefined` | +| killContainerOnErrorThreshold | Number of consecutive errors before the container is killed. Set to 0 to disable. | `Number.parseInt(process.env.KILL_CONTAINER_ON_ERROR_THRESHOLD) ?? 0 : 0` | ## TLS Configuration diff --git a/src/RedisStringsHandler.ts b/src/RedisStringsHandler.ts index 06bc640..0f46a23 100644 --- a/src/RedisStringsHandler.ts +++ b/src/RedisStringsHandler.ts @@ -30,7 +30,7 @@ export function redisErrorHandler>( }) as T; } -// This is a test to check if the event loop is lagging. Increase CPU +// This is a test to check if the event loop is lagging. If it lags, increase CPU of container setInterval(() => { const start = performance.now(); setImmediate(() => { @@ -58,10 +58,12 @@ export type CreateRedisStringsHandlerOptions = { * @default process.env.VERCEL_URL || 'UNDEFINED_URL_' */ keyPrefix?: string; - /** Timeout in milliseconds for Redis operations - * @default 5000 + /** Timeout in milliseconds for time critical Redis operations (during cache get, which blocks site rendering). + * If redis get is not fulfilled within this time, the cache handler will return null so site rendering will + * not be blocked further and site can fallback to re-render/re-fetch the content. + * @default 500 */ - timeoutMs?: number; + getTimeoutMs?: number; /** Number of entries to query in one batch during full sync of shared tags hash map * @default 250 */ @@ -112,12 +114,6 @@ const NEXT_CACHE_IMPLICIT_TAG_ID = '_N_T_'; // This helps track when specific tags were last invalidated const REVALIDATED_TAGS_KEY = '__revalidated_tags__'; -export function getTimeoutRedisCommandOptions( - timeoutMs: number, -): CommandOptions { - return commandOptions({ signal: AbortSignal.timeout(timeoutMs) }); -} - let killContainerOnErrorCount: number = 0; export default class RedisStringsHandler { private client: Client; @@ -126,13 +122,13 @@ export default class RedisStringsHandler { private inMemoryDeduplicationCache: SyncedMap< Promise> >; + private getTimeoutMs: number; private redisGet: Client['get']; private redisDeduplicationHandler: DeduplicatedRequestHandler< Client['get'], string | Buffer | null >; private deduplicatedRedisGet: (key: string) => Client['get']; - private timeoutMs: number; private keyPrefix: string; private redisGetDeduplication: boolean; private inMemoryCachingTime: number; @@ -149,9 +145,9 @@ export default class RedisStringsHandler { database = process.env.VERCEL_ENV === 'production' ? 0 : 1, keyPrefix = process.env.VERCEL_URL || 'UNDEFINED_URL_', sharedTagsKey = '__sharedTags__', - timeoutMs = process.env.REDIS_COMMAND_TIMEOUT_MS - ? (Number.parseInt(process.env.REDIS_COMMAND_TIMEOUT_MS) ?? 5_000) - : 5_000, + getTimeoutMs = process.env.REDIS_COMMAND_TIMEOUT_MS + ? (Number.parseInt(process.env.REDIS_COMMAND_TIMEOUT_MS) ?? 500) + : 500, revalidateTagQuerySize = 250, avgResyncIntervalMs = 60 * 60 * 1_000, redisGetDeduplication = true, @@ -168,12 +164,12 @@ export default class RedisStringsHandler { }: CreateRedisStringsHandlerOptions) { try { this.keyPrefix = keyPrefix; - this.timeoutMs = timeoutMs; this.redisGetDeduplication = redisGetDeduplication; this.inMemoryCachingTime = inMemoryCachingTime; this.defaultStaleAge = defaultStaleAge; this.estimateExpireAge = estimateExpireAge; this.killContainerOnErrorThreshold = killContainerOnErrorThreshold; + this.getTimeoutMs = getTimeoutMs; try { // Create Redis client with properly typed configuration @@ -243,7 +239,6 @@ export default class RedisStringsHandler { keyPrefix, redisKey: sharedTagsKey, database, - timeoutMs, querySize: revalidateTagQuerySize, filterKeys, resyncIntervalMs: @@ -257,7 +252,6 @@ export default class RedisStringsHandler { keyPrefix, redisKey: REVALIDATED_TAGS_KEY, database, - timeoutMs, querySize: revalidateTagQuerySize, filterKeys, resyncIntervalMs: @@ -271,7 +265,6 @@ export default class RedisStringsHandler { keyPrefix, redisKey: 'inMemoryDeduplicationCache', database, - timeoutMs, querySize: revalidateTagQuerySize, filterKeys, customizedSync: { @@ -332,7 +325,7 @@ export default class RedisStringsHandler { 'assertClientIsReady: Timeout waiting for Redis maps to be ready', ), ); - }, this.timeoutMs * 5), + }, 30_000), ), ]); this.clientReadyCalls = 0; @@ -385,14 +378,14 @@ export default class RedisStringsHandler { const serializedCacheEntry = await redisErrorHandler( 'RedisStringsHandler.get(), operation: get' + (this.redisGetDeduplication ? 'deduplicated' : '') + - this.timeoutMs + - 'ms' + ' ' + + this.getTimeoutMs + + 'ms ' + this.keyPrefix + ' ' + key, clientGet( - getTimeoutRedisCommandOptions(this.timeoutMs), + commandOptions({ signal: AbortSignal.timeout(this.getTimeoutMs) }), this.keyPrefix + key, ), ); @@ -474,10 +467,11 @@ export default class RedisStringsHandler { // We don't await this cleanup since it can happen asynchronously in the background. // The cache entry is already considered invalid at this point. this.client - .unlink(getTimeoutRedisCommandOptions(this.timeoutMs), redisKey) + .unlink(redisKey) .catch((err) => { - // If the first unlink fails, only log the error - // Never implement a retry here as the cache entry will be updated directly after this get request + // Log error but don't retry deletion since the cache entry will likely be + // updated immediately after via set(). A retry could dangerously execute + // after the new value is set. console.error( 'Error occurred while unlinking stale data. Error was:', err, @@ -634,16 +628,12 @@ export default class RedisStringsHandler { : this.estimateExpireAge(this.defaultStaleAge); // Setting the cache entry in redis - const options = getTimeoutRedisCommandOptions(this.timeoutMs); const setOperation: Promise = redisErrorHandler( - 'RedisStringsHandler.set(), operation: set' + - this.timeoutMs + - 'ms' + - ' ' + + 'RedisStringsHandler.set(), operation: set ' + this.keyPrefix + ' ' + key, - this.client.set(options, this.keyPrefix + key, serializedCacheEntry, { + this.client.set(this.keyPrefix + key, serializedCacheEntry, { EX: expireAt, }), ); @@ -768,16 +758,12 @@ export default class RedisStringsHandler { // prepare deletion of all keys in redis that are related to this tag const redisKeys = Array.from(keysToDelete); const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key); - const options = getTimeoutRedisCommandOptions(this.timeoutMs); const deleteKeysOperation = redisErrorHandler( - 'RedisStringsHandler.revalidateTag(), operation: unlink' + - this.timeoutMs + - 'ms' + - ' ' + + 'RedisStringsHandler.revalidateTag(), operation: unlink ' + this.keyPrefix + ' ' + fullRedisKeys, - this.client.unlink(options, fullRedisKeys), + this.client.unlink(fullRedisKeys), ); // also delete entries from in-memory deduplication cache if they get revalidated diff --git a/src/SyncedMap.ts b/src/SyncedMap.ts index 61ecc63..e89ac8c 100644 --- a/src/SyncedMap.ts +++ b/src/SyncedMap.ts @@ -1,9 +1,5 @@ // SyncedMap.ts -import { - Client, - getTimeoutRedisCommandOptions, - redisErrorHandler, -} from './RedisStringsHandler'; +import { Client, redisErrorHandler } from './RedisStringsHandler'; import { debugVerbose, debug } from './utils/debug'; type CustomizedSync = { @@ -16,7 +12,6 @@ type SyncedMapOptions = { keyPrefix: string; redisKey: string; // Redis Hash key database: number; - timeoutMs: number; querySize: number; filterKeys: (key: string) => boolean; resyncIntervalMs?: number; @@ -39,7 +34,6 @@ export class SyncedMap { private syncChannel: string; private redisKey: string; private database: number; - private timeoutMs: number; private querySize: number; private filterKeys: (key: string) => boolean; private resyncIntervalMs?: number; @@ -54,7 +48,6 @@ export class SyncedMap { this.redisKey = options.redisKey; this.syncChannel = `${options.keyPrefix}${SYNC_CHANNEL_SUFFIX}${options.redisKey}`; this.database = options.database; - this.timeoutMs = options.timeoutMs; this.querySize = options.querySize; this.filterKeys = options.filterKeys; this.resyncIntervalMs = options.resyncIntervalMs; @@ -93,9 +86,6 @@ export class SyncedMap { 'SyncedMap.initialSync(), operation: hScan ' + this.syncChannel + ' ' + - this.timeoutMs + - 'ms' + - ' ' + this.keyPrefix + ' ' + this.redisKey + @@ -104,7 +94,6 @@ export class SyncedMap { ' ' + this.querySize, this.client.hScan( - getTimeoutRedisCommandOptions(this.timeoutMs), this.keyPrefix + this.redisKey, cursor, hScanOptions, @@ -135,15 +124,8 @@ export class SyncedMap { do { const remoteKeysPortion = await redisErrorHandler( 'SyncedMap.cleanupKeysNotInRedis(), operation: scan ' + - this.timeoutMs + - 'ms' + - ' ' + this.keyPrefix, - this.client.scan( - getTimeoutRedisCommandOptions(this.timeoutMs), - cursor, - scanOptions, - ), + this.client.scan(cursor, scanOptions), ); remoteKeys = remoteKeys.concat(remoteKeysPortion.keys); cursor = remoteKeysPortion.cursor; @@ -328,20 +310,15 @@ export class SyncedMap { return; } if (!this.customizedSync?.withoutRedisHashmap) { - const options = getTimeoutRedisCommandOptions(this.timeoutMs); operations.push( redisErrorHandler( 'SyncedMap.set(), operation: hSet ' + this.syncChannel + ' ' + - this.timeoutMs + - 'ms' + - ' ' + this.keyPrefix + ' ' + key, this.client.hSet( - options, this.keyPrefix + this.redisKey, key as unknown as string, JSON.stringify(value), @@ -360,9 +337,6 @@ export class SyncedMap { 'SyncedMap.set(), operation: publish ' + this.syncChannel + ' ' + - this.timeoutMs + - 'ms' + - ' ' + this.keyPrefix + ' ' + key, @@ -391,21 +365,17 @@ export class SyncedMap { } if (!this.customizedSync?.withoutRedisHashmap) { - const options = getTimeoutRedisCommandOptions(this.timeoutMs * 10); operations.push( redisErrorHandler( 'SyncedMap.delete(), operation: hDel ' + this.syncChannel + ' ' + - this.timeoutMs + - 'ms' + - ' ' + this.keyPrefix + ' ' + this.redisKey + ' ' + keysArray, - this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray), + this.client.hDel(this.keyPrefix + this.redisKey, keysArray), ), ); } @@ -420,9 +390,6 @@ export class SyncedMap { 'SyncedMap.delete(), operation: publish ' + this.syncChannel + ' ' + - this.timeoutMs + - 'ms' + - ' ' + this.keyPrefix + ' ' + keysArray, From 4c4e96986e7f14f41e1d37f878ac8eef3e0ce4d9 Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Fri, 13 Jun 2025 12:08:37 +0000 Subject: [PATCH 13/15] chore(release): 1.8.0-beta.6 [skip ci] --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dcdcbb..5ea3bb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# [1.8.0-beta.6](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.8.0-beta.5...v1.8.0-beta.6) (2025-06-13) + + +### Features + +* remove general timeoutMs and replace it with getTimeoutMs (for faster non-blocking rendering) ([02deb64](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/02deb649fce40085495c6fec5e8750cba42d2428)) + # [1.8.0-beta.5](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.8.0-beta.4...v1.8.0-beta.5) (2025-06-12) diff --git a/package.json b/package.json index 79162b3..584cc55 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@trieb.work/nextjs-turbo-redis-cache", - "version": "1.8.0-beta.5", + "version": "1.8.0-beta.6", "repository": { "type": "git", "url": "https://github.com/trieb-work/nextjs-turbo-redis-cache.git" From 5337ceb609f85ff40c9208b7130451c1e5c8eb8b Mon Sep 17 00:00:00 2001 From: Tilman Marquart Date: Fri, 13 Jun 2025 14:44:26 +0200 Subject: [PATCH 14/15] chore: fmt --- src/RedisStringsHandler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RedisStringsHandler.ts b/src/RedisStringsHandler.ts index 0f46a23..771a8ff 100644 --- a/src/RedisStringsHandler.ts +++ b/src/RedisStringsHandler.ts @@ -41,7 +41,7 @@ setInterval(() => { ); } }); -}, 1000); +}, 10); export type CreateRedisStringsHandlerOptions = { /** Redis redisUrl to use. From 35ea153fafa9e51d705997e366a640fa32c34421 Mon Sep 17 00:00:00 2001 From: Tilman Marquart Date: Fri, 13 Jun 2025 14:49:17 +0200 Subject: [PATCH 15/15] chore: readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9581fb9..6c62036 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ There exists also the SKIP_KEYSPACE_CONFIG_CHECK environment variable to skip th KILL_CONTAINER_ON_ERROR_THRESHOLD: Optional environment variable that defines how many Redis client errors should occur before the process exits with code 1. This is useful in container environments like Kubernetes where you want the container to restart if Redis connectivity issues persist. Set to 0 (default) to disable this feature. For example, setting KILL_CONTAINER_ON_ERROR_THRESHOLD=10 will exit the process after 10 Redis client errors, allowing the container orchestrator to restart the container. -REDIS_COMMAND_TIMEOUT_MS: Optional environment variable that sets the timeout in milliseconds for Redis commands. If not set, defaults to 5000ms (5 seconds). The value is parsed as an integer, and if parsing fails, falls back to the 5000ms default. +REDIS_COMMAND_TIMEOUT_MS: Optional environment variable that sets the timeout in milliseconds for Redis get command. If not set, defaults to 500ms. The value is parsed as an integer, and if parsing fails, falls back to the 500ms default. ### Option A: minimum implementation with default options