Browse Source

migrate to keyv/redis

pull/4270/merge^2
HarukaKishida 2 months ago
parent
commit
2ec12d4e3f
  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. 24
      package-lock.json
  4. 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 { 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 { CacheModule } from '@nestjs/cache-manager'; import { CacheModule } from '@nestjs/cache-manager';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { redisStore } from 'cache-manager-redis-yet';
import type { RedisClientOptions } from 'redis'; import type { RedisClientOptions } from 'redis';
import { RedisCacheService } from './redis-cache.service'; import { RedisCacheService } from './redis-cache.service';
@ -20,10 +20,13 @@ import { RedisCacheService } from './redis-cache.service';
); );
return { return {
store: redisStore, store: [
ttl: configurationService.get('CACHE_TTL'), createKeyv(
url: `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')}`
} as RedisClientOptions; )
],
ttl: configurationService.get('CACHE_TTL')
};
} }
}), }),
ConfigurationModule 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 { getAssetProfileIdentifier } from '@ghostfolio/common/helper';
import { AssetProfileIdentifier, Filter } from '@ghostfolio/common/interfaces'; import { AssetProfileIdentifier, Filter } from '@ghostfolio/common/interfaces';
import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { CACHE_MANAGER, Cache } from '@nestjs/cache-manager';
import { Inject, Injectable, Logger } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { RedisCache } from 'cache-manager-redis-yet';
import { createHash } from 'crypto'; import { createHash } from 'crypto';
import ms from 'ms'; import ms from 'ms';
@Injectable() @Injectable()
export class RedisCacheService { export class RedisCacheService {
public constructor( public constructor(
@Inject(CACHE_MANAGER) private readonly cache: RedisCache, @Inject(CACHE_MANAGER) private cache: Cache,
private readonly configurationService: ConfigurationService private readonly configurationService: ConfigurationService
) { ) {}
const client = cache.store.client;
client.on('error', (error) => {
Logger.error(error, 'RedisCacheService');
});
}
public async get(key: string): Promise<string> { public async get(key: string): Promise<string> {
return this.cache.get(key); return this.cache.get(key);
@ -32,7 +25,7 @@ export class RedisCacheService {
prefix = `${prefix}*`; prefix = `${prefix}*`;
} }
return this.cache.store.keys(prefix); return this.cache.get(prefix);
} }
public getPortfolioSnapshotKey({ public getPortfolioSnapshotKey({
@ -61,10 +54,8 @@ export class RedisCacheService {
public async isHealthy() { public async isHealthy() {
try { try {
const client = this.cache.store.client;
const isHealthy = await Promise.race([ const isHealthy = await Promise.race([
client.ping(), this.cache,
new Promise((_, reject) => new Promise((_, reject) =>
setTimeout( setTimeout(
() => reject(new Error('Redis health check timeout')), () => reject(new Error('Redis health check timeout')),
@ -98,7 +89,7 @@ export class RedisCacheService {
} }
public async reset() { public async reset() {
return this.cache.reset(); return this.cache.clear();
} }
public async set(key: string, value: string, ttl?: number) { public async set(key: string, value: string, ttl?: number) {

24
package-lock.json

@ -29,6 +29,7 @@
"@dfinity/principal": "0.15.7", "@dfinity/principal": "0.15.7",
"@dinero.js/currencies": "2.0.0-alpha.8", "@dinero.js/currencies": "2.0.0-alpha.8",
"@internationalized/number": "3.6.0", "@internationalized/number": "3.6.0",
"@keyv/redis": "^4.2.0",
"@nestjs/bull": "11.0.1", "@nestjs/bull": "11.0.1",
"@nestjs/cache-manager": "3.0.0", "@nestjs/cache-manager": "3.0.0",
"@nestjs/common": "11.0.6", "@nestjs/common": "11.0.6",
@ -6199,6 +6200,29 @@
"@jridgewell/sourcemap-codec": "^1.4.14" "@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": { "node_modules/@keyv/serialize": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/@keyv/serialize/-/serialize-1.0.2.tgz", "resolved": "https://registry.npmjs.org/@keyv/serialize/-/serialize-1.0.2.tgz",

1
package.json

@ -75,6 +75,7 @@
"@dfinity/principal": "0.15.7", "@dfinity/principal": "0.15.7",
"@dinero.js/currencies": "2.0.0-alpha.8", "@dinero.js/currencies": "2.0.0-alpha.8",
"@internationalized/number": "3.6.0", "@internationalized/number": "3.6.0",
"@keyv/redis": "^4.2.0",
"@nestjs/bull": "11.0.1", "@nestjs/bull": "11.0.1",
"@nestjs/cache-manager": "3.0.0", "@nestjs/cache-manager": "3.0.0",
"@nestjs/common": "11.0.6", "@nestjs/common": "11.0.6",

Loading…
Cancel
Save