Count key length in remaining byte-budgeted LRUs (#96231)
Applies the key accounting from #96229 to the other in-memory LRUs with
byte budgets:
- the incremental memory cache
- the default `use cache` handler
- the dev `staticPathsCache` and `serverComponentsHmrCache`
- the dev source-map URL cache.
Keys were free against each budget in the same way; unlike the
filesystem route cache there are no production leak reports for these,
so this is preventative and kept separate. Count-based caches and the
disk-byte LRU are unchanged.
## Test Plan
Each size callback change adds `cacheKey.length` to whatever it returned
before. The memory cache callback is also restructured from early
returns into if/else chains; each `CachedRouteKind` branch returns the
same value as before, plus the key length.
To see the change do something, run this against the built memory cache:
```js
const { getMemoryCache } = require('next/dist/server/lib/incremental-cache/memory-cache.external')
const cache = getMemoryCache(1024 * 1024) // 1 MB budget
for (let i = 0; i < 20000; i++) {
cache.set('/api/data?' + 'p'.repeat(1000) + i, {
value: { kind: 'FETCH', data: { body: 'ok' } },
lastModified: 1,
})
}
console.log(cache.size, cache.currentSize)
```
Before this change, all 20,000 entries are admitted: the cache counts
260 KB against its 1 MB budget while the keys alone hold ~20 MB, and it
keeps growing as long as new keys arrive. After, it holds 1,020 entries
and the counted size stops at the budget.
For regressions: `test/production/app-dir/revalidate` and
`test/e2e/app-dir/use-cache-default-handler-expire-zero` pass in prod
mode (these exercise the fetch cache and the default `use cache`
handler), plus the `packages/next/src/server/lib` jest suites and `pnpm
--filter=next types`.
Co-authored-by:
Marcos Hernanz <96699542+marcoshernanz@users.noreply.github.com>
Showing
Please
register
or
sign in
to comment