網站首頁 編程語言 正文
在.NET Core中提供了Caching的組件。目前Caching組件提供了三種存儲方式:
- Memory
- Redis
- SQLSever
1.Memeor Caching
新建一個ASP.NET Core Web應用程序項目,然后安裝 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);
//設置相對過期時間
memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromSeconds(10)));
//設置絕對過期時間
memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromSeconds(10)));
//刪除緩存
memoryCache.Remove(cacheKey);
//設置緩存優先級(程序壓力大時,會根據優先級自動回收)
memoryCache.Set(cacheKey,result,new MemoryCacheEntryOptions()
.SetPriority(CacheItemPriority.NeverRemove));
//過期時緩存回調
memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromSeconds(60))
.RegisterPostEvictionCallback((key, value, reason, substate)
=>
{
nlog.Warn($"鍵{key}值{value}改變,因為{reason}");
}));
//Token過期時,緩存回調
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 標簽
@{
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>
原文鏈接:https://www.cnblogs.com/afei-24/p/11000367.html
相關推薦
- 2022-11-01 Python如何使用qrcode生成指定內容的二維碼并在GUI界面顯示_python
- 2022-08-03 基于PyQt5完成pdf轉word功能_python
- 2022-05-21 基于C++實現信息管理系統_C 語言
- 2022-05-24 Kubernetes部署可視化地圖的十個步驟_云其它
- 2022-03-26 R語言基于Keras的MLP神經網絡及環境搭建_R語言
- 2022-12-08 React狀態更新的優先級機制源碼解析_React
- 2023-04-19 Element UI Table常用使用方法(header-cell-style;表頭中的全選框取消
- 2022-07-24 Golang實現可重入鎖的示例代碼_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支