fix: dont double cache documents

This commit is contained in:
max_richter 2025-01-25 18:57:06 +01:00
parent 23f33b7472
commit 4ff7ef7b5c

View File

@ -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<T extends GenericResource>(
res: T,
@ -70,6 +71,8 @@ export function createCrud<T extends GenericResource>(
parse: (doc: string, id: string) => T;
},
) {
const cache = createCache<T>(`crud/${prefix}`, { expires: 60 * 1000 });
function pathFromId(id: string) {
return `${prefix}${id.replaceAll(":", "")}.md`;
}
@ -93,6 +96,7 @@ export function createCrud<T extends GenericResource>(
}
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<T extends GenericResource>(
}
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) => {