From 23f33b74722f6d20b78b72ff5652f8112808f070 Mon Sep 17 00:00:00 2001 From: Max Richter Date: Sat, 25 Jan 2025 18:51:10 +0100 Subject: [PATCH] fix: dont double cache documents --- lib/cache.ts | 12 ++++++++++-- lib/crud.ts | 14 -------------- lib/recipeSchema.ts | 3 ++- routes/api/cache.ts | 5 ++++- routes/recipes/[name].tsx | 2 ++ static/global.css | 22 ++++++++++++++++++++++ 6 files changed, 40 insertions(+), 18 deletions(-) diff --git a/lib/cache.ts b/lib/cache.ts index a0d8415..ca56506 100644 --- a/lib/cache.ts +++ b/lib/cache.ts @@ -6,9 +6,12 @@ interface SetCacheOptions { expires?: number; // Override expiration for individual cache entries } -const caches = new Map< +export const caches = new Map< string, - { info: () => { name: string; count: number; sizeInKB: number } } + { + info: () => { name: string; count: number; sizeInKB: number }; + clear: () => void; + } >(); export function createCache( @@ -31,6 +34,10 @@ export function createCache( return entry.value; // Return value if not expired }, + clear() { + cache.clear(); + }, + set(key: string, value: T | unknown, opts: SetCacheOptions = {}) { const now = Date.now(); const expiresIn = opts.expires ?? createOpts.expires; @@ -94,6 +101,7 @@ export function createCache( }; caches.set(cacheName, { + clear: api.clear.bind(api), info: api.info.bind(api), }); diff --git a/lib/crud.ts b/lib/crud.ts index 64d0497..ae9af90 100644 --- a/lib/crud.ts +++ b/lib/crud.ts @@ -12,7 +12,6 @@ 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, @@ -71,8 +70,6 @@ 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`; } @@ -80,10 +77,6 @@ export function createCrud( async function read(id: string) { const path = pathFromId(id); - if (cache.has(path)) { - return cache.get(path); - } - const content = await getDocument(path); if (!content) { return; @@ -95,13 +88,11 @@ export function createCrud( parsed = await addThumbnailToResource(parsed); } const doc = { ...parsed, content }; - cache.set(path, doc, { expires: 10 * 1000 }); return doc; } function create(id: string, content: string | ArrayBuffer | T) { const path = pathFromId(id); - cache.set("all", undefined); if ( typeof content === "string" || content instanceof ArrayBuffer ) { @@ -110,7 +101,6 @@ export function createCrud( if (render) { const rendered = render(content); - cache.set(path, content); return createDocument(path, rendered); } @@ -128,9 +118,6 @@ 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) => { @@ -143,7 +130,6 @@ export function createCrud( }), )).sort(sortFunction(sort)).filter((v) => !!v); - cache.set("all", parsed); return parsed; } diff --git a/lib/recipeSchema.ts b/lib/recipeSchema.ts index 352048c..54a0149 100644 --- a/lib/recipeSchema.ts +++ b/lib/recipeSchema.ts @@ -50,7 +50,8 @@ export function isValidRecipe( | null | undefined, ) { - return recipe?.ingredients?.length && recipe?.instructions?.length && + return recipe?.ingredients?.length && recipe.ingredients.length > 1 && + recipe?.instructions?.length && recipe.name?.length; } diff --git a/routes/api/cache.ts b/routes/api/cache.ts index 42f6cd0..4bca612 100644 --- a/routes/api/cache.ts +++ b/routes/api/cache.ts @@ -2,11 +2,14 @@ import { Handlers } from "$fresh/server.ts"; import { documentTable } from "@lib/db/schema.ts"; import { db } from "@lib/db/sqlite.ts"; import { json } from "@lib/helpers.ts"; +import { caches } from "@lib/cache.ts"; export const handler: Handlers = { async DELETE() { + for (const cache of caches.values()) { + cache.clear(); + } await db.delete(documentTable).run(); return json({ status: "ok" }); }, }; - diff --git a/routes/recipes/[name].tsx b/routes/recipes/[name].tsx index 728edab..961c1b1 100644 --- a/routes/recipes/[name].tsx +++ b/routes/recipes/[name].tsx @@ -67,6 +67,7 @@ export default function Greet( const portion = recipe.meta?.portion; const amount = useSignal(portion || 1); + console.log({ recipe }); const subline = [ recipe?.meta?.time && `Duration ${recipe.meta.time}`, @@ -114,6 +115,7 @@ export default function Greet( ) : (
h1 { + font-size: 2.25rem; + font-weight: 600; +} + +.markdown-body>h2 { + font-size: 1.75rem; + font-weight: 600; +} + +.markdown-body>h3 { + font-size: 1.5rem; + font-weight: 600; +} + +.markdown-body>h1>.anchor, +.markdown-body>h2>.anchor, +.markdown-body>h3>.anchor { + display: inline-flex; + margin-right: 5px; +}