Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the Caching component in ASP.NET Core

Shulou Source: shulou.com Published: 2022-05-31 23:01:37 10月12日 Update

In this article Xiaobian introduces in detail "how to use Caching components in ASP.NET Core", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use Caching components in ASP.NET Core" can help you solve your doubts.

Components of Caching are provided in the .NET Core. Currently, the Caching component provides three storage methods:

Memory

Redis

SQLSever

1.Memeor Caching

Create a new ASP.NET Core Web application project and install Microsoft.Extensions.Caching.Memory.

Modify ConfigureServices method

Services.AddMemoryCache (); services.AddMvc (). SetCompatibilityVersion (CompatibilityVersion.Version_2_1)

In HomeController, use:

Private IMemoryCache memoryCache; public HomeController (IMemoryCache _ memoryCache) {memoryCache = _ memoryCache;} public IActionResult Index () {string cacheKey = "key"; string result; if (! memoryCache.TryGetValue (cacheKey, out result)) {result = $"LineZero {DateTime.Now}" MemoryCache.Set (cacheKey, result); / / set relative expiration time memoryCache.Set (cacheKey, result, new MemoryCacheEntryOptions () .SetSlidingExpiration (TimeSpan.FromSeconds (10) / / set absolute expiration time memoryCache.Set (cacheKey, result, new MemoryCacheEntryOptions () .SetAbsoluteExpiration (TimeSpan.FromSeconds (10); / / delete cache memoryCache.Remove (cacheKey) / / set cache priority (when the program is under pressure, it will be automatically reclaimed according to priority) memoryCache.Set (cacheKey,result,new MemoryCacheEntryOptions () .SetPriority (CacheItemPriority.NeverRemove)) / / cache callback memoryCache.Set (cacheKey, result, new MemoryCacheEntryOptions () .SetAbsoluteExpiration (TimeSpan.FromSeconds (60)) .RegisterPostEvictionCallback ((key, value, reason, substate) = > {nlog.Warn ($"key {key} value {value} change) upon expiration Because {reason} ") })); / / when Token expires, the cache callback var cts = new CancellationTokenSource () MemoryCache.Set (cacheKey, result, new MemoryCacheEntryOptions () .AddExpirationToken (new CancellationChangeToken (cts.Token)) .RegisterPostEvictionCallback ((key, value, reason, substate) = > {nlog.Warn ($"key {key} value {value} changes because {reason}") }));} ViewBag.Cache = result; return View ();} 2.Distributed Cache Tag Helper

There is a Distributed Cache Tag Helper in ASP.NET Core MVC that is dependent on the MemoryCache component.

You can add a distributed-cache tag directly to the attempt

@ {ViewData ["Title"] = "Home Page";}

Cache items expire in 10 seconds (expires-after absolute expiration time)

Relative ten seconds (relative expiration time of expires-sliding)

@ DateTime.Now@ViewBag.Cache read here, this article "how to use Caching components in ASP.NET Core" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about related articles, welcome to follow the industry information channel.

Tags: Component time cache article priority content program proper deposit item application idea new knowledge way method more label step knowledge knowledge point article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft NVidia Shulou Information Shulou Technology Xiaomi