import Redis from "ioredis"

let _redis: Redis | undefined

export function getRedis(): Redis {
  if (!_redis) {
    const url = process.env.REDIS_URL
    if (!url) {
      throw new Error("REDIS_URL environment variable is required")
    }
    _redis = new Redis(url, {
      tls: { rejectUnauthorized: false },
      maxRetriesPerRequest: 3,
      retryStrategy(times) {
        return Math.min(times * 200, 2000)
      },
    })
  }
  return _redis
}
