ASP.NET?Core中的Caching組件簡(jiǎn)介
在.NET Core中提供了Caching的組件。目前Caching組件提供了三種存儲(chǔ)方式:
- Memory
- Redis
- SQLSever
1.Memeor Caching
新建一個(gè)ASP.NET Core Web應(yīng)用程序項(xiàng)目,然后安裝 Microsoft.Extensions.Caching.Memory。
修改ConfigureServices方法
services.AddMemoryCache(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
在HomeController使用:
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);
//設(shè)置相對(duì)過期時(shí)間
memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromSeconds(10)));
//設(shè)置絕對(duì)過期時(shí)間
memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromSeconds(10)));
//刪除緩存
memoryCache.Remove(cacheKey);
//設(shè)置緩存優(yōu)先級(jí)(程序壓力大時(shí),會(huì)根據(jù)優(yōu)先級(jí)自動(dòng)回收)
memoryCache.Set(cacheKey,result,new MemoryCacheEntryOptions()
.SetPriority(CacheItemPriority.NeverRemove));
//過期時(shí)緩存回調(diào)
memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromSeconds(60))
.RegisterPostEvictionCallback((key, value, reason, substate)
=>
{
nlog.Warn($"鍵{key}值{value}改變,因?yàn)閧reason}");
}));
//Token過期時(shí),緩存回調(diào)
var cts = new CancellationTokenSource();
memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
.AddExpirationToken(new CancellationChangeToken(cts.Token))
.RegisterPostEvictionCallback((key, value, reason, substate)
=>
{
nlog.Warn($"鍵{key}值{value}改變,因?yàn)閧reason}");
}));
}
ViewBag.Cache = result;
return View();
}2.Distributed Cache Tag Helper
在ASP.NET Core MVC 中有一個(gè) Distributed Cache Tag Helper,它是依賴于MemoryCache組件的。
可以直接在試圖上增加 distributed-cache 標(biāo)簽
@{
ViewData["Title"] = "Home Page";
}
<distributed-cache name="mycache" expires-after="TimeSpan.FromSeconds(10)">
<p>緩存項(xiàng)10秒過期(expires-after絕對(duì)過期時(shí)間)</p>
</distributed-cache>
<distributed-cache name="mycachenew" expires-sliding="TimeSpan.FromSeconds(10)">
<p>相對(duì)十秒(expires-sliding相對(duì)過期時(shí)間)</p>
@DateTime.Now
</distributed-cache>
<div>@ViewBag.Cache</div>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET上傳圖片并生成可帶版權(quán)信息的縮略圖
ASP.NET上傳圖片并生成可帶版權(quán)信息的縮略圖...2006-09-09
Visual Studio 2015 配置 Opencv3.2的圖文詳解
這篇文章主要介紹了Visual Studio 2015 配置 Opencv3.2的圖文詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
asp.net sql 數(shù)據(jù)庫(kù)處理函數(shù)命令
asp.net sql 數(shù)據(jù)庫(kù)處理函數(shù)命令 ,需要的朋友可以參考下。2009-10-10
EntityFramework 6.x學(xué)習(xí)之多個(gè)上下文遷移實(shí)現(xiàn)分布式事務(wù)詳解
這篇文章主要給大家介紹了關(guān)于EntityFramework 6.x學(xué)習(xí)之多個(gè)上下文遷移實(shí)現(xiàn)分布式事務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
Ubuntu16.04系統(tǒng)搭建.Net Core開發(fā)環(huán)境
本文詳細(xì)講解了Ubuntu系統(tǒng)搭建.Net Core開發(fā)環(huán)境的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
ASP.NET?MVC打印表格并實(shí)現(xiàn)部分視圖表格打印
這篇文章介紹了ASP.NET?MVC打印表格并實(shí)現(xiàn)部分視圖表格打印的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08

