客户端页面缓存方案

方案一:全局,默认是有缓存,这里设置取消缓存

app.UseStaticFiles(new StaticFileOptions()
{
    OnPrepareResponse = context =>
    {
        //context.Context.Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.CacheControl] = "no-cache";//不做本地缓存,但会对比服务器上的文件的摘要
        context.Context.Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.CacheControl] = "no-store";//完全不做存储缓存,每次f5刷新都是获取最新数据
    }
});

方案二:缓存指定的action

        public IActionResult Index()
        {
            //缓存当前action
            base.HttpContext.Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.CacheControl] = "public,max-age=3600";
            return View();
        }

方案三:缓存指定的action

        [ResponseCache(Duration =3600)]//缓存当前action
        public IActionResult Index()
        {
            return View();
        }

方案四:自定义

    public class MyCacheFilterAttribute : Attribute, IActionFilter, IFilterMetadata
    {
        public int Duration { get; set; }
        public void OnActionExecuted(ActionExecutedContext context)
        {
            int timesecond=this.Duration==0 ? 60 : this.Duration;
            context.HttpContext.Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.CacheControl] = $"public,max-age={timesecond}";
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            //throw new NotImplementedException();
        }
    }

应用:

        [MyCacheFilter(Duration =60)]
        public IActionResult Index()
        {
            return View();
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值