What is HybridCache
In .NET 9, Microsoft brought HybridCache into the framework.
HybridCache is a new caching model designed to encapsulate local and distributed caches, so that users don't have to worry about choosing a cache type, thereby optimizing performance and maintenance efficiency.
In fact, HybridCache is based on the interfaces and operations provided by IDistributedCache, but adds some other features, such as encapsulating two different cache libraries (local and distributed), supporting tag deletion (Tag-based Cache Eviction) and constraint options.
Need to pay attentionIt is true that HybridCache is still in the preview stage.
The difference between HybridCache and IDistributedCache
IDistributedCache:
- Only distributed caching is supported, such as Redis, SQL Server, and MemoryCache.
- Select depend on the target cache and management device.
- Tag deletion is not supported, it can only be operated based on key value.
HybridCache:
- Supports encapsulation of local and distributed caches, and prioritizes reading of local caches when reading. If the local does not exist, then reads distributed.
- Support tag deletion and manage cached content by specifying tags.
- Options are more streamlined, supporting automated operations and option constraints.
The benefits of HybridCache
- Performance optimization:The local cache speed is greater than that of distributed, and using HybridCache can reduce the latency when reading distributed cache libraries.
- Simplify the project:Users no longer need to verify which cache to choose by themselves, which increases engineering efficiency.
- Tag management:The cache tag records different types of data, which is convenient for classification management and cache deletion.
- Security:Support option constraints to make caching operations more stringent and prevent incorrect use and content loss.
Code Example
The following code shows how to use HybridCache:
1. Add a cache service
var builder = (args); // Register for HybridCache Service(); // Register the Redis cache service to provide distributed cache for HybridCache(options => { = ("RedisConnectionString"); }); ();
2. Implement interface operation
Read cache
[HttpGet("GetCache")] public async Task<string[]> Get() { return await _cache.GetOrCreateAsync( "a-1", async cancel => await (Summaries) ); }
Delete the cache
[HttpGet("DeleteCache")] public async Task<bool> Delete() { await _cache.RemoveAsync("a-1"); return true; }
Read cache through tags
[HttpGet("GetCacheByTag")] public async Task<string[]> GetCacheByTag() { var tags = new List<string> { "tag1", "tag2", "tag3" }; var entryOptions = new HybridCacheEntryOptions { Expiration = (1), LocalCacheExpiration = (1) }; return await _cache.GetOrCreateAsync( "a-1", async cancel => await (Summaries), entryOptions, tags ); }
Delete cache via tag
[HttpGet("DeleteCacheByTag")] public async Task<bool> DeleteCacheByTag() { var tags = new List<string> { "tag1" }; await _cache.RemoveByTagAsync(tags); return true; }
summary
HybridCache for .NET 9 provides a convenient and efficient caching solution that seamlessly combines local cache and distributed cache, simplifying cache logic for developers while providing more advanced features such as tag management and option constraints. As can be seen from the code examples, HybridCache's operation is intuitive and easy to implement, making it very suitable for modern application scenarios.
If you are using .NET 9, try applying HybridCache to your project and experience its efficiency and simplicity!
Article reprinted from:chester·chen
Original link:/chenyishi/p/18626831
Experience address:Yinmai - JNPF rapid development platform_low code development platform_zero code development platform_process designer_form engine_workflow engine_software architecture
This is all about this article about HybridCache in .NET 9. For more related .NET multi-level caching, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!