From 34f19a7e00bbad9d2d51b4a11cbd59c4f3d0adbf Mon Sep 17 00:00:00 2001 From: atridadl Date: Fri, 1 Dec 2023 16:15:21 -0700 Subject: [PATCH] Wowwww --- app/routes/api.room.create.tsx | 2 +- app/routes/api.room.delete.$roomId.tsx | 7 ++++++- app/routes/api.room.get.all.tsx | 8 ++++---- app/services/redis.server.ts | 14 ++++++++------ 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/app/routes/api.room.create.tsx b/app/routes/api.room.create.tsx index 6ed0403..5af9d47 100644 --- a/app/routes/api.room.create.tsx +++ b/app/routes/api.room.create.tsx @@ -34,7 +34,7 @@ export async function action({ request, params, context }: ActionFunctionArgs) { const success = room.length > 0; if (success) { - await invalidateCache(`kv_roomlist_${userId}`); + await invalidateCache(`kv_roomlist_${userId}`, "sp"); emitter.emit("nodes", "roomlist"); return json(room, { diff --git a/app/routes/api.room.delete.$roomId.tsx b/app/routes/api.room.delete.$roomId.tsx index ce10b25..d1f7c92 100644 --- a/app/routes/api.room.delete.$roomId.tsx +++ b/app/routes/api.room.delete.$roomId.tsx @@ -33,7 +33,7 @@ export async function action({ request, params, context }: ActionFunctionArgs) { const success = deletedRoom.length > 0; if (success) { - await invalidateCache(`kv_roomlist_${userId}`); + await invalidateCache(`kv_roomlist_${userId}`, "sp"); emitter.emit("nodes", "roomlist"); return json(deletedRoom, { @@ -41,4 +41,9 @@ export async function action({ request, params, context }: ActionFunctionArgs) { statusText: "SUCCESS", }); } + + return json(null, { + status: 404, + statusText: "NOT FOUND", + }); } diff --git a/app/routes/api.room.get.all.tsx b/app/routes/api.room.get.all.tsx index f157a0c..8309706 100644 --- a/app/routes/api.room.get.all.tsx +++ b/app/routes/api.room.get.all.tsx @@ -26,7 +26,7 @@ export async function loader({ context, params, request }: LoaderFunctionArgs) { createdAt: Date; roomName: string; }[] - >(`kv_roomlist_${userId}`).then((cachedResult) => { + >(`kv_roomlist_${userId}`, "sp").then((cachedResult) => { if (cachedResult) { send({ event: userId!, data: JSON.stringify(cachedResult) }); } else { @@ -35,7 +35,7 @@ export async function loader({ context, params, request }: LoaderFunctionArgs) { where: eq(rooms.userId, userId || ""), }) .then((roomList) => { - setCache(`kv_roomlist_${userId}`, roomList).then(() => { + setCache(`kv_roomlist_${userId}`, roomList, "sp").then(() => { send({ event: userId!, data: JSON.stringify(roomList) }); }); }); @@ -50,7 +50,7 @@ export async function loader({ context, params, request }: LoaderFunctionArgs) { createdAt: Date; roomName: string; }[] - >(`kv_roomlist_${userId}`).then((cachedResult) => { + >(`kv_roomlist_${userId}`, "sp").then((cachedResult) => { if (cachedResult) { send({ event: userId!, data: JSON.stringify(cachedResult) }); } else { @@ -59,7 +59,7 @@ export async function loader({ context, params, request }: LoaderFunctionArgs) { where: eq(rooms.userId, userId || ""), }) .then((roomList) => { - setCache(`kv_roomlist_${userId}`, roomList).then(() => { + setCache(`kv_roomlist_${userId}`, roomList, "sp").then(() => { send({ event: userId!, data: JSON.stringify(roomList) }); }); }); diff --git a/app/services/redis.server.ts b/app/services/redis.server.ts index 25f5dcc..b321b5a 100644 --- a/app/services/redis.server.ts +++ b/app/services/redis.server.ts @@ -47,27 +47,29 @@ export const unsubscribeToChannel = (channel: string) => { Promise.resolve([sub?.unsubscribe(channel)]); }; -export const setCache = async (key: string, value: T) => { +export const setCache = async (key: string, value: T, prefix?: string) => { try { - await cache?.set(key, JSON.stringify(value)); + await cache?.set(prefix ? `${prefix}_${key}` : key, JSON.stringify(value)); return true; } catch { return false; } }; -export const fetchCache = async (key: string) => { +export const fetchCache = async (key: string, prefix?: string) => { try { - const result = (await cache?.get(key)) as string; + const result = (await cache?.get( + prefix ? `${prefix}_${key}` : key + )) as string; return JSON.parse(result) as T; } catch { return null; } }; -export const invalidateCache = async (key: string) => { +export const invalidateCache = async (key: string, prefix?: string) => { try { - await cache?.del(key); + await cache?.del(prefix ? `${prefix}_${key}` : key); return true; } catch { return false;