FusionCache
FusionCache copied to clipboard
[FEATURE] Optimize serialization in FusionCache by eliminating byte array overhead
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.