-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Description
Description
At the end of the HybridCache doc section Optimize performance -> Reuse objects, it states that you can inform the cache that it's safe to reuse instances of a type by make "at least one" the following 2 changes.
- Marking the type as sealed
- Applying the [ImmutableObject(true)] attribute to the type
However, for a cache item that is a class or other reference type, it actually requires both conditions to be true, not simply one or the other. This is clear by inspecting the source code where cache item type immutability is determined.
I confirmed this behavior in some test code. Calling the method HybridCache.GetOrCreateAsync() to fetch a non-serializable item causes a deserialization exception to be thrown when the cache item's class type is sealed, but is not decorated with the [ImmutableObject(true)] attribute. The same exception happens if the class is decorated with the ImmutableObject(true) attribute, but is not sealed.
It is only when the class has both of those conditions that there is no exception because the cache properly recognizes that the type is immutable and doesn't unexpectedly try to serialize/deserialize it.
Page URL
https://learn.microsoft.com/en-us/aspnet/core/performance/caching/hybrid?view=aspnetcore-10.0
Content source URL
https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/performance/caching/hybrid.md
Document ID
fdd3c6f2-c3f4-6fc7-0928-0b18ba9297cf
Platform Id
77e7a2f3-9ddb-06e2-9b46-7ddeb411f8c4
Article author
Metadata
- ID: fdd3c6f2-c3f4-6fc7-0928-0b18ba9297cf
- PlatformId: 77e7a2f3-9ddb-06e2-9b46-7ddeb411f8c4
- Service: aspnet-core
- Sub-service: performance