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

24
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",

1
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",

Loading…
Cancel
Save