In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-10-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.