Skip to content

Hybrid Cache add DisableLocalCacheSerialization - Partially fixes #6063#7641

Open
snakex64 wants to merge 3 commits into
dotnet:mainfrom
snakex64:hybrid-cache-serialization-6063
Open

Hybrid Cache add DisableLocalCacheSerialization - Partially fixes #6063#7641
snakex64 wants to merge 3 commits into
dotnet:mainfrom
snakex64:hybrid-cache-serialization-6063

Conversation

@snakex64

@snakex64 snakex64 commented Jul 17, 2026

Copy link
Copy Markdown

This add a DisableLocalCacheSerialization option to hybrid cache. Partially fixes #6063

It 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

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.
```
Copilot AI review requested due to automatic review settings July 17, 2026 20:29
@snakex64
snakex64 requested a review from a team as a code owner July 17, 2026 20:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.DisableLocalCacheSerialization as a public option (API baseline updated).
  • Wires the option into DefaultHybridCache by 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.

snakex64 and others added 2 commits July 17, 2026 18:47
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HybridCache serialization issues when in-memory

2 participants