MCPcopy
hub / github.com/angular/angular / cacheResponse

Method cacheResponse

packages/service-worker/worker/src/data.ts:537–574  ·  view source on GitHub ↗

* Operation for caching the response from the server. This has to happen all * at once, so that the cache and LRU tracking remain in sync. If the network request * completes before the timeout, this logic will be run inline with the response flow. * If the request times out on the server, a

(
    req: Request,
    res: Response,
    lru: LruList,
    okToCacheOpaque = false,
  )

Source from the content-addressed store, hash-verified

535 * request will still be running in the background, to be cached when it completes.
536 */
537 private async cacheResponse(
538 req: Request,
539 res: Response,
540 lru: LruList,
541 okToCacheOpaque = false,
542 ): Promise<void> {
543 // Only cache successful responses.
544 if (!(res.ok || (okToCacheOpaque && res.type === 'opaque'))) {
545 return;
546 }
547
548 // If caching this response would make the cache exceed its maximum size, evict something
549 // first.
550 if (lru.size >= this.config.maxSize) {
551 // The cache is too big, evict something.
552 const evictedUrl = lru.pop();
553 if (evictedUrl !== null) {
554 await this.clearCacheForUrl(evictedUrl);
555 }
556 }
557
558 // TODO: evaluate for possible race conditions during flaky network periods.
559
560 // Mark this resource as having been accessed recently. This ensures it won't be evicted
561 // until enough other resources are requested that it falls off the end of the LRU chain.
562 lru.accessed(req.url);
563
564 // Store the response in the cache (cloning because the browser will consume
565 // the body during the caching operation).
566 await (await this.cache).put(req, res.clone());
567
568 // Store the age of the cache.
569 const ageTable = await this.ageTable;
570 await ageTable.write(req.url, {age: this.adapter.time});
571
572 // Sync the LRU chain to non-volatile storage.
573 await this.syncLru();
574 }
575
576 /**
577 * Delete all of the saved state which this group uses to track resources.

Callers 1

safeCacheResponseMethod · 0.95

Calls 7

clearCacheForUrlMethod · 0.95
syncLruMethod · 0.95
popMethod · 0.80
accessedMethod · 0.80
putMethod · 0.65
cloneMethod · 0.65
writeMethod · 0.65

Tested by

no test coverage detected