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

35 lines
1000 B
TypeScript
Raw Normal View History

2023-04-23 12:02:01 +02:00
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
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 {
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);
}
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) {
await this.cache.set(key, value, {
2021-11-08 20:55:38 +01:00
ttl: ttlInSeconds ?? this.configurationService.get('CACHE_TTL')
});
2021-04-13 21:53:58 +02:00
}
}