Skip to content

[Blazor] Support HybridCache backend for persistent component state #62299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2025

Conversation

javiercn
Copy link
Member

@javiercn javiercn commented Jun 10, 2025

  • Adds support for HybridCache as a backend for persisting component state.
  • The implementation automatically switches to use HybridCache if one is registered on DI.
  • A custom HybridCache instance can be set in options for full control over the limits.

@@ -74,6 +74,7 @@
<MicrosoftNETCoreBrowserDebugHostTransportVersion>10.0.0-preview.6.25304.106</MicrosoftNETCoreBrowserDebugHostTransportVersion>
<MicrosoftExtensionsCachingAbstractionsVersion>10.0.0-preview.6.25304.106</MicrosoftExtensionsCachingAbstractionsVersion>
<MicrosoftExtensionsCachingMemoryVersion>10.0.0-preview.6.25304.106</MicrosoftExtensionsCachingMemoryVersion>
<MicrosoftExtensionsCachingHybridVersion>10.0.0-alpha.2.24462.2</MicrosoftExtensionsCachingHybridVersion>
Copy link
Member Author

Choose a reason for hiding this comment

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

@wtgodbe @mgravell Do you know why there aren't 10.0.0-preview6 versions on our feeds?

Copy link
Member Author

Choose a reason for hiding this comment

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

private static readonly Func<CancellationToken, ValueTask<PersistedCircuitState>> _failOnCreate =
static ct => throw new InvalidOperationException();

private static readonly string[] _tags = ["Microsoft.AspNetCore.Components.Server.PersistedCircuitState"];
Copy link
Member

Choose a reason for hiding this comment

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

the primary use of tags is for remove-by-tags-async, but I don't see that used; is this forward thinking to when that might be needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this is about making sure that we are "good citizens" of the shared cache and that people have a way of specifically cleaning these entries. Is this the pattern or should I do it in a different way?

{
await _lock.WaitAsync(cancellation);
var state = await _hybridCache.GetOrCreateAsync(
circuitId.Secret,
Copy link
Member

@mgravell mgravell Jun 11, 2025

Choose a reason for hiding this comment

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

I don't know a lot about "circuitid", but: is this unique enough vs other potential parallel HC uses? there may be some benefit in a key prefix, and if you do that, I strongly recommend using interpolated string literals, i.e. $"circuits/{circuitId.Secret}", because that should start benefitting from zero-alloc paths in .NET 10 (i.e. for cache hits, it doesn't actually allocate a string for the composed value, via voodoo)

Copy link
Member

Choose a reason for hiding this comment

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

edit: and then I see that this is only used in the "we need to remove it" case, in which case: you will need the string, but: just to say, there may still be value in other cases....

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, the circuit id is 32 bytes of entropy so no chance we hit that :)

@javiercn javiercn force-pushed the javiercn/persistent-component-state-graceful branch from f17b908 to 8582da8 Compare June 11, 2025 13:48
@javiercn javiercn force-pushed the javiercn/persistent-component-state-hybrid-cache branch from 5523b8b to 963076a Compare June 11, 2025 13:50
@javiercn javiercn marked this pull request as ready for review June 11, 2025 13:51
@javiercn javiercn requested review from a team and wtgodbe as code owners June 11, 2025 13:51
@javiercn javiercn closed this Jun 11, 2025
@javiercn javiercn reopened this Jun 11, 2025
@javiercn javiercn force-pushed the javiercn/persistent-component-state-graceful branch from e5a076c to b6632dd Compare June 11, 2025 19:22
@javiercn javiercn force-pushed the javiercn/persistent-component-state-hybrid-cache branch from 963076a to bddbfd4 Compare June 11, 2025 19:39
Base automatically changed from javiercn/persistent-component-state-graceful to main June 12, 2025 00:21
@javiercn javiercn force-pushed the javiercn/persistent-component-state-hybrid-cache branch from bddbfd4 to 46ebb2c Compare June 12, 2025 06:41
@javiercn javiercn merged commit 336519c into main Jun 12, 2025
27 checks passed
@javiercn javiercn deleted the javiercn/persistent-component-state-hybrid-cache branch June 12, 2025 12:32
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview6 milestone Jun 12, 2025
javiercn added a commit that referenced this pull request Jun 12, 2025
…62299)

* Adds support for HybridCache as a backend for persisting component state.
* The implementation automatically switches to use HybridCache if one is registered on DI.
* A custom HybridCache instance can be set in options for full control over the limits.
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.

4 participants