作者:手机用户2702934194 | 来源:互联网 | 2023-09-16 08:53
Iamtryingtoenableserver-sidecache(OutputCaching)globallyfromGlobal.asaxinaMVC3project
I am trying to enable server-side cache (Output Caching) globally from Global.asax in a MVC3 project.
我正在尝试使全球范围内的服务器端缓存(输出缓存)实现。asax在一个MVC3项目中。
I tried this:
我试着这样的:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Path.Contains("private"))
{
return;
}
Response.Cache.SetExpires(DateTime.Now.AddSeconds(300));
Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.VaryByHeaders["Host"] = true;
Response.Cache.VaryByParams["myparam"] = true;
}
But if I put a DateTime.Now
in one of my pages it changes on every request. So it doesn't seems to work.
但是如果我放一个DateTime。现在在我的一个页面中,它对每个请求都进行修改。所以这似乎行不通。
I have also tried to put it on Application_PreRequestHandlerExecute
event as in this answer with no luck.
我也试过把它放到application_prerequesththandlerexecute事件中,但没有运气。
Is there any way to achieve this behavior from Global.asax?
是否有办法从Global.asax获得这种行为?
Note: I want to filter some URL's from being cached.
注意:我想过滤一些URL的缓存。
Edit: Started a bounty
Steven V's answer put me on the right track, but after hours of developing "my own cache system" extending ActionFilterAttribute
, I had a lot of problems with threads, response types and cache was mashing results so it's a complete disaster (I think it's not even worth pasting the code here).
Steven V的回答让我走上了正确的道路,但是在开发了几个小时的“我自己的缓存系统”扩展ActionFilterAttribute之后,我在线程、响应类型和缓存方面遇到了很多问题,因此这是一个完全的灾难(我认为在这里粘贴代码甚至都不值得)。
Another option is to control it directly from IIS, if there is a cache module for IIS that can help me achieve this or some configuration than can be made on IIS's Output Caching that would be also very helpful. In the end what I try to achieve is fully URL related.
另一种选择是直接从IIS控制它,如果IIS有一个缓存模块可以帮助我实现这个或一些配置,而不是在IIS的输出缓存中进行,这也非常有用。最后,我试图实现的是完全与URL相关的。
The main idea is to have a global cache enabled with filtering, URL depending, controlled from a centralized area.
其主要思想是让全局缓存启用过滤、URL依赖、从集中区域控制。
2 个解决方案