fix: dont double cache documents

This commit is contained in:
max_richter 2025-01-25 18:51:10 +01:00
parent 0f146ea699
commit 23f33b7472
6 changed files with 40 additions and 18 deletions

View File

@ -6,9 +6,12 @@ interface SetCacheOptions {
expires?: number; // Override expiration for individual cache entries expires?: number; // Override expiration for individual cache entries
} }
const caches = new Map< export const caches = new Map<
string, string,
{ info: () => { name: string; count: number; sizeInKB: number } } {
info: () => { name: string; count: number; sizeInKB: number };
clear: () => void;
}
>(); >();
export function createCache<T>( export function createCache<T>(
@ -31,6 +34,10 @@ export function createCache<T>(
return entry.value; // Return value if not expired return entry.value; // Return value if not expired
}, },
clear() {
cache.clear();
},
set(key: string, value: T | unknown, opts: SetCacheOptions = {}) { set(key: string, value: T | unknown, opts: SetCacheOptions = {}) {
const now = Date.now(); const now = Date.now();
const expiresIn = opts.expires ?? createOpts.expires; const expiresIn = opts.expires ?? createOpts.expires;
@ -94,6 +101,7 @@ export function createCache<T>(
}; };
caches.set(cacheName, { caches.set(cacheName, {
clear: api.clear.bind(api),
info: api.info.bind(api), info: api.info.bind(api),
}); });

View File

@ -12,7 +12,6 @@ import { SILVERBULLET_SERVER } from "@lib/env.ts";
import { imageTable } from "@lib/db/schema.ts"; import { imageTable } from "@lib/db/schema.ts";
import { db } from "@lib/db/sqlite.ts"; import { db } from "@lib/db/sqlite.ts";
import { eq } from "drizzle-orm/sql"; import { eq } from "drizzle-orm/sql";
import { createCache } from "@lib/cache.ts";
export async function addThumbnailToResource<T extends GenericResource>( export async function addThumbnailToResource<T extends GenericResource>(
res: T, res: T,
@ -71,8 +70,6 @@ export function createCrud<T extends GenericResource>(
parse: (doc: string, id: string) => T; parse: (doc: string, id: string) => T;
}, },
) { ) {
const cache = createCache<T>(`crud/${prefix}`, { expires: 60 * 1000 });
function pathFromId(id: string) { function pathFromId(id: string) {
return `${prefix}${id.replaceAll(":", "")}.md`; return `${prefix}${id.replaceAll(":", "")}.md`;
} }
@ -80,10 +77,6 @@ export function createCrud<T extends GenericResource>(
async function read(id: string) { async function read(id: string) {
const path = pathFromId(id); const path = pathFromId(id);
if (cache.has(path)) {
return cache.get(path);
}
const content = await getDocument(path); const content = await getDocument(path);
if (!content) { if (!content) {
return; return;
@ -95,13 +88,11 @@ export function createCrud<T extends GenericResource>(
parsed = await addThumbnailToResource(parsed); parsed = await addThumbnailToResource(parsed);
} }
const doc = { ...parsed, content }; const doc = { ...parsed, content };
cache.set(path, doc, { expires: 10 * 1000 });
return doc; return doc;
} }
function create(id: string, content: string | ArrayBuffer | T) { function create(id: string, content: string | ArrayBuffer | T) {
const path = pathFromId(id); const path = pathFromId(id);
cache.set("all", undefined);
if ( if (
typeof content === "string" || content instanceof ArrayBuffer typeof content === "string" || content instanceof ArrayBuffer
) { ) {
@ -110,7 +101,6 @@ export function createCrud<T extends GenericResource>(
if (render) { if (render) {
const rendered = render(content); const rendered = render(content);
cache.set(path, content);
return createDocument(path, rendered); return createDocument(path, rendered);
} }
@ -128,9 +118,6 @@ export function createCrud<T extends GenericResource>(
} }
async function readAll({ sort = "rating" }: { sort?: SortType } = {}) { async function readAll({ sort = "rating" }: { sort?: SortType } = {}) {
if (cache.has("all")) {
return cache.get("all") as unknown as T[];
}
const allDocuments = await getDocuments(); const allDocuments = await getDocuments();
const parsed = (await Promise.all( const parsed = (await Promise.all(
allDocuments.filter((d) => { allDocuments.filter((d) => {
@ -143,7 +130,6 @@ export function createCrud<T extends GenericResource>(
}), }),
)).sort(sortFunction<T>(sort)).filter((v) => !!v); )).sort(sortFunction<T>(sort)).filter((v) => !!v);
cache.set("all", parsed);
return parsed; return parsed;
} }

View File

@ -50,7 +50,8 @@ export function isValidRecipe(
| null | null
| undefined, | undefined,
) { ) {
return recipe?.ingredients?.length && recipe?.instructions?.length && return recipe?.ingredients?.length && recipe.ingredients.length > 1 &&
recipe?.instructions?.length &&
recipe.name?.length; recipe.name?.length;
} }

View File

@ -2,11 +2,14 @@ import { Handlers } from "$fresh/server.ts";
import { documentTable } from "@lib/db/schema.ts"; import { documentTable } from "@lib/db/schema.ts";
import { db } from "@lib/db/sqlite.ts"; import { db } from "@lib/db/sqlite.ts";
import { json } from "@lib/helpers.ts"; import { json } from "@lib/helpers.ts";
import { caches } from "@lib/cache.ts";
export const handler: Handlers = { export const handler: Handlers = {
async DELETE() { async DELETE() {
for (const cache of caches.values()) {
cache.clear();
}
await db.delete(documentTable).run(); await db.delete(documentTable).run();
return json({ status: "ok" }); return json({ status: "ok" });
}, },
}; };

View File

@ -67,6 +67,7 @@ export default function Greet(
const portion = recipe.meta?.portion; const portion = recipe.meta?.portion;
const amount = useSignal(portion || 1); const amount = useSignal(portion || 1);
console.log({ recipe });
const subline = [ const subline = [
recipe?.meta?.time && `Duration ${recipe.meta.time}`, recipe?.meta?.time && `Duration ${recipe.meta.time}`,
@ -114,6 +115,7 @@ export default function Greet(
) )
: ( : (
<div <div
class="whitespace-break-spaces markdown-body"
dangerouslySetInnerHTML={{ dangerouslySetInnerHTML={{
__html: renderMarkdown(recipe?.markdown || ""), __html: renderMarkdown(recipe?.markdown || ""),
}} }}

View File

@ -128,3 +128,25 @@ main {
main.loading { main.loading {
opacity: 0; opacity: 0;
} }
.markdown-body>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;
}