Merge branch 'dev'
This commit is contained in:
commit
b6b30de3b5
4 changed files with 19 additions and 12 deletions
|
@ -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, {
|
||||
|
|
|
@ -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",
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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) });
|
||||
});
|
||||
});
|
||||
|
|
|
@ -47,27 +47,29 @@ export const unsubscribeToChannel = (channel: string) => {
|
|||
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 {
|
||||
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 <T>(key: string) => {
|
||||
export const fetchCache = async <T>(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;
|
||||
|
|
Loading…
Add table
Reference in a new issue