0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

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 <gao@silverhand.io>

---------

Co-authored-by: Gao Sun <gao@silverhand.io>
This commit is contained in:
simeng-li 2024-11-25 17:46:51 +08:00 committed by GitHub
parent 2856f9ee28
commit 3e034b580f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 4 deletions

View file

@ -90,6 +90,9 @@ export class RedisCache extends RedisCacheBase {
this.client = createClient({ this.client = createClient({
url: conditional(!yes(redisUrl) && redisUrl), url: conditional(!yes(redisUrl) && redisUrl),
socket: this.getSocketOptions(trySafe(() => new URL(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) => { this.client.on('error', (error) => {

View file

@ -89,15 +89,16 @@ export class WellKnownCache {
/** /**
* Get value from the inner cache store for the given type and key. * 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 extends WellKnownCacheType>( async get<Type extends WellKnownCacheType>(
type: Type, type: Type,
key: string key: string
): Promise<Optional<WellKnownMap[Type]>> { ): Promise<Optional<WellKnownMap[Type]>> {
const data = await this.cacheStore.get(this.cacheKey(type, key)); return trySafe(async () => {
const data = await this.cacheStore.get(this.cacheKey(type, key));
return trySafe(() => getValueGuard(type).parse(JSON.parse(data ?? ''))); return getValueGuard(type).parse(JSON.parse(data ?? ''));
});
} }
/** /**