HybridCache is a .NET library that combines the benefits of in-memory and distributed caching, simplifying cache management in .NET applications. Beyond standard caching functionalities, HybridCache introduces several innovative features that enhance application performance and scalability.

Installing the HybridCache Package

Begin by installing the Microsoft.Extensions.Caching.Hybrid NuGet package:

dotnet add package Microsoft.Extensions.Caching.Hybrid

This package provides the necessary components to implement HybridCache in your project.

Configuring Services

In your project, register the HybridCache service with the dependency injection container:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHybridCache();

Cache Stampede Protection

A cache stampede is when several clients request the same data at the same time after the cache has expired, causing an increase in the backend system load. HybridCache resolves this issue by ensuring that, when there are concurrent requests for the same data, only the first request performs the necessary action to recreate the data, and the others wait for the result. This maximizes resource utilization and ensures data consistency.

Cache Invalidation with Tags

HybridCache provides the use of tags to filter and manage the cache entries. By assigning one or more tags to an entry, you can invalidate sets of items that are associated with a single API call, which simplifies and makes cache management more efficient for invalidation operation.

Example of using tags in HybridCache:

public class SampleService
{
    private readonly HybridCache _cache;

    public SampleService(HybridCache cache)
    {
        _cache = cache;
    }

    public async Task<string> GetSomeInfoAsync(string name, int id, CancellationToken token = default)
    {
        var tags = new List<string> { "sample1", "sample2" };
        var entryOptions = new HybridCacheEntryOptions
        {
            Expiration = TimeSpan.FromMinutes(5),
            LocalCacheExpiration = TimeSpan.FromMinutes(5)
        };
        return await _cache.GetOrCreateAsync(
            $"{name}-{id}",
            async cancel => await GetDataFromSourceAsync(name, id, cancel),
            entryOptions,
            tags,
            cancellationToken: token
        );
    }

    private async Task<string> GetDataFromSourceAsync(string name, int id, CancellationToken token)
    {
        // Simulate an operation
        await Task.Delay(100, token);
        return $"Information for {name} with ID {id}";
    }
}

In this example, cache entries are associated within the tags “sample1” and “sample2”, allowing invalidation of all related entries using these tags.

Simplified API with GetOrCreateAsync

HybridCache simplifies the implementation of the cache-aside pattern through the GetOrCreateAsync method, reducing the code required to manage the cache to a single line. This method checks if an entry exists in the cache and, if not, executes a function to retrieve and store the data.

Example of using GetOrCreateAsync:

public class SampleService
{
    private readonly HybridCache _cache;

    public SampleService(HybridCache cache)
    {
        _cache = cache;
    }

    public async Task<SomeInformation> GetSomeInformationAsync(string name, int id, CancellationToken token = default)
    {
        return await _cache.GetOrCreateAsync(
            $"someinfo:{name}:{id}",
            async cancel => await SomeExpensiveOperationAsync(name, id, cancel),
            token: token
        );
    }

    private async Task<SomeInformation> SomeExpensiveOperationAsync(string name, int id, CancellationToken token)
    {
        // Simulate an operation
        await Task.Delay(100, token);
        return new SomeInformation { Name = name, Id = id };
    }
}

This approach reduces boilerplate code and simplifies cache management in applications.

Performance Optimization

HybridCache offers options to optimize performance, such as object reuse to reduce CPU overhead and memory allocations associated with deserialization. By setting types as sealed and applying the [ImmutableObject(true)] attribute, you can indicate that objects are thread-safe and can be reused, enhancing efficiency. ​

Compatibility

HybridCache, introduced with.NET 9, is made compatible with multiple.NET runtimes to support as high as.NET Framework 4.7.2 and.NET Standard 2.0 versions. This broad compatibility allows developers to integrate HybridCache in older and new applications to simplify the utilization of fine caching models on other projects.

Conclusion

By integrating HybridCache into ASP.NET Core applications, developers can leverage better cache management and richer functions that improve performance and scalability. Advanced features of cache stampede prevention, tag-driven invalidation, and accelerated API make HybridCache an enticing proposition for enabling improved data access optimization in existing and future applications.


<
Previous Post
Reverse Proxy: What It Is, What It Does, and a Practical Example with Microsoft’s YARP
>
Next Post
Understanding HATEOAS in RESTful APIs with C# and ASP.NET Core