| 3 | import { ICache } from './cache'; |
| 4 | |
| 5 | export class RedisCache implements ICache { |
| 6 | private client: Redis; |
| 7 | private static instance: RedisCache; |
| 8 | |
| 9 | constructor(redisUrl: string) { |
| 10 | this.client = new Redis(redisUrl); |
| 11 | } |
| 12 | |
| 13 | static getInstance(redisUrl: string): RedisCache { |
| 14 | if (!this.instance) { |
| 15 | this.instance = new RedisCache(redisUrl); |
| 16 | } |
| 17 | |
| 18 | return this.instance; |
| 19 | } |
| 20 | |
| 21 | async set( |
| 22 | type: string, |
| 23 | args: string[], |
| 24 | value: any, |
| 25 | expirySeconds: number, |
| 26 | ): Promise<void> { |
| 27 | const key = this.generateKey(type, args); |
| 28 | if (expirySeconds) { |
| 29 | await this.client.set(key, JSON.stringify(value), 'EX', expirySeconds); |
| 30 | } else { |
| 31 | await this.client.set(key, JSON.stringify(value)); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | async get(type: string, args: string[]): Promise<any> { |
| 36 | const key = this.generateKey(type, args); |
| 37 | const value = await this.client.get(key); |
| 38 | return value ? JSON.parse(value) : null; |
| 39 | } |
| 40 | |
| 41 | async evict(type: string, args: string[]): Promise<null> { |
| 42 | const key = this.generateKey(type, args); |
| 43 | await this.client.del(key); |
| 44 | return null; |
| 45 | } |
| 46 | |
| 47 | private generateKey(type: string, args: string[]): string { |
| 48 | return `${type}:${args.join(':')}`; |
| 49 | } |
| 50 | } |
nothing calls this directly
no outgoing calls
no test coverage detected