ASP.NET Core 开发-缓存(Caching)
2016-08-18 00:00
537 查看
ASP.NET Core 缓存Caching,.NET Core 中为我们提供了Caching 的组件。
目前Caching 组件提供了三种存储方式。
Memory
Redis
SqlServer
学习在ASP.NET Core 中使用Caching。
2.添加引用
3.使用
在Startup.cs 中 ConfigureServices
然后在
这里是简单使用,直接设置缓存。
我们还可以加上过期时间,以及移除缓存,还可以在移除时回掉方法。
过期时间支持相对和绝对。
下面是详细的各种用法。
我们直接在页面上增加distributed-cache 标签即可。
这样就能缓存标签内的内容。
如果你觉得本文对你有帮助,请点击“推荐”,谢谢。
目前Caching 组件提供了三种存储方式。
Memory
Redis
SqlServer
学习在ASP.NET Core 中使用Caching。
Memory Caching
1.新建一个 ASP.NET Core 项目,选择Web 应用程序,将身份验证 改为 不进行身份验证。2.添加引用
Install-Package Microsoft.Extensions.Caching.Memory -Pre
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>
这样就能缓存标签内的内容。
如果你觉得本文对你有帮助,请点击“推荐”,谢谢。
相关文章推荐
- ASP.NET Core 开发-缓存(Caching)
- ASP.NET Core ResponseCaching:基于 VaryByHeader 定制缓存 Key
- ASP.NET Core 开发-中间件(Middleware)
- [Asp.net 5] Caching-缓存预告
- ASP.NET企业开发框架IsLine FrameWork系列之十一--HttpContentProvider 访问缓存
- [翻译]ASP.NET MVC 3 开发的20个秘诀(二十)[20 Recipes for Programming MVC 3]:缓存结果数据加速页面载入
- ASP.net(1.1)原理学习笔记--第九章 缓存Caching
- 如何在ASP.NET中使用缓存Caching
- ASP.NET Core 开发-中间件(StaticFiles)使用
- 使用Donut Caching和Donut Hole Caching在ASP.NET MVC应用中缓存页面
- ASP.NET 开发实践--性能与缓存
- ASP.NET Core WebAPI 开发-新建WebAPI项目
- ASP.NET企业开发框架IsLine FrameWork系列之十一--HttpContentProvider 访问缓存
- [Asp.net 5] Caching-缓存架构与源码分析
- 如何在ASP.NET中使用缓存Caching
- 配置visual studio code进行asp.net core rc2的开发
- asp.net 数据缓存System.Web.Caching.Cache 实例运用
- asp.net 客户端缓存(ClientCaching)
- Web开发中的缓存技术之三:通过ETag实现缓存处理(ASP.NET MVC版)
- memcache实战之四:接口编程(分别用asp.net自带的System.Web.Caching,以及分布式缓存MemcachedProviders)来实现缓存接口