• 使用 Qjx.CustomCache 在接口进行AOP 数据缓存


    日常工作中免不了用缓存,总不能在每个代码块加上缓存

    我用Qjx.CustomCache 中间件,进行AOP拦截,统一设置API缓存

    步骤如下

    (1)引入nuget:Qjx.CustomCache

    (2)改造Program

      public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    //改用Autofac来实现依赖注入
                    .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                        
                    });

    (3)改造Startup

      public IConfiguration Configuration { get; }
    
    
            //autofac 新增
            public ILifetimeScope AutofacContainer { get; private set; }
      public void ConfigureServices(IServiceCollection services)
            {
                //支持redis
               services.AddRedisCacheSetup(o=> { o.Configuration = "localhost"; });
                //支持本地缓存
                 //services.AddMemoryCacheSetup();
    //other code
    public void ConfigureContainer(ContainerBuilder builder)
            {
    
                builder.RegisterType<CacheAOP>();
                var assembly = this.GetType().GetTypeInfo().Assembly;
                builder.RegisterAssemblyTypes(assembly).EnableClassInterceptors();
          }

    (4)改造API 在需要AOP拦截的类上加上

    [Intercept(typeof(CacheAOP))]
    [Intercept(typeof(CacheAOP))]
        [ApiController]
        [Route("[controller]")]
    
        public class WeatherForecastController : ControllerBase

    (5)改造方法

    在需要缓存的地方加上如下缓存单位S

    [Caching(AbsoluteExpiration = 1000, KeyName = "id-{#id}", Method = CacheMethod.Add)
               ]
      [Route("adddata1")]
            [HttpGet]
            [Caching(AbsoluteExpiration = 1000, KeyName = "id-{#id}", Method = CacheMethod.Add)
               ]
            public virtual  Xuliehua adddata1([FromQuery] Myid myid)
            {
                Debug.WriteLine("this is getnme");
                Xuliehua hua = new Xuliehua();
                hua.obj = DateTime.Now.ToString();
                return hua;
            }
  • 相关阅读:
    AVUE 根据 某个字段 倒序查询
    Java hutool工具包的使用
    AVUE 添加搜索项
    SpringBlade 添加 回收站功能
    接口 form-data 将对象转换为复杂url参数
    AVUE 隐藏 新增按钮
    AVUE 查看crud列的属性配置
    AVUE dialog对话框 去掉 点击屏幕空白区 就关闭弹框
    接口 C#/Java 请求数据 raw 的方式传输复杂对象
    接口 PostMan 常用
  • 原文地址:https://www.cnblogs.com/qiejinxing/p/14654271.html
Copyright © 2020-2023  润新知