cachear home por 10 min

tarda mucho hacer la query random
This commit is contained in:
Cat /dev/Nulo 2024-01-04 20:44:42 -03:00
parent 0dd725aafd
commit 387036a958

View file

@ -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;
};