From eb69412fd17e564c25e9742af34a28804df5cd69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Sat, 16 Mar 2024 20:58:39 -0400 Subject: [PATCH] perf(api): tweak `cachedEventHandler` parameters --- server/api/sidebar/index.get.ts | 16 ++++++++++++---- server/api/user/me/stories.get.ts | 26 +++++++++++++++++--------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/server/api/sidebar/index.get.ts b/server/api/sidebar/index.get.ts index 9e4d0f6..21dea2e 100644 --- a/server/api/sidebar/index.get.ts +++ b/server/api/sidebar/index.get.ts @@ -1,6 +1,14 @@ import { SidebarItem } from "@models/sidebarEntry"; -export default cachedEventHandler(async (ev) => { - const si = await SidebarItem.find({}); - return si.map((a) => a.toObject()); -}); +export default cachedEventHandler( + async (ev) => { + const si = await SidebarItem.find({}); + return si.map((a) => a.toObject()); + }, + { + getKey(event) { + return "sidebar"; + }, + maxAge: 60 * 10, + }, +); diff --git a/server/api/user/me/stories.get.ts b/server/api/user/me/stories.get.ts index 7e7cdf7..774095d 100644 --- a/server/api/user/me/stories.get.ts +++ b/server/api/user/me/stories.get.ts @@ -1,11 +1,19 @@ import { listQuerier } from "@server/dbHelpers"; import { isLoggedIn } from "@server/middlewareButNotReally"; -export default cachedEventHandler(async (ev) => { - isLoggedIn(ev); - const id = ev.context.currentUser!._id; - let s = await listQuerier({ author: id }, ev.context, ev); - return { - stories: s.stories, - total: s.stories.length, - }; -}); +export default cachedEventHandler( + async (ev) => { + isLoggedIn(ev); + const id = ev.context.currentUser!._id; + let s = await listQuerier({ author: { $in: [id] } }, ev.context, ev); + return { + stories: s?.stories || [], + total: s?.stories.length || 0, + }; + }, + { + varies: ["Cookie"], + getKey(event) { + return `${event.context.currentUser?._id || null}/stories`; + }, + }, +);