MVC OutputCaching 是否优先于设置缓存响应标头?
这个问题与 我的其他问题。
我有一个 MVC 应用程序,对所有控制器操作禁用缓存。我通过在 Application_BeginRequest
中设置缓存响应标头来实现此目的:
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();
}
有一个我确实希望启用缓存的控制器操作。我已使用 OutputCache
属性修饰此操作:
[OutputCache(Duration = 300, VaryByParam = "id")]
此操作现在会发生什么?它是因为 OutputCache 属性而被缓存,还是因为响应标头而未被缓存?
-- 编辑 --
看起来,响应标头优先。所以我的问题是:如何为单个控制器操作启用缓存?再次覆盖响应标头吗?
This question is related to my other question.
I have an MVC application with caching disabled for all controller actions. I do this by setting cache response headers in Application_BeginRequest
:
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();
}
There is single controller action for which I do want caching enabled. I have decorated this action with the OutputCache
attribute:
[OutputCache(Duration = 300, VaryByParam = "id")]
What happens now for this action? Does it get cached because of the OutputCache attribute, or is it not cached because of the response headers?
-- EDIT --
As it seems, the response headers take preference. So my question becomes: how can I enable cache for single controller actions? Overwrite the response headers again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这两件事是分开的;响应缓存主要查看客户端看到的内容 - 他们将在不访问服务器的情况下使用什么,或者他们将发送到服务器的修改日期。
然而,OutputCache 的重点是服务器;请求仍然会发生(与客户端缓存的内容不同),但有可能(希望很可能)您的方法不会被调用:相反,将返回缓存的版本。
所以:它没有缓存在客户端;发出 HTTP 请求,并且(对于 5 分钟内的请求,对于相同的
id
,内存允许)从服务器返回缓存版本(通常会减少 IO 和 CPU)在服务器上加载)。有道理吗?The two things are separate; the response cache is primarily looking at what the client sees - what they will use without hitting the server, or what modified-date they will send up to the server.
OutputCache, however, is focused at the server; the request will still happen (unlike something cached at the client), but it is possible (hopefully likely) that your method won't be called: instead, the cached version will be returned.
So: it is not cached at the client; a HTTP request is made, and (for requests within 5 minutes, for the same
id
, memory permitting) the cached version is returned from the server (typically reducing IO and CPU load at the server). Make sense?响应标头强制执行缓存控制。解决方案是不为需要缓存的控制器操作设置响应标头。我现在不使用
OutputCache
,而是使用自定义缓存属性,该属性还在请求项字典中设置ISCACHED
键。我的问题中的代码片段已更改为:我必须将其从
BeginRequest
移至EndRequest
,以允许操作设置ISCACHED
请求第一个项目。如果设置了,则控制器已经处理了该请求的缓存,否则缓存将被禁用。The response headers enforce the cache control. The solution was not to set response headers for controller actions that require caching. Instead of using
OutputCache
, I'm now using a custom cache attribute that also sets anISCACHED
key in the request items dictionary. The code snippet from my question was changed to this:I had to move this from
BeginRequest
toEndRequest
, to allow actions to set theISCACHED
request item first. If it is set, a controller already handled caching for this request, otherwise caching is disabled.