Hybrid Cache add DisableLocalCacheSerialization - Partially fixes #6063 - #7641
Hybrid Cache add DisableLocalCacheSerialization - Partially fixes #6063#7641snakex64 wants to merge 3 commits into
Conversation
Add DisableLocalCacheSerialization to hybrid cache. Partially fixes dotnet#6063 Enables the hybrid cache to skip serialization when fetching from L1 (in memory) cache. This speeds up the cache when possible, and allows getting the same object instance. ```
There was a problem hiding this comment.
Pull request overview
Adds a new opt-in HybridCacheOptions.DisableLocalCacheSerialization switch to allow the HybridCache L1 (in-memory) layer to return cached values by reference (avoiding the defensive-copy deserialize path), which can improve performance and enables caching scenarios where deserialization into the declared type is not possible (e.g., interfaces).
Changes:
- Introduces
HybridCacheOptions.DisableLocalCacheSerializationas a public option (API baseline updated). - Wires the option into
DefaultHybridCacheby changing the cache-item type selection to store mutable values as “immutable” (by-reference) cache items when enabled. - Adds tests validating reference equality behavior for mutable values when L1 hits occur.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| test/Libraries/Microsoft.Extensions.Caching.Hybrid.Tests/L2Tests.cs | Adds coverage for by-reference L1 behavior when local serialization is disabled. |
| test/Libraries/Microsoft.Extensions.Caching.Hybrid.Tests/BufferReleaseTests.cs | Updates CacheItem<T>.Create call sites for the new signature. |
| src/Libraries/Microsoft.Extensions.Caching.Hybrid/Microsoft.Extensions.Caching.Hybrid.json | Adds the new public option to the API baseline. |
| src/Libraries/Microsoft.Extensions.Caching.Hybrid/Internal/DefaultHybridCache.StampedeStateT.cs | Passes the new option into cache-item creation. |
| src/Libraries/Microsoft.Extensions.Caching.Hybrid/Internal/DefaultHybridCache.CacheItem.cs | Extends CacheItem<T>.Create to allow forcing immutable/by-reference cache items. |
| src/Libraries/Microsoft.Extensions.Caching.Hybrid/HybridCacheOptions.cs | Documents the new option on HybridCacheOptions. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
Hi, sorry for not responding sooner. Since this PR requires first agreeing on the API shape in #6063 and then a formal API approval, I am going to turn it into a draft for now. |
No problem! I appreciate the time either way |
This add a
DisableLocalCacheSerializationoption to hybrid cache. Partially fixes #6063It enables the hybrid cache to skip serialization when storing or fetching from L1 (in memory) cache. This speeds up the cache when possible, and allows getting the same object instance.
It "partially" fixes the issue because I realized after I started that I couldn't add a flag in here:
HybridCacheEntryFlags.cs since it's in another repo.
I wanted to get the conversation going again before I spend more time on it, but the better choice in my opinion would be to add a flag for each entry instead of globally like I did in this PR. But thisrequires doing a PR in the runtime repo too.
If this implementation seems good conceptually I will just need guidance into how to create and synchronize the 2 PR required to change both the "extensions" and the "runtime" repo :)
Microsoft Reviewers: Open in CodeFlow