ASP.NET?Core中的Caching組件簡介
在.NET Core中提供了Caching的組件。目前Caching組件提供了三種存儲方式:
- Memory
- Redis
- SQLSever
1.Memeor Caching
新建一個ASP.NET Core Web應(yī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è)置相對過期時間
memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromSeconds(10)));
//設(shè)置絕對過期時間
memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromSeconds(10)));
//刪除緩存
memoryCache.Remove(cacheKey);
//設(shè)置緩存優(yōu)先級(程序壓力大時,會根據(jù)優(yōu)先級自動回收)
memoryCache.Set(cacheKey,result,new MemoryCacheEntryOptions()
.SetPriority(CacheItemPriority.NeverRemove));
//過期時緩存回調(diào)
memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromSeconds(60))
.RegisterPostEvictionCallback((key, value, reason, substate)
=>
{
nlog.Warn($"鍵{key}值{value}改變,因為{reason}");
}));
//Token過期時,緩存回調(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}改變,因為{reason}");
}));
}
ViewBag.Cache = result;
return View();
}2.Distributed Cache Tag Helper
在ASP.NET Core MVC 中有一個 Distributed Cache Tag Helper,它是依賴于MemoryCache組件的。
可以直接在試圖上增加 distributed-cache 標(biāo)簽
@{
ViewData["Title"] = "Home Page";
}
<distributed-cache name="mycache" expires-after="TimeSpan.FromSeconds(10)">
<p>緩存項10秒過期(expires-after絕對過期時間)</p>
</distributed-cache>
<distributed-cache name="mycachenew" expires-sliding="TimeSpan.FromSeconds(10)">
<p>相對十秒(expires-sliding相對過期時間)</p>
@DateTime.Now
</distributed-cache>
<div>@ViewBag.Cache</div>以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用.NET升級助手將.NET?Framework項目升級為.NET?6
這篇文章介紹了使用.NET升級助手將.NET?Framework項目升級為.NET?6的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
使用UserControl做網(wǎng)站導(dǎo)航條的思路 分析
使用UserControl做網(wǎng)站導(dǎo)航條的思路 分析...2007-09-09
asp.net中ListBox 綁定多個選項為選中及刪除實現(xiàn)方法
文章介紹了關(guān)于在asp.net中的listbox的綁定多個選項和同時選中多個選項以及刪除多個選項的方法2012-04-04
asp.net錯誤處理Application_Error事件示例
Application_Error事件與Page_Error事件相類似,可使用他捕獲發(fā)生在應(yīng)用程序中的錯誤。由于事件發(fā)生在整個應(yīng)用程序范圍內(nèi),因此您可記錄應(yīng)用程序的錯誤信息或處理其他可能發(fā)生的應(yīng)用程序級別的錯誤2014-01-01
asp.net中使用 Repeater控件拖拽實現(xiàn)排序并同步數(shù)據(jù)庫字段排序
這篇文章主要介紹了asp.net中使用 Repeater控件拖拽實現(xiàn)排序并同步數(shù)據(jù)庫字段排序的相關(guān)資料,需要的朋友可以參考下2015-12-12
配置Visual Studio 以調(diào)試.net framework源代碼
看到.net框架代碼發(fā)布了,興奮了一下,把在Visual Studio 2008上配置的內(nèi)容翻譯了一下,只翻譯了原文的基本步驟,高級用戶篇和QA沒有翻譯。2009-04-04
Entity?Framework使用DataBase?First模式實現(xiàn)數(shù)據(jù)庫的增刪改查
本文詳細(xì)講解了Entity?Framework使用DataBase?First模式實現(xiàn)數(shù)據(jù)庫的增刪改查,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03

