| import { createLoggerWithContext } from "@midday/logger"; |
|
|
| const logger = createLoggerWithContext("redis"); |
|
|
| let sharedRedisClient: any = null; |
| let RedisClientClass: any = null; |
|
|
| |
| try { |
| ({ RedisClient: RedisClientClass } = require("bun")); |
| } catch { |
| |
| } |
|
|
| |
| |
| |
| |
| |
| function resolveRedisUrl(): string | undefined { |
| return process.env.REDIS_URL; |
| } |
|
|
| |
| |
| |
| |
| export function getSharedRedisClient(): any { |
| if (sharedRedisClient) { |
| return sharedRedisClient; |
| } |
|
|
| if (!RedisClientClass) { |
| return null; |
| } |
|
|
| const redisUrl = resolveRedisUrl(); |
|
|
| if (!redisUrl) { |
| throw new Error( |
| "Redis URL not found. Set per-region REDIS_CACHE_* env vars or REDIS_URL.", |
| ); |
| } |
|
|
| const isProduction = |
| process.env.NODE_ENV === "production" || |
| process.env.RAILWAY_ENVIRONMENT === "production"; |
|
|
| sharedRedisClient = new RedisClientClass(redisUrl, { |
| connectionTimeout: isProduction ? 10000 : 5000, |
| autoReconnect: true, |
| maxRetries: 10, |
| enableOfflineQueue: true, |
| enableAutoPipelining: true, |
| }); |
|
|
| sharedRedisClient.onclose = (err: Error) => { |
| if (err) { |
| logger.error("Connection closed", { error: err.message }); |
| } |
| }; |
|
|
| |
| sharedRedisClient.connect().catch((err: Error) => { |
| logger.error("Initial connection error", { error: err.message }); |
| }); |
|
|
| return sharedRedisClient; |
| } |
|
|