redis.ts32 lines · main
| 1 | import Redis from 'ioredis'; |
| 2 | |
| 3 | import { env } from '../env.js'; |
| 4 | import { log } from './logger.js'; |
| 5 | |
| 6 | let _redis: Redis | null = null; |
| 7 | |
| 8 | export function getRedis(): Redis | null { |
| 9 | if (!env.BRIVEN_REDIS_URL) return null; |
| 10 | if (!_redis) { |
| 11 | _redis = new Redis(env.BRIVEN_REDIS_URL, { |
| 12 | maxRetriesPerRequest: 2, |
| 13 | enableReadyCheck: true, |
| 14 | lazyConnect: false, |
| 15 | }); |
| 16 | _redis.on('error', (err) => { |
| 17 | log.warn('redis_error', { message: err.message }); |
| 18 | }); |
| 19 | } |
| 20 | return _redis; |
| 21 | } |
| 22 | |
| 23 | export async function pingRedis(): Promise<boolean> { |
| 24 | const r = getRedis(); |
| 25 | if (!r) return false; |
| 26 | try { |
| 27 | const reply = await r.ping(); |
| 28 | return reply === 'PONG'; |
| 29 | } catch { |
| 30 | return false; |
| 31 | } |
| 32 | } |