ASP. NET Core Development-cache (Caching)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.