ASP.NET Core 开发-缓存(Caching)
ASP.NET Core 缓存Caching,.NET Core 中为我们提供了Caching 的组件。
目前Caching 组件提供了三种存储方式。
Memory
Redis
SqlServer
学习在ASP.NET Core 中使用Caching。
Memory Caching
1.新建一个 ASP.NET Core 项目,选择Web 应用程序,将身份验证 改为 不进行身份验证。
2.添加引用
Install-Package Microsoft.Extensions.Caching.Memory
3.使用
在Startup.cs 中 ConfigureServices
public void ConfigureServices(IServiceCollection services) { services.AddMemoryCache(); // Add framework services. services.AddMvc(); }
然后在
public class HomeController : Controller { 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); } ViewBag.Cache = result; return View(); } }
这里是简单使用,直接设置缓存。
我们还可以加上过期时间,以及移除缓存,还可以在移除时回掉方法。
过期时间支持相对和绝对。
下面是详细的各种用法。
public IActionResult Index() { string cacheKey = "key"; string result; if (!_memoryCache.TryGetValue(cacheKey, out result)) { result = $"LineZero{DateTime.Now}"; _memoryCache.Set(cacheKey, result); //设置相对过期时间2分钟 _memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromMinutes(2))); //设置绝对过期时间2分钟 _memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions() .SetAbsoluteExpiration(TimeSpan.FromMinutes(2))); //移除缓存 _memoryCache.Remove(cacheKey); //缓存优先级 (程序压力大时,会根据优先级自动回收) _memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions() .SetPriority(CacheItemPriority.NeverRemove)); //缓存回调 10秒过期会回调 _memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions() .SetAbsoluteExpiration(TimeSpan.FromSeconds(10)) .RegisterPostEvictionCallback((key, value, reason, substate) => { Console.WriteLine($"键{key}值{value}改变,因为{reason}"); })); //缓存回调 根据Token过期 var cts = new CancellationTokenSource(); _memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions() .AddExpirationToken(new CancellationChangeToken(cts.Token)) .RegisterPostEvictionCallback((key, value, reason, substate) => { Console.WriteLine($"键{key}值{value}改变,因为{reason}"); })); cts.Cancel(); } ViewBag.Cache = result; return View(); }
Distributed Cache Tag Helper
在ASP.NET Core MVC 中有一个 Distributed Cache 我们可以使用。
我们直接在页面上增加distributed-cache 标签即可。
<distributed-cache name="mycache" expires-after="TimeSpan.FromSeconds(10)"> <p>缓存项10秒过期-LineZero</p> @DateTime.Now </distributed-cache> <distributed-cache name="mycachenew" expires-sliding="TimeSpan.FromSeconds(10)"> <p>缓存项有人访问就不会过期,无人访问10秒过期-LineZero</p> @DateTime.Now </distributed-cache>
这样就能缓存标签内的内容。
如果你觉得本文对你有帮助,请点击“推荐”,谢谢。
GitHub:https://github.com/linezero
博客示例代码GitHub:https://github.com/linezero/Blog
分类:
ASP.NET Core
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?