// https://makerkit.dev/blog/tutorials/nextjs-redis (I got tired of having redis issues) import Redis, { RedisOptions } from "ioredis"; const configuration = { redis: { host: process.env.REDIS_HOST ?? "localhost", port: parseInt(process.env.REDIS_PORT ?? "6379"), password: process.env.REDIS_PASSWORD ?? null, }, }; function getRedisConfiguration(): { port: number; host: string; password: string | null; } { return configuration.redis; } export function createRedisInstance(config = getRedisConfiguration()) { try { const options: RedisOptions = { host: config.host, lazyConnect: true, showFriendlyErrorStack: true, enableAutoPipelining: true, maxRetriesPerRequest: 0, retryStrategy: (times: number) => { if (times > 3) { throw new Error(`[Redis] Could not connect after ${times} attempts`); } return Math.min(times * 200, 1000); }, }; if (config.port) { options.port = config.port; } if (config.password) { options.password = config.password; } const redis = new Redis(options); redis.on("error", (error: unknown) => { console.warn("[Redis] Error connecting", error); }); return redis; } catch (e) { throw new Error(`[Redis] Could not create a Redis instance`); } }