From 3e034b580f08226512b7dc323cf4c646e77aa9bd Mon Sep 17 00:00:00 2001 From: simeng-li Date: Mon, 25 Nov 2024 17:46:51 +0800 Subject: [PATCH] fix(core): add redis ping interval (#6819) * fix(core): add redis ping interval add redis ping interval * refactor(core): add trySafe to wellknown cache add trySafe to wellknown cache * chore: add comments Co-authored-by: Gao Sun --------- Co-authored-by: Gao Sun --- packages/core/src/caches/index.ts | 3 +++ packages/core/src/caches/well-known.ts | 9 +++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/core/src/caches/index.ts b/packages/core/src/caches/index.ts index b92c368a1..45531fc0a 100644 --- a/packages/core/src/caches/index.ts +++ b/packages/core/src/caches/index.ts @@ -90,6 +90,9 @@ export class RedisCache extends RedisCacheBase { this.client = createClient({ url: conditional(!yes(redisUrl) && redisUrl), socket: this.getSocketOptions(trySafe(() => new URL(redisUrl))), + // Azure redis has a 10 minutes idle timeout + // @see https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-best-practices-connection#idle-timeout + pingInterval: 8 * 60 * 1000, // 8 minutes }); this.client.on('error', (error) => { diff --git a/packages/core/src/caches/well-known.ts b/packages/core/src/caches/well-known.ts index 7b9f12f62..d3ba49981 100644 --- a/packages/core/src/caches/well-known.ts +++ b/packages/core/src/caches/well-known.ts @@ -89,15 +89,16 @@ export class WellKnownCache { /** * Get value from the inner cache store for the given type and key. - * Note: Format errors will be silently caught and result an `undefined` return. + * Note: Redis connection and format errors will be silently caught and result an `undefined` return. */ async get( type: Type, key: string ): Promise> { - const data = await this.cacheStore.get(this.cacheKey(type, key)); - - return trySafe(() => getValueGuard(type).parse(JSON.parse(data ?? ''))); + return trySafe(async () => { + const data = await this.cacheStore.get(this.cacheKey(type, key)); + return getValueGuard(type).parse(JSON.parse(data ?? '')); + }); } /**