2023-04-23 12:02:01 +02:00
|
|
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
|
2023-06-24 13:06:28 +02:00
|
|
|
import { UniqueAsset } from '@ghostfolio/common/interfaces';
|
2021-04-13 21:53:58 +02:00
|
|
|
import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Cache } from 'cache-manager';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class RedisCacheService {
|
2021-04-18 19:06:54 +02:00
|
|
|
public constructor(
|
|
|
|
@Inject(CACHE_MANAGER) private readonly cache: Cache,
|
|
|
|
private readonly configurationService: ConfigurationService
|
|
|
|
) {}
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
public async get(key: string): Promise<string> {
|
|
|
|
return await this.cache.get(key);
|
|
|
|
}
|
|
|
|
|
2023-06-24 13:06:28 +02:00
|
|
|
public getQuoteKey({ dataSource, symbol }: UniqueAsset) {
|
|
|
|
return `quote-${dataSource}-${symbol}`;
|
|
|
|
}
|
|
|
|
|
2021-04-13 21:53:58 +02:00
|
|
|
public async remove(key: string) {
|
|
|
|
await this.cache.del(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async reset() {
|
|
|
|
await this.cache.reset();
|
|
|
|
}
|
|
|
|
|
2021-11-08 20:55:38 +01:00
|
|
|
public async set(key: string, value: string, ttlInSeconds?: number) {
|
2021-04-18 19:06:54 +02:00
|
|
|
await this.cache.set(key, value, {
|
2021-11-08 20:55:38 +01:00
|
|
|
ttl: ttlInSeconds ?? this.configurationService.get('CACHE_TTL')
|
2021-04-18 19:06:54 +02:00
|
|
|
});
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
|
|
|
}
|