(
req: Request,
lru: LruList,
)
| 494 | } |
| 495 | |
| 496 | private async loadFromCache( |
| 497 | req: Request, |
| 498 | lru: LruList, |
| 499 | ): Promise<{res: Response; age: number} | null> { |
| 500 | // Look for a response in the cache. If one exists, return it. |
| 501 | const cache = await this.cache; |
| 502 | let res = await cache.match(req, this.config.cacheQueryOptions); |
| 503 | if (res !== undefined) { |
| 504 | // A response was found in the cache, but its age is not yet known. Look it up. |
| 505 | try { |
| 506 | const ageTable = await this.ageTable; |
| 507 | const age = this.adapter.time - (await ageTable.read<AgeRecord>(req.url)).age; |
| 508 | // If the response is young enough, use it. |
| 509 | if (age <= this.config.maxAge) { |
| 510 | // Successful match from the cache. Use the response, after marking it as having |
| 511 | // been accessed. |
| 512 | lru.accessed(req.url); |
| 513 | return {res, age}; |
| 514 | } |
| 515 | |
| 516 | // Otherwise, or if there was an error, assume the response is expired, and evict it. |
| 517 | } catch { |
| 518 | // Some error getting the age for the response. Assume it's expired. |
| 519 | } |
| 520 | |
| 521 | lru.remove(req.url); |
| 522 | await this.clearCacheForUrl(req.url); |
| 523 | |
| 524 | // TODO: avoid duplicate in event of network timeout, maybe. |
| 525 | await this.syncLru(); |
| 526 | } |
| 527 | return null; |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Operation for caching the response from the server. This has to happen all |
no test coverage detected