import Redis from "ioredis"; let redis: Redis | null = null; export function getRedis(): Redis | null { const url = process.env.REDIS_URL; if (!url) return null; if (!redis) { redis = new Redis(url, { maxRetriesPerRequest: 3, lazyConnect: true }); } return redis; } export async function cacheGet(key: string): Promise { const r = getRedis(); if (!r) return null; try { const val = await r.get(key); return val ? (JSON.parse(val) as T) : null; } catch { return null; } } export async function cacheSet(key: string, value: unknown, ttlSec = 3600) { const r = getRedis(); if (!r) return; try { await r.setex(key, ttlSec, JSON.stringify(value)); } catch { /* ignore */ } }