Nginx缓存、本地responseCache响应式缓存、ResourceFilter缓存。

1.配置缓存存放地址

proxy_cache_path  /WangCong/nginx-1.18.0/data  levels=1:2 keys_zone=web_cache:50m inactive=10m max_size=1g;

写在最上面,路径去掉盘符。

 

 2.配置缓存哪些文件

location /First/ {
proxy_store off;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X_Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://testaddress/First/;

proxy_cache web_cache;
proxy_cache_valid 200 304 2m;
proxy_cache_key $scheme$proxy_host$request_uri; #url做key
}

 

 

 

3.修改完配置文件后,重启

 

二。本地响应式缓存

本地responseCache也可以基本实现nginx的效果,把缓存存在内存,而nginx是把缓存数据存在一个文件夹中。唯一不同的是,刷新页面时,本地响应式缓存请求头有Cache-Control = "max-age=0",会强制从后端重新取数据,不会取缓存数据。nginx则还是取缓存数据。

       public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();

            //redis分布式缓存
            services.AddDistributedRedisCache(options =>
            {
                options.Configuration = "127.0.0.1:6379";
                options.InstanceName = "RedisDistributionedCache20220409";
            });

            services.AddMemoryCache(options => {
                options.Clock = new LocalClock();
            });

            services.AddResponseCaching();
        }

        private class LocalClock : ISystemClock {
            public DateTimeOffset UtcNow => DateTime.Now;
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseResponseCaching();
        }

在startup类中配置完成后,需要在相应的方法上加上特性

    public class FirstController : Controller
    {
        [ResponseCache(Duration =60)]   //1.http协议缓存,增加请求头cache-control。2.响应式缓存也会用到。
        //[ResponseCacheActionFilter(Duration =95)] 
        public IActionResult Index()
        {
            base.ViewBag.Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff");
            //base.HttpContext.Response.Headers[HeaderNames.CacheControl] = "public,max-age=60";
            return View();
        }
    }

 三,ResourceFiter缓存

ResourceFilter缓存的是ActionResult(例如View, JsonResult),页面需要经过Razor的渲染。所以如果写在页面的,不是固定的页面。需要经过Razor的渲染。

首先创建ResourceFilter的特性

    public class ResponseCacheResourseFilterAttribute : Attribute, IResourceFilter,IFilterMetadata, IOrderedFilter
    {
        public static Dictionary<string, IActionResult> cacheDict = new Dictionary<string, IActionResult>();
        public int Order => 1;

        public void OnResourceExecuting(ResourceExecutingContext context)
        {
            string key = context.HttpContext.Request.Path;
            if (cacheDict.ContainsKey(key))
            {
                context.Result = cacheDict[key];
            }
            //throw new NotImplementedException();
        }

        public void OnResourceExecuted(ResourceExecutedContext context)
        {
            string key = context.HttpContext.Request.Path;
            cacheDict.Add(key, context.Result);
        }
    }

2.其次在需要缓存的方法上加上特性

public class SecondController : Controller
    {
        [ResponseCacheResourseFilter]
        public IActionResult Index()
        {
            base.ViewBag.Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff");
            return View();
        }
    }

会发现,存在ViewBag.Now的信息会一直在缓存中,不变。而写在页面上的变量@DateTime.Now会变化

<div class="text-center">
    <h1 class="display-4">First</h1>
    <h3>后台 DateTime: @ViewBag.Now</h3>
    <h3>DateTime当前时间: @DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")</h3>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

 

posted @ 2022-04-06 00:00  留下成长的足迹  阅读(288)  评论(0编辑  收藏  举报