diff --git a/web/src/artifact_cache.ts b/web/src/artifact_cache.ts index 394cda83bc437..2450d2ade6a7d 100644 --- a/web/src/artifact_cache.ts +++ b/web/src/artifact_cache.ts @@ -7,6 +7,11 @@ export interface ArtifactCacheTemplate { */ fetchWithCache(url: string); + /** + * add ey url to cache + */ + addToCache(url: string); + /** * check if cache has all keys in Cache */ @@ -16,4 +21,5 @@ export interface ArtifactCacheTemplate { * Delete url in cache if url exists */ deleteInCache(url: string); + } diff --git a/web/src/runtime.ts b/web/src/runtime.ts index a0ba25c8134d1..0dd1e2c341c04 100644 --- a/web/src/runtime.ts +++ b/web/src/runtime.ts @@ -1013,6 +1013,17 @@ export class ArtifactCache implements ArtifactCacheTemplate { return result; } + async addToCache(url: string) { + const request = new Request(url); + if (this.cache === undefined) { + this.cache = await caches.open(this.scope); + } + const result = await this.cache.match(request); + if (result === undefined){ + await this.cache.add(request); + } + } + async hasAllKeys(keys: string[]) { if (this.cache === undefined) { this.cache = await caches.open(this.scope); @@ -1548,8 +1559,7 @@ export class Instance implements Disposable { text: "Start to fetch params", }); } - - for (let i = 0; i < list.length; ++i) { + const downloadCache = async (i: number) => { reportCallback(i); const shard = list[i]; const dataUrl = new URL(shard.dataPath, ndarrayCacheUrl).href; @@ -1560,6 +1570,18 @@ export class Instance implements Disposable { this.env.logger("Error: Cannot fetch " + dataUrl + " err= " + err); throw err; } + } + await Promise.all(list.map((_, index) => downloadCache(index))); + for (let i = 0; i < list.length; ++i) { + const shard = list[i]; + const dataUrl = new URL(shard.dataPath, ndarrayCacheUrl).href; + let buffer; + try { + buffer = await (await artifactCache.fetchWithCache(dataUrl)).arrayBuffer(); + } catch (err) { + this.env.logger("Error: Cannot fetch " + dataUrl + " err= " + err); + throw err; + } const shardRecords = shard.records; for (let j = 0; j < shardRecords.length; ++j) { const rec = shardRecords[j];