Browse Source

migrate to keyv/redis

pull/4270/head
HarukaKishida 6 months ago
committed by Thomas Kaul
parent
commit
7773cbf407
  1. 13
      apps/api/src/app/redis-cache/redis-cache.module.ts
  2. 23
      apps/api/src/app/redis-cache/redis-cache.service.ts
  3. 1
      package.json

13
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

23
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<string> {
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) {

1
package.json

@ -76,6 +76,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",

Loading…
Cancel
Save