热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

从Global.asax启用全局缓存-EnableCacheGloballyfromGlobal.asax

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 个解决方案

#1


1  

I think you can achieve the output caching by adding OutputCacheAttribute to the filters on application start.

我认为您可以通过在应用程序启动时向过滤器添加OutputCacheAttribute来实现输出缓存。

There's probably RegisterGlobalFilters(GlobalFilters.Filters); in your Application_Start. So inside RegisterGlobalFilters add:

有可能RegisterGlobalFilters(GlobalFilters.Filters);在你Application_Start。所以在RegisterGlobalFilters添加:

filters.Add(new OutputCacheAttribute { Duration = 300, VaryByHeader = "Host", VaryByParam = "myparam" });

Obviously this could have unintended side effects since nearly everything that is rendered will be cached for 300 seconds. If you don't want to have that on every single action, I'd look into the [OutputCache] attribute, and decorate the controllers you want to be cached.

显然,这可能会产生意外的副作用,因为几乎所有呈现的内容都会被缓存300秒。如果您不希望在每个操作上都有这个属性,我将查看[OutputCache]属性,并修饰希望缓存的控制器。

#2


1  

You should try building a custom cache attribute.

您应该尝试构建一个自定义缓存属性。

In that custom attribute skip the cache, if the url (Request.Url.ToString()) contains the 'private' keyword.

在该自定义属性中,如果url (request . url . url . tostring()包含“private”关键字,则跳过缓存。

For an easy sample on how to build a custom cache attribute, use this blogpost from Steven Sanderson. Hope this helps.

要了解如何构建自定义缓存属性的简单示例,请使用Steven Sanderson的博客文章。希望这个有帮助。


推荐阅读
author-avatar
手机用户2702934194
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有