gpt4 book ai didi

c# - MVC OutputCaching 是否优先于设置缓存响应 header ?

转载 作者:太空狗 更新时间:2023-10-29 18:30:14 26 4
gpt4 key购买 nike

这个问题与my other question有关.

我有一个 MVC 应用程序,所有 Controller 操作都禁用了缓存。我通过在 Application_BeginRequest 中设置缓存响应 header 来做到这一点:

    protected void Application_BeginRequest()
{
HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
}

我确实希望为单个 Controller 操作启用缓存。我用 OutputCache 属性装饰了这个 Action :

[OutputCache(Duration = 300, VaryByParam = "id")]

此操作现在会发生什么?它是因为 OutputCache 属性而被缓存,还是因为响应 header 而未被缓存?

-- 编辑 --

看起来,响应 header 优先。所以我的问题变成了:如何为单个 Controller 操作启用缓存?再次覆盖响应 header ?

最佳答案

这两件事是分开的;响应缓存主要查看客户端看到的内容 - 他们将在不访问服务器的情况下使用什么,或者他们将发送到服务器的修改日期。 p>

但是,OutputCache 专注于服务器;该请求仍将发生(与客户端缓存的内容不同),但有可能(希望很可能)您的方法不会被调用:相反,缓存的版本将被返回。

因此:它未缓存在客户端;发出 HTTP 请求,并且(对于 5 分钟内的请求,对于相同的 id,内存允许)从服务器返回缓存版本(通常减少 IO 和 CPU在服务器上加载)。有道理吗?

关于c# - MVC OutputCaching 是否优先于设置缓存响应 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4078093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com