Tuesday, 4 June 2024

What is HybridCache The Future of .NET Caching?

Leave a Comment

Explain HybridCache
The purpose of the HybridCache library is to fill in the gaps in the current.NET caching solutions, such as IDistributedCache and IMemoryCache. Several cutting-edge features are introduced by this potent new tool, including. 

  • Stampede Protection: Prevents multiple parallel fetches for the same data.
  • Configurable Serialization: Allows for flexible and customized serialization options.

HybridCache aims to be a drop-in replacement for IDistributedCache and IMemoryCache, offering a unified API for both in-process and out-of-process caching, simplifying the caching code significantly.

Why HybridCache?

Here’s a typical example of how caching is done with IDistributedCache.

public class SomeService(IDistributedCache cache)
{
    public async Task<SomeInformation> GetSomeInformationAsync(string name, int id, CancellationToken token = default)
    {
        var key = $"someinfo:{name}:{id}";
        var bytes = await cache.GetAsync(key, token);
        SomeInformation info;
        if (bytes is null)
        {
            info = await SomeExpensiveOperationAsync(name, id, token);
            bytes = SomeSerializer.Serialize(info);
            await cache.SetAsync(key, bytes, token);
        }
        else
        {
            info = SomeSerializer.Deserialize<SomeInformation>(bytes);
        }
        return info;
    }
    private async Task<SomeInformation> SomeExpensiveOperationAsync(string name, int id, CancellationToken token = default)
    { /* ... */ }
}

This method involves handling serialization and potential cache stampedes manually. With HybridCache, this complexity is greatly reduced.

Simplifying with HybridCache

First, add the HybridCache library

<PackageReference Include="Microsoft.Extensions.Caching.Hybrid" Version="9.0.0" />

Register the HybridCache service in your application.

services.AddHybridCache(); // Not shown: optional configuration API.

Now, you can simplify the caching logic as follows.

public class SomeService(HybridCache cache)
{
    public async Task<SomeInformation> GetSomeInformationAsync
        (string name, int id, CancellationToken token = default)
    {
        return await cache.GetOrCreateAsync(
            $"someinfo:{name}:{id}", // Unique key for this combination.
            async cancel => await SomeExpensiveOperationAsync(name, id, cancel),
            token: token
        );
    }
}
Advanced usage and custom implementations

HybridCache supports creating custom implementations and handles all caching concerns, including concurrent operations. For high-throughput scenarios, you can use the TState pattern to avoid overhead from captured variables:

public class SomeService(HybridCache cache)
{
    public async Task<SomeInformation> GetSomeInformationAsync(string name, int id, CancellationToken token = default)
    {
        return await cache.GetOrCreateAsync(
            $"someinfo:{name}:{id}", // unique key for this combination
            (name, id), // all of the state we need for the final call, if needed
            static async (state, token) =>
                await SomeExpensiveOperationAsync(state.name, state.id, token),
            token: token
        );
    }
}
Object Reuse and Performance

HybridCache maintains the behavior of deserializing objects on each retrieval to ensure thread safety. However, if your cached objects are immutable or not modified, you can optimize performance by reusing instances.

  • Mark the type as sealed using the sealed keyword.
  • Apply the [ImmutableObject(true)] attribute to indicate immutability.
Additional features
  • Key Removal: HybridCache supports key removal with RemoveKeyAsync.
  • Optimized IDistributedCache APIs: Avoid byte[] allocations with preview versions of Microsoft.Extensions.Caching.StackExchangeRedis and Microsoft.Extensions.Caching.SqlServer.
  • Flexible Serialization: Configure serialization with WithSerializer and WithSerializerFactory methods during service registration.
  • Cross-Platform Support: Supports .NET Framework 4.7.2 and .NET Standard 2.0.

 ASP.NET Core 9.0 Hosting Recommendation

One of the most important things when choosing a good ASP.NET Core 9.0 hosting is the feature and reliability. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core, their servers are optimized for PHP web applications. The performance and the uptime of the hosting service are excellent and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. 

At HostForLIFE.eu, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its datacenters are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core. And the engineers do regular maintenance and monitoring works to assure its Orchard hosting are security and always up.

 

0 comments:

Post a Comment