From 3af558c580ab295512b7dc7403c24dfd19d35011 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sun, 8 Jun 2025 20:58:47 +0200 Subject: [PATCH] Bugfix/handle exception in getKeys() of Redis cache service (#4871) * Handle exception in getKeys() * Update changelog --- CHANGELOG.md | 1 + .../app/redis-cache/redis-cache.service.ts | 40 +++++++++---------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6f98ee1f..e2d973013 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Handled an exception in the get keys function of the _Redis_ cache service - Fixed missing `/.well-known/assetlinks.json` for TWA ## 2.168.0 - 2025-06-07 diff --git a/apps/api/src/app/redis-cache/redis-cache.service.ts b/apps/api/src/app/redis-cache/redis-cache.service.ts index ae4493e51..621199cc9 100644 --- a/apps/api/src/app/redis-cache/redis-cache.service.ts +++ b/apps/api/src/app/redis-cache/redis-cache.service.ts @@ -5,17 +5,28 @@ import { AssetProfileIdentifier, Filter } from '@ghostfolio/common/interfaces'; import { CACHE_MANAGER, Cache } from '@nestjs/cache-manager'; import { Inject, Injectable, Logger } from '@nestjs/common'; import { createHash } from 'crypto'; +import Keyv from 'keyv'; import ms from 'ms'; @Injectable() export class RedisCacheService { + private client: Keyv; + public constructor( @Inject(CACHE_MANAGER) private readonly cache: Cache, private readonly configurationService: ConfigurationService ) { - const client = cache.stores[0]; + this.client = cache.stores[0]; + + this.client.deserialize = (value) => { + try { + return JSON.parse(value); + } catch {} + + return value; + }; - client.on('error', (error) => { + this.client.on('error', (error) => { Logger.error(error, 'RedisCacheService'); }); } @@ -28,28 +39,13 @@ export class RedisCacheService { const keys: string[] = []; const prefix = aPrefix; - this.cache.stores[0].deserialize = (value) => { - try { - return JSON.parse(value); - } catch (error: any) { - if (error instanceof SyntaxError) { - Logger.debug( - `Failed to parse json, returning the value as String: ${value}`, - 'RedisCacheService' - ); - - return value; - } else { - throw error; + try { + for await (const [key] of this.client.iterator({})) { + if ((prefix && key.startsWith(prefix)) || !prefix) { + keys.push(key); } } - }; - - for await (const [key] of this.cache.stores[0].iterator({})) { - if ((prefix && key.startsWith(prefix)) || !prefix) { - keys.push(key); - } - } + } catch {} return keys; }