Merge branch 'dev'

This commit is contained in:
Atridad Lahiji 2023-12-01 16:15:50 -07:00
commit b6b30de3b5
No known key found for this signature in database
4 changed files with 19 additions and 12 deletions

View file

@ -34,7 +34,7 @@ export async function action({ request, params, context }: ActionFunctionArgs) {
const success = room.length > 0; const success = room.length > 0;
if (success) { if (success) {
await invalidateCache(`kv_roomlist_${userId}`); await invalidateCache(`kv_roomlist_${userId}`, "sp");
emitter.emit("nodes", "roomlist"); emitter.emit("nodes", "roomlist");
return json(room, { return json(room, {

View file

@ -33,7 +33,7 @@ export async function action({ request, params, context }: ActionFunctionArgs) {
const success = deletedRoom.length > 0; const success = deletedRoom.length > 0;
if (success) { if (success) {
await invalidateCache(`kv_roomlist_${userId}`); await invalidateCache(`kv_roomlist_${userId}`, "sp");
emitter.emit("nodes", "roomlist"); emitter.emit("nodes", "roomlist");
return json(deletedRoom, { return json(deletedRoom, {
@ -41,4 +41,9 @@ export async function action({ request, params, context }: ActionFunctionArgs) {
statusText: "SUCCESS", statusText: "SUCCESS",
}); });
} }
return json(null, {
status: 404,
statusText: "NOT FOUND",
});
} }

View file

@ -26,7 +26,7 @@ export async function loader({ context, params, request }: LoaderFunctionArgs) {
createdAt: Date; createdAt: Date;
roomName: string; roomName: string;
}[] }[]
>(`kv_roomlist_${userId}`).then((cachedResult) => { >(`kv_roomlist_${userId}`, "sp").then((cachedResult) => {
if (cachedResult) { if (cachedResult) {
send({ event: userId!, data: JSON.stringify(cachedResult) }); send({ event: userId!, data: JSON.stringify(cachedResult) });
} else { } else {
@ -35,7 +35,7 @@ export async function loader({ context, params, request }: LoaderFunctionArgs) {
where: eq(rooms.userId, userId || ""), where: eq(rooms.userId, userId || ""),
}) })
.then((roomList) => { .then((roomList) => {
setCache(`kv_roomlist_${userId}`, roomList).then(() => { setCache(`kv_roomlist_${userId}`, roomList, "sp").then(() => {
send({ event: userId!, data: JSON.stringify(roomList) }); send({ event: userId!, data: JSON.stringify(roomList) });
}); });
}); });
@ -50,7 +50,7 @@ export async function loader({ context, params, request }: LoaderFunctionArgs) {
createdAt: Date; createdAt: Date;
roomName: string; roomName: string;
}[] }[]
>(`kv_roomlist_${userId}`).then((cachedResult) => { >(`kv_roomlist_${userId}`, "sp").then((cachedResult) => {
if (cachedResult) { if (cachedResult) {
send({ event: userId!, data: JSON.stringify(cachedResult) }); send({ event: userId!, data: JSON.stringify(cachedResult) });
} else { } else {
@ -59,7 +59,7 @@ export async function loader({ context, params, request }: LoaderFunctionArgs) {
where: eq(rooms.userId, userId || ""), where: eq(rooms.userId, userId || ""),
}) })
.then((roomList) => { .then((roomList) => {
setCache(`kv_roomlist_${userId}`, roomList).then(() => { setCache(`kv_roomlist_${userId}`, roomList, "sp").then(() => {
send({ event: userId!, data: JSON.stringify(roomList) }); send({ event: userId!, data: JSON.stringify(roomList) });
}); });
}); });

View file

@ -47,27 +47,29 @@ export const unsubscribeToChannel = (channel: string) => {
Promise.resolve([sub?.unsubscribe(channel)]); Promise.resolve([sub?.unsubscribe(channel)]);
}; };
export const setCache = async <T>(key: string, value: T) => { export const setCache = async <T>(key: string, value: T, prefix?: string) => {
try { try {
await cache?.set(key, JSON.stringify(value)); await cache?.set(prefix ? `${prefix}_${key}` : key, JSON.stringify(value));
return true; return true;
} catch { } catch {
return false; return false;
} }
}; };
export const fetchCache = async <T>(key: string) => { export const fetchCache = async <T>(key: string, prefix?: string) => {
try { 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; return JSON.parse(result) as T;
} catch { } catch {
return null; return null;
} }
}; };
export const invalidateCache = async (key: string) => { export const invalidateCache = async (key: string, prefix?: string) => {
try { try {
await cache?.del(key); await cache?.del(prefix ? `${prefix}_${key}` : key);
return true; return true;
} catch { } catch {
return false; return false;