Files
plainleaf/lib/plugos/manifest_cache.ts
T
MrMugameandGitHub ad39158cd1 Separate plug name and file name (#1541)
* Separate plug name from file path

Plug name and plug path were very intertwined in the code. This could
cause problems, if the file path and plug name didn't align.
The plug manager also modified the plug path for some plugs, which could
be even more confusing.

* Code improvements for the plug manager

* Be more rigorous about failure

* Also allow `silverbullet-name` case again
2025-09-15 08:54:55 +02:00

68 lines
2.0 KiB
TypeScript

import type { KvPrimitives } from "../data/kv_primitives.ts";
import type { Plug } from "./plug.ts";
import type { Manifest } from "./types.ts";
export interface ManifestCache<T> {
getManifest(
plug: Plug<T>,
cacheKey: string,
cacheHash: number,
): Promise<Manifest<T>>;
}
export class KVPrimitivesManifestCache<T> implements ManifestCache<T> {
constructor(private kv: KvPrimitives, private manifestPrefix: string) {
}
async getManifest(
plug: Plug<T>,
cacheKey: string,
cacheHash: number,
): Promise<Manifest<T>> {
const [cached] = await this.kv.batchGet([[
this.manifestPrefix,
cacheKey,
]]);
if (cached && cached.hash === cacheHash) {
// console.log("Using KV cached manifest for", plug.name);
return cached.manifest;
}
await plug.sandbox.init();
const manifest = plug.sandbox.manifest!;
await this.kv.batchSet([{
key: [this.manifestPrefix, cacheKey],
// Deliverately removing the assets from the manifest to preserve space, will be re-added upon load of actual worker
value: { manifest: { ...manifest, assets: undefined }, hash: cacheHash },
}]);
return manifest;
}
}
export class InMemoryManifestCache<T> implements ManifestCache<T> {
private cache = new Map<string, {
manifest: Manifest<T>;
hash: number;
}>();
async getManifest(
plug: Plug<T>,
cacheKey: string,
cacheHash: number,
): Promise<Manifest<T>> {
const cached = this.cache.get(cacheKey);
if (cached && cached.hash === cacheHash) {
// console.log("Using memory cached manifest for", plug.name);
return cached.manifest;
}
await plug.sandbox.init();
const manifest = plug.sandbox.manifest!;
// Deliverately removing the assets from the manifest to preserve space, will be re-added upon load of actual worker
this.cache.set(cacheKey, {
manifest: { ...manifest, assets: undefined },
hash: cacheHash,
});
return manifest;
}
}