fix prasi cache

This commit is contained in:
Rizky 2024-06-28 00:40:04 +07:00
parent 332061d5d0
commit 899c3802de
1 changed files with 20 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import mime from "mime";
const cache = { const cache = {
route: null as any, route: null as any,
comps: {} as Record<string, any>,
}; };
export const _ = { export const _ = {
url: "/_prasi/**", url: "/_prasi/**",
@ -89,15 +90,34 @@ export const _ = {
}, },
comp: async () => { comp: async () => {
const comps = {} as Record<string, any>; const comps = {} as Record<string, any>;
const pending = new Set<string>();
if (req.params.ids) { if (req.params.ids) {
for (const id of req.params.ids) { for (const id of req.params.ids) {
const comp = g.deploy.comps[id]; const comp = g.deploy.comps[id];
if (comp) { if (comp) {
comps[id] = comp; comps[id] = comp;
} else if (cache.comps[id]) {
comps[id] = cache.comps[id];
} else {
pending.add(id);
} }
} }
} }
if (pending.size > 0) {
try {
const res = await fetch(
`https://prasi.avolut.com/prod/452e91b8-c474-4ed2-9c43-447ac8778aa8/_prasi/comp`,
{ method: "POST", body: JSON.stringify({ ids: [...pending] }) }
);
for (const [k, v] of Object.entries((await res.json()) as any)) {
cache.comps[k] = v;
comps[k] = v;
}
} catch (e) {}
}
return await responseCompressed(req, JSON.stringify(comps)); return await responseCompressed(req, JSON.stringify(comps));
}, },
"load.json": async () => { "load.json": async () => {