From 387036a958a4814a9d086a567da67190b53a1f71 Mon Sep 17 00:00:00 2001 From: Nulo Date: Thu, 4 Jan 2024 20:44:42 -0300 Subject: [PATCH] cachear home por 10 min tarda mucho hacer la query random --- sitio/src/routes/+page.server.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/sitio/src/routes/+page.server.ts b/sitio/src/routes/+page.server.ts index 2f44867..0fd646d 100644 --- a/sitio/src/routes/+page.server.ts +++ b/sitio/src/routes/+page.server.ts @@ -1,9 +1,14 @@ -import type { PageServerLoad } from "./$types"; +import type { PageData, PageServerLoad } from "./$types"; import { db, schema } from "$lib/server/db"; const { precios } = schema; import { sql } from "drizzle-orm"; +let cache: null | { key: Date; data: PageData } = null; + export const load: PageServerLoad = async ({ params }) => { + if (cache && +new Date() < +cache.key + 1000 * 60 * 10) { + return cache.data; + } const q = db .select({ ean: precios.ean, @@ -16,5 +21,7 @@ export const load: PageServerLoad = async ({ params }) => { .orderBy(sql`random()`) .limit(150); const res = await q; - return { precios: res }; + const data = { precios: res }; + cache = { key: new Date(), data }; + return data; };