asp.net core 资源过滤器

1.资源过滤器可以做资源缓存,缓存后,就可以直接从内存中直接取数据,而无需在执行方法。

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Log4NetTest
{
    public class CustomerResouerceFilterAttribute : Attribute, IResourceFilter
    {
        private static readonly Dictionary<string, IActionResult> dic = new();

        public void OnResourceExecuted(ResourceExecutedContext context)
        {
            var path = context.HttpContext.Request.Path;
            dic[path] = context.Result;
        }

        public void OnResourceExecuting(ResourceExecutingContext context)
        {
            var path = context.HttpContext.Request.Path;
            if (dic.ContainsKey(path))
            {
                context.Result = dic[path];
            }
        }
    }
}
        [HttpGet]
        [CustomerResouerceFilter]
        public async Task<IActionResult> Singout()
        {
            await _signInManager.SignOutAsync();
            return Ok("退出成功");
        }
    }

第一次会进Singout方法,然后就缓存到了内存中,下次执行就,直接从内存中取数据,不需要执行方法了。

你可能感兴趣的:(asp.net,后端)