diff --git a/lib/crud.ts b/lib/crud.ts index ae9af90..3075c6a 100644 --- a/lib/crud.ts +++ b/lib/crud.ts @@ -12,6 +12,7 @@ import { SILVERBULLET_SERVER } from "@lib/env.ts"; import { imageTable } from "@lib/db/schema.ts"; import { db } from "@lib/db/sqlite.ts"; import { eq } from "drizzle-orm/sql"; +import { createCache } from "@lib/cache.ts"; export async function addThumbnailToResource( res: T, @@ -70,6 +71,8 @@ export function createCrud( parse: (doc: string, id: string) => T; }, ) { + const cache = createCache(`crud/${prefix}`, { expires: 60 * 1000 }); + function pathFromId(id: string) { return `${prefix}${id.replaceAll(":", "")}.md`; } @@ -93,6 +96,7 @@ export function createCrud( } function create(id: string, content: string | ArrayBuffer | T) { const path = pathFromId(id); + cache.set("all", undefined); if ( typeof content === "string" || content instanceof ArrayBuffer ) { @@ -118,6 +122,9 @@ export function createCrud( } async function readAll({ sort = "rating" }: { sort?: SortType } = {}) { + if (cache.has("all")) { + return cache.get("all") as unknown as T[]; + } const allDocuments = await getDocuments(); const parsed = (await Promise.all( allDocuments.filter((d) => {