Repository navigation
[Caching] Include response cache keys in size accounting - #69231
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Outstanding review comments request additional documentation and coverage for edge cases, aggregate capacity, replacement, and admission verification.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Updates Response Caching size accounting to include retained UTF-16 cache-key content.
Changes:
- Adds checked key-size estimates to response and vary-rule entries.
- Clarifies
SizeLimitdocumentation. - Extends bounded-cache and middleware test coverage.
File summaries
| File | Description |
|---|---|
src/Middleware/ResponseCaching/test/TestUtils.cs |
Forwards SizeLimit to hosted middleware tests. |
src/Middleware/ResponseCaching/test/ResponseCachingTests.cs |
Adds bounded middleware scenarios. |
src/Middleware/ResponseCaching/test/MemoryResponseCacheTests.cs |
Tests key-size admission and UTF-16 accounting. |
src/Middleware/ResponseCaching/src/ResponseCachingOptions.cs |
Documents key-inclusive size limits. |
src/Middleware/ResponseCaching/src/MemoryResponseCache.cs |
Includes key size in cache-entry estimates. |
Review details
Suppressed comments (2)
src/Middleware/ResponseCaching/src/ResponseCachingOptions.cs:13
- Please state explicitly that
SizeLimitis an estimate of retained variable content, not a hard managed-heap/process-memory cap. “Estimated size” is ambiguous beside “size limit in bytes”; the public contract should also exclude object headers, cache/dictionary structures, allocator overhead, and other runtime costs.
/// The size limit for the response cache middleware in bytes, including the estimated size of cached response data,
/// vary-by rules, and cache keys. The default is set to 100 MB.
src/Middleware/ResponseCaching/test/MemoryResponseCacheTests.cs:15
- These store theories cover only nonempty keys with an empty vary-rule object and a response with empty headers/body. They do not verify that key bytes are added to the existing nonempty rule/response estimates, the fallback path, or the documented empty/null-key behavior, so regressions in those cases could pass. Add exact-limit and one-byte-under cases for those inputs.
[Theory]
[InlineData("a", 6)]
[InlineData("\u00E9", 6)]
[InlineData("\U0001F600", 8)]
public void Set_CachedResponse_AccountsForKeySize(string key, long estimatedSize)
- Files reviewed: 5/5 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Member
Author
|
/azp run |
|
Azure Pipelines: Successfully started running 2 pipeline(s). 3 pipeline(s) were filtered out due to trigger conditions. |
Member
Author
|
/azp run |
|
Azure Pipelines: Successfully started running 2 pipeline(s). 3 pipeline(s) were filtered out due to trigger conditions. |
DeagleGross
approved these changes
Sep 28, 2026
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #69214
Overview
Response Caching previously declared only each cached value's estimated size to the bounded
MemoryCache, even though the cache also retains the supplied string key. This change includes retained key content in both response and vary-rule entry sizes and clarifies theResponseCachingOptions.SizeLimitcontract. The accounting model remains an estimate of retained variable content rather than a hard managed-heap ceiling.Design
Cache keys are charged at the storage boundary, where the actual key and value are both known. The estimate uses the retained in-memory representation:
(long)key.Length * sizeof(char), so units are UTF-16 bytes and surrogate pairs count as two code units. This matches the existing estimators' treatment of strings and avoids changing key identity, reconstructing keys from request components, or introducing a separate key limit.SizeLimitdocumentation now states that the budget includes estimated cached response data, vary-by rules, and cache keys, and explicitly excludes object headers, cache structures, allocator overhead, and other runtime-dependent costs. Defaults, public signatures, response body limits, expiration, and independent rule/response admission remain unchanged; at the same limit, entries with larger keys can now reach capacity sooner.Implementation
The multiplication widens to
long, and the combined estimate uses checked arithmetic. The original key is still passed unchanged toMemoryCache, preserving existing null-key rejection and cache identity behavior.Tests
Focused store tests use a real bounded
MemoryCacheand cover:Middleware coverage uses the existing harness in two layers. A TestServer theory proves rejected entries still deliver the originating response and miss later through the synchronous, asynchronous, and send-file delegates. A focused middleware theory inspects the real bounded cache after both writes, proving that an oversized base key rejects only the vary rules while the varied response is stored, and that an oversized varied key rejects only the response while the vary rules remain stored.
Validation
With key charging temporarily removed, 11 of the 13 focused store/middleware cases fail for the expected admission reason; only the empty- and null-key compatibility cases remain green. With the implementation restored, all focused cases pass. The complete
Microsoft.AspNetCore.ResponseCaching.Testssuite passes: 293 passed, 0 failed;git diff --checkalso passes.The latest CI run passed lint, code check, all build legs, all platform test legs, and the quarantined suite. Its aggregate result is red only because the unrelated
Microsoft.AspNetCore.Server.Kestrel.Core.Tests.Http3StreamTests.RequestTrailers_ContainsNewlinesHelix work item crashed withBadExiton Ubuntu; Build Analysis reports that test's existing 5.97% failure rate. The earlier local repository wrapper limitation likewise came from missing unrelatedsrc/submodules/googletest; the isolated Response Caching project builds and its full suite passes.