| 5 | const redisUrl = process.env.REDIS_URL; |
| 6 | |
| 7 | export class Cache implements ICache { |
| 8 | private static instance: Cache; |
| 9 | private delegate: ICache; |
| 10 | |
| 11 | private constructor() { |
| 12 | if (redisUrl) { |
| 13 | this.delegate = RedisCache.getInstance(redisUrl); |
| 14 | } else { |
| 15 | this.delegate = InMemoryCache.getInstance(); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | static getInstance(): Cache { |
| 20 | if (!this.instance) { |
| 21 | this.instance = new Cache(); |
| 22 | } |
| 23 | return this.instance; |
| 24 | } |
| 25 | |
| 26 | async set( |
| 27 | type: string, |
| 28 | args: string[], |
| 29 | value: any, |
| 30 | expirySeconds: number = parseInt(process.env.CACHE_EXPIRE_S || '100', 10), |
| 31 | ): Promise<void> { |
| 32 | return this.delegate.set(type, args, value, expirySeconds); |
| 33 | } |
| 34 | |
| 35 | async get(type: string, args: string[]): Promise<any> { |
| 36 | return this.delegate.get(type, args); |
| 37 | } |
| 38 | |
| 39 | async evict(type: string, args: string[]): Promise<null> { |
| 40 | return this.delegate.evict(type, args); |
| 41 | } |
| 42 | } |
| 43 | export const cache = Cache.getInstance(); |
nothing calls this directly
no outgoing calls
no test coverage detected