import Redis from 'ioredis'; import { logger } from '../logger'; function makeRedis(bullmq = false) { const client = process.env.REDIS_URL ? new Redis(process.env.REDIS_URL, { maxRetriesPerRequest: bullmq ? null : 3 }) : new Redis({ host: process.env.REDIS_HOST || 'localhost', port: parseInt(process.env.REDIS_PORT || '6379'), username: process.env.REDIS_USERNAME || 'default', password: process.env.REDIS_PASSWORD || undefined, tls: process.env.REDIS_TLS === 'true' ? {} : undefined, maxRetriesPerRequest: bullmq ? null : 3, }); client.on('error', (err) => logger.error({ err }, `[REDIS] ${bullmq ? 'BullMQ' : 'Cache'} connection error`)); return client; } /** Standard caching client (organization, normalization, meta-status) */ export const redis = makeRedis(false); /** BullMQ-compatible client — maxRetriesPerRequest: null required by BullMQ */ export const redisForBullMQ = makeRedis(true); /** Dedicated subscriber for SSE — each sub needs its own connection */ export function createSubscriber() { return makeRedis(false); } /** Channel name for org-scoped real-time events */ export const orgChannel = (orgId: string) => `org:${orgId}:events`;