Browse Source

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

* Handle exception in getKeys()

* Update changelog
pull/4873/head
Thomas Kaul 3 weeks ago
committed by GitHub
parent
commit
3af558c580
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 1
      CHANGELOG.md
  2. 40
      apps/api/src/app/redis-cache/redis-cache.service.ts

1
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

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

Loading…
Cancel
Save