Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

Bugfix/handle exception in getKeys() of Redis cache service (#4871)

Thomas Kaul3af558c580ab
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a6f98ee1fd..e2d9730136 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 ae4493e514..621199cc94 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;
}