Skip to content
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

[FEATURE] Optimize serialization in FusionCache by eliminating byte array overhead #371

Open
SukharevAndrey opened this issue Jan 30, 2025 · 1 comment

Comments

@SukharevAndrey
Copy link

SukharevAndrey commented Jan 30, 2025

Problem

Right now, the only supported way to serialize objects for storage in FusionCache is by converting them into byte arrays using IFusionCacheSerializer. This design decision was based on the method signatures of IDistributedCache, which only accept byte arrays.

With the introduction of HybridCache, a new interface, IBufferDistributedCache, was added. This interface extends IDistributedCache and introduces methods that utilize IBufferWriter<byte> and ReadOnlySequence<byte> for read and write operations, respectively.

Existing libraries, such as Microsoft.Extensions.Caching.StackExchangeRedis, have already adopted IBufferDistributedCache, benefiting from reduced memory allocations and improved performance. However, FusionCache currently does not leverage this interface internally and still relies on IDistributedCache.

The excessive use of byte arrays can contribute to increased GC pressure. By adopting IBufferDistributedCache, FusionCache could mitigate this issue, leading to more efficient serialization and deserialization processes.

Solution

FusionCache should support IBufferDistributedCache as an alternative to IDistributedCache. This would allow for more efficient serialization, reducing memory overhead and improving performance. The internal implementation should be updated to detect and use IBufferDistributedCache when available, while maintaining compatibility with existing implementations relying on IDistributedCache.

Implementing this feature would eliminate the need for the changes introduced in PR #349 and simplify the code.

Another important interface to consider is IHybridCacheSerializer<T>. When a user registers a custom serializer that implements this interface:

services.AddFusionCache()
        .AsHybridCache()
        .WithSerializer<CustomSerializerImplementation>();

FusionCache should recognize and utilize it within its serializer implementation via internal adapter.

Additional Context

Check out the official docs on IBufferDistributedCache for more details: Microsoft Docs.

@jodydonetti
Copy link
Collaborator

Hi @SukharevAndrey , can confirm: implementing IBufferDistributedCache is already on my backlog 🙂

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

No branches or pull requests

2 participants