fix: dont double cache documents
This commit is contained in:
parent
0f146ea699
commit
23f33b7472
12
lib/cache.ts
12
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<T>(
|
||||
@ -31,6 +34,10 @@ export function createCache<T>(
|
||||
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<T>(
|
||||
};
|
||||
|
||||
caches.set(cacheName, {
|
||||
clear: api.clear.bind(api),
|
||||
info: api.info.bind(api),
|
||||
});
|
||||
|
||||
|
14
lib/crud.ts
14
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<T extends GenericResource>(
|
||||
res: T,
|
||||
@ -71,8 +70,6 @@ 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`;
|
||||
}
|
||||
@ -80,10 +77,6 @@ export function createCrud<T extends GenericResource>(
|
||||
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<T extends GenericResource>(
|
||||
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<T extends GenericResource>(
|
||||
|
||||
if (render) {
|
||||
const rendered = render(content);
|
||||
cache.set(path, content);
|
||||
return createDocument(path, rendered);
|
||||
}
|
||||
|
||||
@ -128,9 +118,6 @@ 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) => {
|
||||
@ -143,7 +130,6 @@ export function createCrud<T extends GenericResource>(
|
||||
}),
|
||||
)).sort(sortFunction<T>(sort)).filter((v) => !!v);
|
||||
|
||||
cache.set("all", parsed);
|
||||
return parsed;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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" });
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -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(
|
||||
)
|
||||
: (
|
||||
<div
|
||||
class="whitespace-break-spaces markdown-body"
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: renderMarkdown(recipe?.markdown || ""),
|
||||
}}
|
||||
|
@ -128,3 +128,25 @@ main {
|
||||
main.loading {
|
||||
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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user