forked from silverbulletmd/silverbullet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Manifest caching and lazy loading of plug workers * Fixes silverbulletmd#546 Plug unloading after time out
- Loading branch information
Showing
16 changed files
with
225 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { KvPrimitives } from "./lib/kv_primitives.ts"; | ||
import { Plug } from "./plug.ts"; | ||
import { Manifest } from "./types.ts"; | ||
|
||
export interface ManifestCache<T> { | ||
getManifest(plug: Plug<T>, hash: number): Promise<Manifest<T>>; | ||
} | ||
|
||
export class KVPrimitivesManifestCache<T> implements ManifestCache<T> { | ||
constructor(private kv: KvPrimitives, private manifestPrefix: string) { | ||
} | ||
|
||
async getManifest(plug: Plug<T>, hash: number): Promise<Manifest<T>> { | ||
const [cached] = await this.kv.batchGet([[ | ||
this.manifestPrefix, | ||
plug.name, | ||
]]); | ||
if (cached && cached.hash === hash) { | ||
// 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, plug.name], | ||
value: { manifest, hash }, | ||
}]); | ||
return manifest; | ||
} | ||
} | ||
|
||
export class InMemoryManifestCache<T> implements ManifestCache<T> { | ||
private cache = new Map<string, { | ||
manifest: Manifest<T>; | ||
hash: number; | ||
}>(); | ||
|
||
async getManifest(plug: Plug<T>, hash: number): Promise<Manifest<T>> { | ||
const cached = this.cache.get(plug.workerUrl.href); | ||
if (cached && cached.hash === hash) { | ||
// console.log("Using memory cached manifest for", plug.name); | ||
return cached.manifest; | ||
} | ||
await plug.sandbox.init(); | ||
const manifest = plug.sandbox.manifest!; | ||
this.cache.set(plug.name!, { manifest, hash }); | ||
return manifest; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.