MCPcopy
hub / github.com/apache/tvm / ArtifactCache

Class ArtifactCache

web/src/artifact_cache.ts:336–413  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

334 * Cache to store model related data, implemented with the Cache API.
335 */
336export class ArtifactCache implements ArtifactCacheTemplate {
337 private scope: string;
338 private cache?: Cache;
339
340 constructor(scope: string) {
341 this.scope = scope;
342 }
343
344 /**
345 * Convert the Response object to the expected storetype instead
346 */
347 async responseTostoretype(response: Response, storetype?: string): Promise<any> {
348 if (storetype === undefined) {
349 return response;
350 } else if (storetype.toLowerCase() === "json") {
351 return await response.json();
352 } else if (storetype.toLowerCase() === "arraybuffer") {
353 return await response.arrayBuffer();
354 } else {
355 console.error("Unknown storage type " + storetype + ", returning raw response");
356 return response;
357 }
358 }
359
360 /**
361 * fetch the corresponding url object in response or stored object format
362 * @param url url
363 * @param storetype the storage type for indexedDB
364 * @param signal an optional abort signal to abort fetching
365 * @returns response in json, arraybuffer or pure response format
366 */
367 async fetchWithCache(url: string, storetype?: string, signal?: AbortSignal): Promise<any> {
368 await this.addToCache(url, storetype, signal);
369 const result = await this.cache.match(new Request(url));
370 if (result === undefined) {
371 // Already called `addToCache()`, should expect the request in cache.
372 throw Error("Cannot fetch " + url);
373 }
374 return await this.responseTostoretype(result, storetype);
375 }
376
377 async addToCache(url: string, storetype?: string, signal?: AbortSignal) {
378 const request = new Request(url, signal ? { signal } : undefined);
379 if (this.cache === undefined) {
380 this.cache = await caches.open(this.scope);
381 }
382 const result = await this.cache.match(request);
383 if (result === undefined) {
384 await this.cache.add(request);
385 }
386 }
387
388 /**
389 * Determine if all keys exist in the cache
390 * @param keys the url key list of the strings
391 * @returns boolean value indicate if all keys are in cache
392 */
393 async hasAllKeys(keys: string[]) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…