2023-04-23 12:02:01 +02:00
|
|
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
|
2023-08-01 09:10:13 +02:00
|
|
|
import { getAssetProfileIdentifier } from '@ghostfolio/common/helper';
|
2023-06-24 13:06:28 +02:00
|
|
|
import { UniqueAsset } from '@ghostfolio/common/interfaces';
|
2023-08-05 11:08:10 +02:00
|
|
|
import { CACHE_MANAGER } from '@nestjs/cache-manager';
|
|
|
|
import { Inject, Injectable, Logger } from '@nestjs/common';
|
2023-07-26 20:30:32 +02:00
|
|
|
|
|
|
|
import type { RedisCache } from './interfaces/redis-cache.interface';
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class RedisCacheService {
|
2021-04-18 19:06:54 +02:00
|
|
|
public constructor(
|
2023-07-26 20:30:32 +02:00
|
|
|
@Inject(CACHE_MANAGER) private readonly cache: RedisCache,
|
2021-04-18 19:06:54 +02:00
|
|
|
private readonly configurationService: ConfigurationService
|
2023-07-26 20:30:32 +02:00
|
|
|
) {
|
|
|
|
const client = cache.store.getClient();
|
|
|
|
|
|
|
|
client.on('error', (error) => {
|
|
|
|
Logger.error(error, 'RedisCacheService');
|
|
|
|
});
|
|
|
|
}
|
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) {
|
2023-08-01 09:10:13 +02:00
|
|
|
return `quote-${getAssetProfileIdentifier({ dataSource, symbol })}`;
|
2023-06-24 13:06:28 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2023-08-05 11:08:10 +02:00
|
|
|
await this.cache.set(
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
ttlInSeconds ?? this.configurationService.get('CACHE_TTL')
|
|
|
|
);
|
2021-04-13 21:53:58 +02:00
|
|
|
}
|
|
|
|
}
|