-
Notifications
You must be signed in to change notification settings - Fork 3
/
redisKvCacheService.ts
52 lines (50 loc) · 1.7 KB
/
redisKvCacheService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Logger } from '@/utils/logger/Logger';
import { Redis } from 'ioredis';
export class RedisKvCacheService {
private static instance: RedisKvCacheService;
private redis: Redis;
private logger = new Logger('RedisKvCacheService');
private constructor() {
const host = process.env.REDIS_HOST;
const port_str = process.env.REDIS_PORT;
if (typeof host !== 'string' || typeof port_str !== 'string') {
throw new Error('Redis host or port not provided!');
}
const port = Number.parseInt(port_str);
this.redis = new Redis({
host: host,
port: port,
});
}
public static getInstance() {
if (!RedisKvCacheService.instance) {
RedisKvCacheService.instance = new RedisKvCacheService();
}
return RedisKvCacheService.instance;
}
public async get<T>(
fn: () => Promise<T>,
cacheConfig: { key: string; ttl: number },
noCache: boolean = false,
): Promise<T> {
const { key, ttl } = cacheConfig;
const cached_data = await this.redis.get('cache:' + key);
if (cached_data === null || noCache) {
const data = await fn();
const res = await this.redis.setex('cache:' + key, ttl, JSON.stringify(data));
if (res !== 'OK') {
this.logger.error('Redis Cache Save Fail!');
throw new Error('Redis Save Fail');
}
this.logger.debug(`Return data without cache (Cache-Miss or Bypass) Key: ${key}`);
return data;
} else {
this.logger.debug(`Return data with cache (Cache-Hit) Key: ${key}`);
return JSON.parse(cached_data) as T;
}
}
public async drop(key: string) {
const dropped = await this.redis.del('cache:' + key);
this.logger.debug(`Drop Cache ${key}. dropped ${dropped}`);
}
}