diff --git a/app/services/redis.server.ts b/app/services/redis.server.ts index b321b5a..0b7d9b5 100644 --- a/app/services/redis.server.ts +++ b/app/services/redis.server.ts @@ -1,22 +1,65 @@ import Redis from "ioredis"; -export const cache = process.env.REDIS_URL - ? new Redis(process.env.REDIS_URL, { - family: 6, - }) - : null; +let cache: Redis | null = null; +let pub: Redis | null = null; +let sub: Redis | null = null; -export const pub = process.env.REDIS_URL - ? new Redis(process.env.REDIS_URL, { - family: 6, - }) - : null; +declare global { + var __cache: Redis | null; + var __pub: Redis | null; + var __sub: Redis | null; +} -export const sub = process.env.REDIS_URL - ? new Redis(process.env.REDIS_URL, { - family: 6, - }) - : null; +if (process.env.NODE_ENV === "production") { + cache = process.env.REDIS_URL + ? new Redis(process.env.REDIS_URL, { + family: 6, + }) + : null; +} else { + if (!global.__cache) { + global.__cache = process.env.REDIS_URL + ? new Redis(process.env.REDIS_URL, { + family: 6, + }) + : null; + } + cache = global.__cache; +} + +if (process.env.NODE_ENV === "production") { + pub = process.env.REDIS_URL + ? new Redis(process.env.REDIS_URL, { + family: 6, + }) + : null; +} else { + if (!global.__pub) { + global.__pub = process.env.REDIS_URL + ? new Redis(process.env.REDIS_URL, { + family: 6, + }) + : null; + } + pub = global.__pub; +} + +if (process.env.NODE_ENV === "production") { + sub = process.env.REDIS_URL + ? new Redis(process.env.REDIS_URL, { + family: 6, + }) + : null; +} else { + if (!global.__sub) { + global.__sub = process.env.REDIS_URL + ? new Redis(process.env.REDIS_URL, { + family: 6, + }) + : null; + } + sub = global.__sub; +} export const publishToChannel = async (channel: string, message: string) => { await pub?.publish(channel, JSON.stringify(message));