From 2ec12d4e3fc233eaee9210eeed0c8daa358dc32f Mon Sep 17 00:00:00 2001 From: HarukaKishida Date: Tue, 11 Feb 2025 16:22:12 +0900 Subject: [PATCH] migrate to keyv/redis --- .../src/app/redis-cache/redis-cache.module.ts | 13 ++++++---- .../app/redis-cache/redis-cache.service.ts | 23 ++++++------------ package-lock.json | 24 +++++++++++++++++++ package.json | 1 + 4 files changed, 40 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 5411309bd..208a7c28b 100644 --- a/apps/api/src/app/redis-cache/redis-cache.module.ts +++ b/apps/api/src/app/redis-cache/redis-cache.module.ts @@ -1,9 +1,9 @@ import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; +import { createKeyv } from '@keyv/redis'; import { CacheModule } from '@nestjs/cache-manager'; import { Module } from '@nestjs/common'; -import { redisStore } from 'cache-manager-redis-yet'; import type { RedisClientOptions } from 'redis'; import { RedisCacheService } from './redis-cache.service'; @@ -20,10 +20,13 @@ import { RedisCacheService } from './redis-cache.service'; ); return { - store: redisStore, - ttl: configurationService.get('CACHE_TTL'), - url: `redis://${redisPassword ? `:${redisPassword}` : ''}@${configurationService.get('REDIS_HOST')}:${configurationService.get('REDIS_PORT')}/${configurationService.get('REDIS_DB')}` - } as RedisClientOptions; + store: [ + createKeyv( + `redis://${redisPassword ? `:${redisPassword}` : ''}@${configurationService.get('REDIS_HOST')}:${configurationService.get('REDIS_PORT')}/${configurationService.get('REDIS_DB')}` + ) + ], + ttl: configurationService.get('CACHE_TTL') + }; } }), 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 a504d6cc0..b441b1b42 100644 --- a/apps/api/src/app/redis-cache/redis-cache.service.ts +++ b/apps/api/src/app/redis-cache/redis-cache.service.ts @@ -2,24 +2,17 @@ import { ConfigurationService } from '@ghostfolio/api/services/configuration/con import { getAssetProfileIdentifier } from '@ghostfolio/common/helper'; import { AssetProfileIdentifier, Filter } from '@ghostfolio/common/interfaces'; -import { CACHE_MANAGER } from '@nestjs/cache-manager'; -import { Inject, Injectable, Logger } from '@nestjs/common'; -import { RedisCache } from 'cache-manager-redis-yet'; +import { CACHE_MANAGER, Cache } from '@nestjs/cache-manager'; +import { Inject, Injectable } from '@nestjs/common'; import { createHash } from 'crypto'; import ms from 'ms'; @Injectable() export class RedisCacheService { public constructor( - @Inject(CACHE_MANAGER) private readonly cache: RedisCache, + @Inject(CACHE_MANAGER) private cache: Cache, private readonly configurationService: ConfigurationService - ) { - const client = cache.store.client; - - client.on('error', (error) => { - Logger.error(error, 'RedisCacheService'); - }); - } + ) {} public async get(key: string): Promise { return this.cache.get(key); @@ -32,7 +25,7 @@ export class RedisCacheService { prefix = `${prefix}*`; } - return this.cache.store.keys(prefix); + return this.cache.get(prefix); } public getPortfolioSnapshotKey({ @@ -61,10 +54,8 @@ export class RedisCacheService { public async isHealthy() { try { - const client = this.cache.store.client; - const isHealthy = await Promise.race([ - client.ping(), + this.cache, new Promise((_, reject) => setTimeout( () => reject(new Error('Redis health check timeout')), @@ -98,7 +89,7 @@ export class RedisCacheService { } public async reset() { - return this.cache.reset(); + return this.cache.clear(); } public async set(key: string, value: string, ttl?: number) { diff --git a/package-lock.json b/package-lock.json index 4c0d0bced..8df318a8b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,6 +29,7 @@ "@dfinity/principal": "0.15.7", "@dinero.js/currencies": "2.0.0-alpha.8", "@internationalized/number": "3.6.0", + "@keyv/redis": "^4.2.0", "@nestjs/bull": "11.0.1", "@nestjs/cache-manager": "3.0.0", "@nestjs/common": "11.0.6", @@ -6199,6 +6200,29 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@keyv/redis": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@keyv/redis/-/redis-4.2.0.tgz", + "integrity": "sha512-QszmBfZZ3wOKJ5z1hn0CTLf04WN/552ITrSDYC3Yg4jT6yVdlz2fJxi5CNrnZ8NIu/Qaj7OAkbSL+pyFUXp6oA==", + "license": "MIT", + "dependencies": { + "cluster-key-slot": "^1.1.2", + "keyv": "^5.2.2", + "redis": "^4.7.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/@keyv/redis/node_modules/keyv": { + "version": "5.2.3", + "resolved": "https://registry.npmjs.org/keyv/-/keyv-5.2.3.tgz", + "integrity": "sha512-AGKecUfzrowabUv0bH1RIR5Vf7w+l4S3xtQAypKaUpTdIR1EbrAcTxHCrpo9Q+IWeUlFE2palRtgIQcgm+PQJw==", + "license": "MIT", + "dependencies": { + "@keyv/serialize": "^1.0.2" + } + }, "node_modules/@keyv/serialize": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/@keyv/serialize/-/serialize-1.0.2.tgz", diff --git a/package.json b/package.json index 9120a9d19..54f66353b 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "@dfinity/principal": "0.15.7", "@dinero.js/currencies": "2.0.0-alpha.8", "@internationalized/number": "3.6.0", + "@keyv/redis": "^4.2.0", "@nestjs/bull": "11.0.1", "@nestjs/cache-manager": "3.0.0", "@nestjs/common": "11.0.6",