From 3ceeda1b3c46ed44a8dfe182218ff4700c4addc2 Mon Sep 17 00:00:00 2001 From: atridadl Date: Mon, 4 Dec 2023 23:18:24 -0700 Subject: [PATCH 1/2] small redis optimizations --- app/services/redis.server.ts | 83 +++++++++++++++++++++++++++++------- 1 file changed, 68 insertions(+), 15 deletions(-) diff --git a/app/services/redis.server.ts b/app/services/redis.server.ts index b321b5a..b40f65c 100644 --- a/app/services/redis.server.ts +++ b/app/services/redis.server.ts @@ -1,22 +1,75 @@ 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 isConnectedToCache = process.env.REDIS_URL +// ? cache?.status === "ready" +// : true; +// export const isConnectedToPub = process.env.REDIS_URL +// ? pub?.status === "ready" +// : true; +// export const isConnectedToSub = process.env.REDIS_URL +// ? sub?.status === "ready" +// : true; export const publishToChannel = async (channel: string, message: string) => { await pub?.publish(channel, JSON.stringify(message)); From 9cdb71a1104ae941529ed765a0a04e2dc38af6f3 Mon Sep 17 00:00:00 2001 From: atridadl Date: Mon, 4 Dec 2023 23:19:03 -0700 Subject: [PATCH 2/2] remove comment --- app/services/redis.server.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/app/services/redis.server.ts b/app/services/redis.server.ts index b40f65c..0b7d9b5 100644 --- a/app/services/redis.server.ts +++ b/app/services/redis.server.ts @@ -61,16 +61,6 @@ if (process.env.NODE_ENV === "production") { sub = global.__sub; } -// export const isConnectedToCache = process.env.REDIS_URL -// ? cache?.status === "ready" -// : true; -// export const isConnectedToPub = process.env.REDIS_URL -// ? pub?.status === "ready" -// : true; -// export const isConnectedToSub = process.env.REDIS_URL -// ? sub?.status === "ready" -// : true; - export const publishToChannel = async (channel: string, message: string) => { await pub?.publish(channel, JSON.stringify(message)); };