Unverified Commit 3af558c5 authored by Thomas Kaul's avatar Thomas Kaul Committed by GitHub
Browse files

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

* Handle exception in getKeys()

* Update changelog
parent 85aa323e
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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
+18 −22
Original line number Diff line number Diff line
@@ -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 {}

    client.on('error', (error) => {
      return value;
    };

    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;
        }
      }
    };

    for await (const [key] of this.cache.stores[0].iterator({})) {
      for await (const [key] of this.client.iterator({})) {
        if ((prefix && key.startsWith(prefix)) || !prefix) {
          keys.push(key);
        }
      }
    } catch {}

    return keys;
  }