Browse Source

fix:Adapting keyv store

pull/4270/head
HarukaKishida 6 months ago
committed by Thomas Kaul
parent
commit
81752360b6
  1. 18
      apps/api/src/app/redis-cache/redis-cache.module.ts
  2. 26
      apps/api/src/app/redis-cache/redis-cache.service.ts

18
apps/api/src/app/redis-cache/redis-cache.module.ts

@ -1,17 +1,16 @@
import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; 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 { CacheModule } from '@nestjs/cache-manager';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import type { RedisClientOptions } from 'redis';
import { RedisCacheService } from './redis-cache.service'; import { RedisCacheService } from './redis-cache.service';
@Module({ @Module({
exports: [RedisCacheService], exports: [RedisCacheService],
imports: [ imports: [
CacheModule.registerAsync<RedisClientOptions>({ CacheModule.registerAsync<Keyv>({
imports: [ConfigurationModule], imports: [ConfigurationModule],
inject: [ConfigurationService], inject: [ConfigurationService],
useFactory: async (configurationService: ConfigurationService) => { useFactory: async (configurationService: ConfigurationService) => {
@ -20,13 +19,14 @@ import { RedisCacheService } from './redis-cache.service';
); );
return { return {
store: [ store: new Keyv({
createKeyv( store: new KeyvRedis(
`redis://${redisPassword ? `:${redisPassword}` : ''}@${configurationService.get('REDIS_HOST')}:${configurationService.get('REDIS_PORT')}/${configurationService.get('REDIS_DB')}` `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 ConfigurationModule

26
apps/api/src/app/redis-cache/redis-cache.service.ts

@ -19,13 +19,17 @@ export class RedisCacheService {
} }
public async getKeys(aPrefix?: string): Promise<string[]> { public async getKeys(aPrefix?: string): Promise<string[]> {
let prefix = aPrefix; const prefix = aPrefix;
const keyList = [];
if (prefix) { this.cache.stores[0].iterator((key) => {
prefix = `${prefix}*`; if (prefix && key.startsWith(prefix)) {
} keyList.push(key);
} else if (!prefix) {
return this.cache.get(prefix); keyList.push(key);
}
});
return keyList;
} }
public getPortfolioSnapshotKey({ public getPortfolioSnapshotKey({
@ -54,8 +58,9 @@ export class RedisCacheService {
public async isHealthy() { public async isHealthy() {
try { try {
const client = this.cache.stores[0];
const isHealthy = await Promise.race([ const isHealthy = await Promise.race([
this.cache, client.ttl,
new Promise((_, reject) => new Promise((_, reject) =>
setTimeout( setTimeout(
() => reject(new Error('Redis health check timeout')), () => reject(new Error('Redis health check timeout')),
@ -82,10 +87,7 @@ export class RedisCacheService {
const keys = await this.getKeys( const keys = await this.getKeys(
`${this.getPortfolioSnapshotKey({ userId })}` `${this.getPortfolioSnapshotKey({ userId })}`
); );
this.cache.mdel(keys);
for (const key of keys) {
await this.remove(key);
}
} }
public async reset() { public async reset() {

Loading…
Cancel
Save