refactor: resource cache

This commit is contained in:
cha0s 2024-10-02 14:39:56 -05:00
parent 3bbaf83140
commit 0639c825a9

View File

@ -1,3 +1,9 @@
import {LRUCache} from 'lru-cache';
const cache = new LRUCache({
max: 128,
});
export async function computeMissing(current, manifest) {
const missing = [];
for (const path in manifest) {
@ -58,12 +64,15 @@ export async function get() {
export async function set(resources) {
const {set} = await import('idb-keyval');
cache.clear();
await set('$$silphius_resources', resources);
}
export async function readAsset(path) {
const {pathname} = new URL(path, 'http://example.org');
const resourcePath = pathname.slice('/resources/'.length);
const resources = await get();
return resources[resourcePath]?.asset;
if (!cache.has(path)) {
const {pathname} = new URL(path, 'http://example.org');
const resourcePath = pathname.slice('/resources/'.length);
cache.set(path, get().then((resources) => resources[resourcePath]?.asset));
}
return cache.get(path);
}