(精华)2020年10月18日 高可用高并发 缓存的使用(HTTP缓存验证)

首先安装包

Marvin.Cache.Headers

配置

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddResponseCaching();
            services.AddHttpCacheHeaders();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseResponseCaching();
            app.UseHttpCacheHeaders();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

使用

[Route("api/[controller]")]
    [ApiController]
    public class Sample01Controller : ControllerBase
    {
        public static IList<Person> Persons = new List<Person>
        {
            new Person{Id = 1, Name = "张三"}
        };
		[HttpCacheValidation]
        [HttpCacheExpiration]
        public ActionResult Get()
        {
            return Ok(Persons);
        }

        [HttpPut]
        public ActionResult Put()
        {
            var person = new Person
            {
                Id = 2,
                Name = "李四"
            };
            Persons.Add(person);
            return Ok();
        }
    }

备注客户端请求响应头带上if-macth="tag的值"就可以缓存

©️2020 CSDN 皮肤主题: 猿与汪的秘密 设计师:上身试试 返回首页