惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
Vercel News
Vercel News
Recent Announcements
Recent Announcements
博客园 - Franky
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
宝玉的分享
宝玉的分享
I
InfoQ
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
V
V2EX
U
Unit 42
Stack Overflow Blog
Stack Overflow Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
T
The Blog of Author Tim Ferriss
量子位

博客园 - 风雨行者

asp.net core 基于autofac 实现AOP 拦截 之 第三种方式 -基于class asp.net core 基于autofac 实现AOP 拦截 之 第二种方式 -基于class asp.net core 3.1 应用Aufac 进行AOP 的三种方式1 基于接口AOP linux之 tomcat 安装配置入门 linux的nginx的安装以及负载均衡 linux 系统管理 linux 服务管理 redis主从复制 读写分离 Redis 集群部署之Redis 安装(1) hbuilder 运行在 android 模拟器中 asp.net digest 摘要认证过程 转载:Spring Boot 不使用默认的 parent,改用自己的项目的 paren .NET Core CLI 的性能诊断介绍 转载学习:windows下将ES和kibana作为服务启动 转(以作记录):cmd命令行---进行Windows服务操作 在 .NET Core 中使用 Diagnostics MyEclipse 使用外部tomcat 调试springboot Git与GitHub 学习笔记 dropdownlist 支持键盘拼音定位选择
使用 Qjx.CustomCache 在接口进行AOP 数据缓存
风雨行者 · 2021-04-13 · via 博客园 - 风雨行者

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

我用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;
        }