網(wǎng)站首頁 編程語言 正文
在.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>
原文鏈接:https://www.cnblogs.com/afei-24/p/11000367.html
相關(guān)推薦
- 2022-08-19 python如何使用contextvars模塊源碼分析_python
- 2022-05-20 Maven的配置及使用
- 2023-07-22 linux查看進(jìn)程的啟動(dòng)路徑:ll /proc/PID
- 2022-05-09 Python的ini配置文件你了解嗎_python
- 2022-06-11 MSSQL基本語法及實(shí)例操作語句_MsSql
- 2022-06-18 C#實(shí)現(xiàn)多線程編程的簡單案例_C#教程
- 2022-05-25 <C++>搞明白構(gòu)造函數(shù)和析構(gòu)函數(shù)有這一篇就夠了
- 2022-02-02 bat命令刪除文件夾
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支