• C#如何使用IResourceFilter实现资源缓存功能
  • 发布于 2个月前
  • 337 热度
    0 评论
  • Zappos
  • 18 粉丝 42 篇博客
  •   
一、使用演示
1.创建 CustomResourceFilter 实现类
using Microsoft.AspNetCore.Mvc.Filters;

namespace StartUp.Filters
{
    public class CustomResourceFilter : Attribute, IResourceFilter
    {
        /// <summary>
        /// 堆代码 duidaima.com
        /// 在xxx资源之前
        /// </summary>
        /// <param name="context"></param>
        public void OnResourceExecuting(ResourceExecutingContext context)
        {
            Console.WriteLine("before");
        }

        /// <summary>
        /// 在xxx资源之后
        /// </summary>
        /// <param name="context"></param>
        public void OnResourceExecuted(ResourceExecutedContext context)
        {
            Console.WriteLine("after");
        }

    }
}
2.在需要过滤的方法上添加注解[CustomResourceFilter],填写参数名为刚刚创建的类名

3.测试

二、应用场景——缓存
2.1 演示代码
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace StartUp.Filters
{
    public class CustomResourceFilter : Attribute, IResourceFilter
    {

        // 定义一个缓存区域
        private static Dictionary<string,object> cache = new Dictionary<string, object>();

        /// <summary>
        /// 在xxx资源之前
        /// </summary>
        /// <param name="context"></param>
        public void OnResourceExecuting(ResourceExecutingContext context)
        {
            // 如果有缓存,则返回缓存值
            string key = context.HttpContext.Request.Path;
            if (cache.ContainsKey(key))
            {
                Console.WriteLine("有缓存了,调用缓存");
                // 只要是给result赋值了,就会终止执行,直接返回结果给调用方
                context.Result = (IActionResult?)cache[key];
            }
        }

        /// <summary>
        /// 在xxx资源之后
        /// </summary>
        /// <param name="context"></param>
        public void OnResourceExecuted(ResourceExecutedContext context)
        {
            Console.WriteLine("方法已经执行了,将结果存入缓存");
            string key = context.HttpContext.Request.Path;
            // 计算结果保存到缓存中
            cache[key] = context.Result;
        }

    }
}
2.2 测试

三、 异步版本 IAsyncResourceFilter
3.1 演示代码
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace StartUp.Filters
{
    public class CustomResourceFilter : Attribute, IAsyncResourceFilter
    {
        /// <summary>
        /// 当xxx资源去执行时
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
        {
            await Console.Out.WriteLineAsync("异步方法执行前");

            ResourceExecutedContext resourceExecutedContext = await next.Invoke();

            await Console.Out.WriteLineAsync("异步方法执行后");
        }
    }
}

3.2 代码改造
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace StartUp.Filters
{
    public class CustomResourceFilter : Attribute, IAsyncResourceFilter
    {
        // 堆代码 duidaima.com
        // 定义一个缓存区域
        private static Dictionary<string, object> cache = new Dictionary<string, object>();

        /// <summary>
        /// 当xxx资源去执行时
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
        {
            string key = context.HttpContext.Request.Path;
            if (cache.ContainsKey(key))
            {
                Console.WriteLine("有缓存了,调用缓存");
                // 只要是给result赋值了,就会终止执行,直接返回结果给调用方
                context.Result = (IActionResult?)cache[key];
            }
            else
            {
                ResourceExecutedContext resourceExecutedContext = await next.Invoke();
                Console.WriteLine("方法已经执行了,将结果存入缓存");
                // 计算结果保存到缓存中
                cache[key] = context.Result;
            }
        }
    }
}

用户评论