Caching is a powerful way to make your web applications faster and more efficient. In ASP.NET Core, HybridCache is a modern caching solution that combines the speed of in-memory caching with the consistency of distributed caching. This article will walk you through what HybridCache is, how to set it up, and how to use its key features to build a modern caching system for your applications.
What is HybridCache?
HybridCache is a caching library in ASP.NET Core that merges two types of caching:
- In-Memory Cache: This is super fast because it stores data directly in your application’s memory.
- Distributed Cache: This is slower but ensures consistency across multiple servers. Examples include Redis or SQL Server.
Here’s how it works:
- When you request data, HybridCache first checks the in-memory cache (primary cache). If the data is there, it returns it immediately.
- If the data isn’t in the in-memory cache (primary cache), it checks the distributed cache (secondary cache). If found, it returns the data and also stores it in the in-memory cache for faster access next time.
- If the data isn’t in either cache, HybridCache fetches it from the original source (like a database), stores it in both caches, and then returns it.
This combination gives you the best of both worlds: speed and consistency.
Using HybridCache in Practice
To use HybridCache in your ASP.NET Core app, follow these steps:
- Install the
Microsoft.Extensions.Caching.Hybrid
package using the .NET CLI or NuGet Package Manager. - Register HybridCache with
AddHybridCache
in your Program.cs file:
|
|
Note: If you don’t configure a distributed cache, HybridCache will only use in-memory caching.
Let’s say you have a service that fetches user information from a database. Instead of hitting the database every time, you can use HybridCache to store the results. Here’s an example:
|
|
In this example:
- The
GetOrCreateAsync
method checks the cache for the data. - If the data isn’t found, It fetches from the database and stores it in both caches, primary and secondary caches.
- The next time the data is requested, it comes from the cache instead of the database.
Key Features of HybridCache
Tags for Grouping Cache Entries
Tags allow you to group related cache entries together. This is useful when you want to remove multiple cache entries at once. For example, if you have a list of products in a category, you can tag them with the category name and remove all products in that category when the category changes.
|
|
Removing Cache Entries with RemoveAsync
If you want to remove a specific cache entry manually, you can use the RemoveAsync
method:
|
|
This removes the cache entry with the key user_123
from both the in-memory and distributed caches.
Setting Cache Entries with SetAsync
If you already have the data and want to store it in the cache, you can use the SetAsync
method:
|
|
This stores the value User info for 123
in the cache with the key user_123
.
Configuring HybridCache Options
You can configure global caching options when registering HybridCache. For example, you can set default expiration times for cache entries:
|
|
In this example:
- With Expiration option, The cache entry will expire from both the distributed cache and the in-memory cache after 5 minutes.
- With LocalCacheExpiration option, The cache entry will expire from the in-memory cache after 2 minutes.
Stampede Protection
HybridCache also provides stampede protection. This prevents multiple requests from overwhelming your database when a popular piece of data expires in the cache. Only one request fetches the data, while the others wait for the result.
|
|
When you run this, only one request will fetch the data from the database, while the other nine requests will wait for the result.
You can find the sample code in this repository:
🔗 https://github.com/meysamhadeli/blog-samples/tree/main/src/hybrid-cache-sample
Conclusion
HybridCache is a powerful tool for improving the performance of your ASP.NET Core applications. It combines the speed of in-memory caching with the consistency of distributed caching and offers advanced features to manage your cache effectively. By following the examples in this article, you can start using HybridCache to build a modern and efficient caching system for your apps.
If you found this guide helpful, feel free to share it with others. Happy coding! 🚀
Reference
https://learn.microsoft.com/en-us/aspnet/core/performance/caching/hybrid?view=aspnetcore-9.0