From 81752360b699233a2b4b969061ff438c637ca5a6 Mon Sep 17 00:00:00 2001 From: HarukaKishida Date: Tue, 4 Mar 2025 00:04:58 +0900 Subject: [PATCH] fix:Adapting keyv store --- .../src/app/redis-cache/redis-cache.module.ts | 18 ++++++------- .../app/redis-cache/redis-cache.service.ts | 26 ++++++++++--------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/apps/api/src/app/redis-cache/redis-cache.module.ts b/apps/api/src/app/redis-cache/redis-cache.module.ts index 208a7c28b..31c93d067 100644 --- a/apps/api/src/app/redis-cache/redis-cache.module.ts +++ b/apps/api/src/app/redis-cache/redis-cache.module.ts @@ -1,17 +1,16 @@ import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; -import { createKeyv } from '@keyv/redis'; +import KeyvRedis, { Keyv } from '@keyv/redis'; import { CacheModule } from '@nestjs/cache-manager'; import { Module } from '@nestjs/common'; -import type { RedisClientOptions } from 'redis'; import { RedisCacheService } from './redis-cache.service'; @Module({ exports: [RedisCacheService], imports: [ - CacheModule.registerAsync({ + CacheModule.registerAsync({ imports: [ConfigurationModule], inject: [ConfigurationService], useFactory: async (configurationService: ConfigurationService) => { @@ -20,13 +19,14 @@ import { RedisCacheService } from './redis-cache.service'; ); return { - store: [ - createKeyv( + store: new Keyv({ + store: new KeyvRedis( `redis://${redisPassword ? `:${redisPassword}` : ''}@${configurationService.get('REDIS_HOST')}:${configurationService.get('REDIS_PORT')}/${configurationService.get('REDIS_DB')}` - ) - ], - ttl: configurationService.get('CACHE_TTL') - }; + ), + ttl: configurationService.get('CACHE_TTL'), + useKeyPrefix: false + }) + } as Keyv; } }), ConfigurationModule 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 7e8550c30..24eeb659a 100644 --- a/apps/api/src/app/redis-cache/redis-cache.service.ts +++ b/apps/api/src/app/redis-cache/redis-cache.service.ts @@ -19,13 +19,17 @@ export class RedisCacheService { } public async getKeys(aPrefix?: string): Promise { - let prefix = aPrefix; - - if (prefix) { - prefix = `${prefix}*`; - } - - return this.cache.get(prefix); + const prefix = aPrefix; + const keyList = []; + this.cache.stores[0].iterator((key) => { + if (prefix && key.startsWith(prefix)) { + keyList.push(key); + } else if (!prefix) { + keyList.push(key); + } + }); + + return keyList; } public getPortfolioSnapshotKey({ @@ -54,8 +58,9 @@ export class RedisCacheService { public async isHealthy() { try { + const client = this.cache.stores[0]; const isHealthy = await Promise.race([ - this.cache, + client.ttl, new Promise((_, reject) => setTimeout( () => reject(new Error('Redis health check timeout')), @@ -82,10 +87,7 @@ export class RedisCacheService { const keys = await this.getKeys( `${this.getPortfolioSnapshotKey({ userId })}` ); - - for (const key of keys) { - await this.remove(key); - } + this.cache.mdel(keys); } public async reset() {