The ASP. NET Core Cache Caching,.net core provides us with caching components.
Currently, the caching component provides three ways to store.
Memory
Redis
Sql server
Learn to use caching in ASP.
Memory Caching
1. Create a new ASP. NET Core project, select the Web application , and change the authentication to no authentication.
2. Adding references
Install-package Microsoft.extensions.caching.memory-pre
3. Use
Configureservices in the Startup.cs
Public void configureservices (iservicecollection services) { services. Addmemorycache (); // ADD Framework Services. Services. Addmvc (); }
And then in
Public classHomecontroller:controller {Private IMemoryCache _memorycache; PublicHomeController (imemorycache memorycache) {_memorycache=MemoryCache; } PublicIactionresult Index () {stringCacheKey ="Key"; stringresult; if(!_memorycache.trygetvalue (CacheKey, outresult)) {Result= $"Linezero{datetime.now}"; _memorycache.set (CacheKey, result); } Viewbag.cache=result; returnView (); } }
Here is a simple to use, directly set cache.
We can also add the expiration time, remove the cache, and also remove the method when it is removed.
The expiration time supports relative and absolute.
Here is a detailed variety of usage.
PublicIactionresult Index () {stringCacheKey ="Key"; stringresult; if(!_memorycache.trygetvalue (CacheKey, outresult)) {Result= $"Linezero{datetime.now}"; _memorycache.set (CacheKey, result); //Set relative expiration time 2 minutes_memorycache.set (CacheKey, result,Newmemorycacheentryoptions (). Setslidingexpiration (Timespan.fromminutes (2))); //set an absolute expiration time of 2 minutes_memorycache.set (CacheKey, result,Newmemorycacheentryoptions (). Setabsoluteexpiration (Timespan.fromminutes (2))); //Remove Cache_memorycache.remove (CacheKey); //Cache Priority (when program pressure is high, it is automatically recycled according to priority)_memorycache.set (CacheKey, result,Newmemorycacheentryoptions (). SetPriority (Cacheitempriority.neverremove)); //Cache callback 10 seconds expires callback_memorycache.set (CacheKey, result,Newmemorycacheentryoptions (). Setabsoluteexpiration (Timespan.fromseconds (Ten)) . Registerpostevictioncallback (key, value, Reason, substate)={Console.WriteLine ($"the key {key} value {value} changed because {reason}"); })); //Cache callback expires based on token varCTS =NewCancellationTokenSource (); _memorycache.set (CacheKey, result,Newmemorycacheentryoptions (). Addexpirationtoken (NewCancellationchangetoken (CTS. Token)). Registerpostevictioncallback (key, value, Reason, substate)={Console.WriteLine ($"the key {key} value {value} changed because {reason}"); })); Cts. Cancel (); } Viewbag.cache=result; returnView (); }
Distributed Cache Tag Helper
There is a distributed Cache in ASP. NET Core MVC that we can use.
We add distributed-cache tags directly on the page.
<Distributed-cachename= "Mycache"Expires-after= "Timespan.fromseconds (Ten)"> <P>Cache entry expires in 10 seconds-linezero</P>@DateTime. now</Distributed-cache><Distributed-cachename= "Mycachenew"expires-sliding= "Timespan.fromseconds (Ten)"> <P>The cache entry will not expire when someone accesses it, and 10 seconds of unattended access expires-linezero</P>@DateTime. now</Distributed-cache>
This allows you to cache the contents of the tag.
If you think this article is helpful to you, please click " recommend ", thank you.
ASP. NET Core Development-cache (Caching)