Introduction to ASP.NET Core Caching

  Caching provides the components in .NET Core. Caching component currently offers three storage:

    Memory

    Redis

    SQLSever

 1.Memeor Caching

  Create a new ASP.NET Core Web Application project, and then 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 the relative expiry time 
                memoryCache.Set (cacheKey, the Result, new new MemoryCacheEntryOptions ()
                    .SetSlidingExpiration (TimeSpan.FromSeconds ( 10 )));
                 // set an absolute expiration time 
                memoryCache.Set (cacheKey, the Result, new new MemoryCacheEntryOptions ()
                    .SetAbsoluteExpiration (TimeSpan.FromSeconds ( 10 )));
                 // delete cache 
                memoryCache.Remove (cacheKey);
                 // Set cache priority (pressure when the program will be based on the priority of automatic recovery) 
                memoryCache.Set (cacheKey, the Result, new new MemoryCacheEntryOptions ()
                    .SetPriority (CacheItemPriority.NeverRemove));
                // cache callback expired 
                memoryCache.Set (cacheKey, the Result, new new MemoryCacheEntryOptions ()
                    .SetAbsoluteExpiration(TimeSpan.FromSeconds(60))
                    .RegisterPostEvictionCallback((key, value, reason, substate)
                    =>
                    {
                        nlog.Warn ($ " {key} in {value} value change, because reason} { " );
                    }));
                // When Token expires, the cache callback 
                var CTS = new new CancellationTokenSource ();
                memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
                    .AddExpirationToken(new CancellationChangeToken(cts.Token))
                    .RegisterPostEvictionCallback((key, value, reason, substate)
                    =>
                    {
                        nlog.Warn ($ " {key} in {value} value change, because reason} { " );
                    }));
            }
            ViewBag.Cache = result;
            return View();
        }

  

  2.Distributed Cache Tag Helper

    There is a Distributed Cache Tag Helper in ASP.NET Core MVC, which is dependent on MemoryCache components.

    In an attempt to directly increase on distributed-cache tag

@{
    ViewData["Title"] = "Home Page";
}
< Distributed Cache- name = ", mycache" Expires After- = "TimeSpan.FromSeconds (10)" > 
    < P > cache entry expires 10 seconds (expires-after absolute expiration time) </ P > 
</ Distributed Cache- > 
< Distributed -cache name = "mycachenew" expires Sliding- = "TimeSpan.FromSeconds (10)" > 
    < P > relative ten seconds (expires-sliding relative expiry time) </ P >
    @DateTime.Now
</distributed-cache>
<div>@ViewBag.Cache</div>

 

 

 

  

Guess you like

Origin www.cnblogs.com/afei-24/p/11000367.html