.net core(.net 6) IResourceFilter 扩展缓存(IAsyncResourceFilter)
参考:.net core(.net 6) IResourceFilter 的基础使用(IAsyncResourceFilter)
缓存:就是一个临时存储区域,以一个Key-value格式保存数据;key---保存数据的标识,也需要这个表示key才能获取缓存。
a 定义一个静态的键值对Dictionary作为临时存储区域
b 在API执行前(OnResourceExecuting)判断该请求是否存在缓存,若存在则直接返回缓存的数据终止action执行
c 在API执行后(OnResourceExecuted)此时说明没有缓存执行了action,就把计算结果存储到缓存内
扩展类:CustomCacheResourceFilterAttribute.cs
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using System; namespace Demo02.Utility.Filters { /// <summary> /// 自定义ResourceFilter扩展缓存 /// </summary> public class CustomCacheResourceFilterAttribute : Attribute, IResourceFilter { private static Dictionary<string, Object> CacheDictionary = new Dictionary<string, object>(); /// <summary> /// API执行后 执行 /// </summary> /// <param name="context"></param> /// <exception cref="NotImplementedException"></exception> public void OnResourceExecuted(ResourceExecutedContext context) { //执行到这里说明之前没有缓存,并已执行了API,需要将该次数据处理的结果存储到缓存中 string key = context.HttpContext.Request.Path; CacheDictionary[key] = context.Result; } /// <summary> /// API执行前 执行 /// </summary> /// <param name="context"></param> /// <exception cref="NotImplementedException"></exception> public void OnResourceExecuting(ResourceExecutingContext context) { //将Url作为缓存标识符,相同url同一数据 string key = context.HttpContext.Request.Path;//Url地址 if(CacheDictionary.ContainsKey(key)) { object oResult = CacheDictionary[key]; IActionResult result = oResult as IActionResult; context.Result = result;//该变量相当于请求数据处理中的一个断路器,如果给Result值就会终止执行返回Result,如果没有赋值为null,就会继续往后执行。 } } } }
API action测试:
/// <summary> /// GET AOP测试 ResourceFilter缓存 /// </summary> /// <returns></returns> [HttpGet()] [Route("ResourceFilter/CustomCacheResourceFilter/{id:int}")] [CustomCacheResourceFilter] public IActionResult Get(int id) { return new JsonResult(new ApiResut<String>() { Success = true, Data = "AOP ResourceFilter 测试", Time = DateTime.Now.ToString() }); }
测试结果:
IAsyncResourceFilter
CustomCacheAsyncResourceFilterAttribute 自定义AsyncResourceFilter标记类
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using System; namespace Demo02.Utility.Filters { /// <summary> /// 自定义AsyncResourceFilter标记类 /// </summary> public class CustomCacheAsyncResourceFilterAttribute : Attribute, IAsyncResourceFilter { private static Dictionary<string, Object> CacheDictionary = new Dictionary<string, object>(); /// <summary> /// API异步action 执行前后 /// </summary> /// <param name="context"></param> /// <param name="next"></param> /// <returns></returns> public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next) { //将Url作为缓存标识符,相同url同一数据 string key = context.HttpContext.Request.Path;//Url地址 if (CacheDictionary.ContainsKey(key)) { object oResult = CacheDictionary[key]; IActionResult result = oResult as IActionResult; context.Result = result;//该变量相当于请求数据处理中的一个断路器,如果给Result值就会终止执行返回Result,如果没有赋值为null,就会继续往后执行。 } else { ResourceExecutedContext executedContext = await next.Invoke(); //执行到这里说明之前没有缓存,并已执行了API,需要将该次数据处理的结果存储到缓存中 CacheDictionary[key] = executedContext.Result; } } } }
API action测试
/// <summary> /// GET AOP测试 ResourceFilter缓存异步 /// </summary> /// <returns></returns> [HttpGet()] [Route("ResourceFilter/CustomCacheAsyncResourceFilter/{id:int}")] [CustomCacheAsyncResourceFilter] public IActionResult AsyncGet(int id) { return new JsonResult(new ApiResut<String>() { Success = true, Data = "AOP ResourceFilter 测试", Time = DateTime.Now.ToString() }); }