(
name: string,
url: string,
overrides: RedisOptions = {},
)
| 20 | } |
| 21 | |
| 22 | const createRedisClient = ( |
| 23 | name: string, |
| 24 | url: string, |
| 25 | overrides: RedisOptions = {}, |
| 26 | ): ExtendedRedis => { |
| 27 | const client = new Redis(url, { |
| 28 | ...options, |
| 29 | ...overrides, |
| 30 | }) as ExtendedRedis; |
| 31 | |
| 32 | client.on('error', (error) => { |
| 33 | console.error(`[${name}] Redis Client Error:`, error); |
| 34 | }); |
| 35 | |
| 36 | client.getJson = async <T = any>(key: string): Promise<T | null> => { |
| 37 | const value = await client.get(key); |
| 38 | if (value) { |
| 39 | const res = getSuperJson(value) as T; |
| 40 | if (res && Array.isArray(res) && res.length === 0) { |
| 41 | return null; |
| 42 | } |
| 43 | |
| 44 | if (res && typeof res === 'object' && Object.keys(res).length === 0) { |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | if (res) { |
| 49 | return res; |
| 50 | } |
| 51 | } |
| 52 | return null; |
| 53 | }; |
| 54 | |
| 55 | client.setJson = async <T = any>( |
| 56 | key: string, |
| 57 | expireInSec: number, |
| 58 | value: T, |
| 59 | ): Promise<void> => { |
| 60 | await client.setex(key, expireInSec, setSuperJson(value)); |
| 61 | }; |
| 62 | |
| 63 | return client; |
| 64 | }; |
| 65 | |
| 66 | let redisCache: ExtendedRedis; |
| 67 | export function getRedisCache() { |
no test coverage detected