Add data to cache with both inactivity and max lifetime timers
(url: string, data: Uint8Array)
| 742 | |
| 743 | /** Add data to cache with both inactivity and max lifetime timers */ |
| 744 | function setCacheEntry(url: string, data: Uint8Array): void { |
| 745 | // Clear any existing entry first |
| 746 | deleteCacheEntry(url); |
| 747 | |
| 748 | // Evict least-recently-used entries until under the byte cap. |
| 749 | for (const oldest of cache.keys()) { |
| 750 | if (totalBytes + data.length <= maxTotalBytes) break; |
| 751 | deleteCacheEntry(oldest); |
| 752 | } |
| 753 | |
| 754 | const entry: CacheEntry = { |
| 755 | data, |
| 756 | createdAt: Date.now(), |
| 757 | inactivityTimer: setTimeout(() => { |
| 758 | deleteCacheEntry(url); |
| 759 | }, CACHE_INACTIVITY_TIMEOUT_MS), |
| 760 | maxLifetimeTimer: setTimeout(() => { |
| 761 | deleteCacheEntry(url); |
| 762 | }, CACHE_MAX_LIFETIME_MS), |
| 763 | }; |
| 764 | |
| 765 | cache.set(url, entry); |
| 766 | totalBytes += data.length; |
| 767 | } |
| 768 | |
| 769 | /** Slice a cached or freshly-fetched full body to the requested range. */ |
| 770 | function sliceToChunk( |
no test coverage detected
searching dependent graphs…