最近给SpaceBuilder增加OutputCache 时发现了一些问题,贴在这做个备忘,也方便遇到类似问题的朋友查阅。

目前SpaceBuilder表现层使用是asp.net mvc v1.0,使用了很多RenderAction(关于asp.net mvc的Partial Requests参见Partial Requests in ASP.NET MVC)。希望对于实时性要求不高的内容区域采用客户端缓存来提升性能同时也弥补一下RenderAction对性能的损失。

使用asp.net mvc自带的OutputCache Filter时发现了一个可怕的bug,在View中任何一个RenderAction设置OutputCache却影响了整个View。搜索发现确实是asp.net mvc目前已知的一个bug ,关于该问题的解决也有很多人提出了自己的方法。

关于asp.net mvc的缓存,Haacked写了两篇文章:

Donut Caching in ASP.NET MVC 介绍的是缓存整个页面,对于一部分内容禁用缓存,是在mvc中实现的WebForm的Substitution功能。存在以下弊端:当前一个View中有多个区域需要禁用缓存时使用比较麻烦,另外不能实现对页面的不同的区域使用不同的过期策略。

Donut Hole Caching in ASP.NET MVC介 绍的是我想要的功能,即只缓存页面的部分区域。但是弊端也非常明显:只能通过WebForm中的声明方式来使用用户控件(:),现在已经有点不适应这种方 式了,而且必须使用WebFormViewEngine),无法直接使用RenderPartial,而且还必须设置强类型的ViewPage,确保在用 户控件中的Model与View中的Model相同。使用太麻烦,限制也多。

Maarten Balliauw在 Creating an ASP.NET MVC OutputCache ActionFilterAttributeExtending ASP.NET MVC OutputCache ActionFilterAttribute - Adding substitution   也提出了一个完整的OutputCache解决方案。但是经测试启用客户端缓存时同样会产生与RenderAction同样的问题,还没有时间彻查这个问题,先把客户端缓存禁用,暂时使用服务器端缓存应付一阵。

以Maarten Balliauw的代码为原型,编写了SpaceBuilder的ActionOutputCacheAttribute:


    publicclass ActionOutputCacheAttribute : ActionFilterAttribute     {         privatestatic MethodInfo _switchWriterMethod =typeof(HttpResponse).GetMethod("SwitchWriter", BindingFlags.Instance | BindingFlags.NonPublic);          public ActionOutputCacheAttribute(int cacheDuration)         {             _cacheDuration = cacheDuration;         }          //目前还不能设置为Client缓存,会与OutputCache同样的问题         private CachePolicy _cachePolicy = CachePolicy.Server;         privateint _cacheDuration;         private TextWriter _originalWriter;         privatestring _cacheKey;          publicoverridevoid OnActionExecuting(ActionExecutingContext filterContext)         {             // Server-side caching?             if (_cachePolicy == CachePolicy.Server || _cachePolicy == CachePolicy.ClientAndServer)             {                 _cacheKey = GenerateCacheKey(filterContext);                 CacheContainer cachedOutput = (CacheContainer)filterContext.HttpContext.Cache[_cacheKey];                 if (cachedOutput !=null)                 {                     filterContext.HttpContext.Response.ContentType = cachedOutput.ContentType;                     filterContext.Result =new ContentResult { Content = cachedOutput.Output };                 }                 else                 {                     StringWriter stringWriter =new StringWriterWithEncoding(filterContext.HttpContext.Response.ContentEncoding);                     HtmlTextWriter newWriter =new HtmlTextWriter(stringWriter);                     _originalWriter = (TextWriter)_switchWriterMethod.Invoke(HttpContext.Current.Response, newobject[] { newWriter });                 }             }         }          publicoverridevoid OnResultExecuted(ResultExecutedContext filterContext)         {             // Server-side caching?             if (_cachePolicy == CachePolicy.Server || _cachePolicy == CachePolicy.ClientAndServer)             {                 if (_originalWriter !=null) // Must complete the caching                 {                     HtmlTextWriter cacheWriter = (HtmlTextWriter)_switchWriterMethod.Invoke(HttpContext.Current.Response, newobject[] { _originalWriter });                     string textWritten = ((StringWriter)cacheWriter.InnerWriter).ToString();                     filterContext.HttpContext.Response.Write(textWritten);                     CacheContainer container =new CacheContainer(textWritten, filterContext.HttpContext.Response.ContentType);                     filterContext.HttpContext.Cache.Add(_cacheKey, container, null, DateTime.Now.AddSeconds(_cacheDuration), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);                 }             }         }          privatestring GenerateCacheKey(ActionExecutingContext filterContext)         {             StringBuilder cacheKey =new StringBuilder("OutputCacheKey:");              // Controller + action             cacheKey.Append(filterContext.Controller.GetType().FullName.GetHashCode());             if (filterContext.RouteData.Values.ContainsKey("action"))             {                 cacheKey.Append("_");                 cacheKey.Append(filterContext.RouteData.Values["action"].ToString());             }              foreach (KeyValuePair<string, object> pair in filterContext.ActionParameters)             {                 cacheKey.Append("_");                 cacheKey.Append(pair.Key);                 cacheKey.Append("=");                  if (pair.Value !=null)                     cacheKey.Append(pair.Value.ToString());                 else                     cacheKey.Append(string.Empty);             }              return cacheKey.ToString();         }          privateclass CacheContainer         {             publicstring Output;             publicstring ContentType;             public CacheContainer(string data, string contentType)             {                 Output = data;                 ContentType = contentType;             }         }          publicenum CachePolicy         {             NoCache =,             Client =,             Server =,             ClientAndServer =         }     }

    { encoding;        

StringWriterWithEncoding     publicclass StringWriterWithEncoding : StringWriter     {         Encoding encoding;         public StringWriterWithEncoding(Encoding encoding)         {             this.encoding = encoding;         }         publicoverride Encoding Encoding         {             get{ return encoding; }         }     }

转自:http://www.cnblogs.com/mazq/archive/2009/05/30/1492298.html

asp.net mvc Partial OutputCache 在SpaceBuilder中的应用实践的更多相关文章

  1. Asp.net mvc web api 在项目中的实际应用

    Asp.net mvc web api 在项目中的实际应用 前言:以下只是记录本人在项目中的应用,而web api在数据传输方面有多种实现方式,具体可根据实际情况而定! 1:数据传输前的加密,以下用到 ...

  2. 在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下:

    Asp.net MVC中的ViewData与ViewBag ViewData ViewBag 它是Key/Value字典集合 它是dynamic类型对像 从Asp.net MVC 1 就有了 ASP. ...

  3. ASP.NET MVC 学习8、Controller中的Detail和Delete方法

    参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and ...

  4. ASP.NET MVC 学习4、Controller中添加SearchIndex页面,实现简单的查询功能

    参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-method ...

  5. [小技巧][ASP.Net MVC Hack] 使用 HTTP 报文中的 Header 字段进行身份验证

    在一些 Web 系统中,身份验证是依靠硬件证书进行的:在电脑上插入 USB 证书,浏览器插件读取证书的相关信息,然后在发送 HTTP 登录请求时顺便在 Header 字段附加上身份信息.服务器端处理这 ...

  6. [转]Load ASP.NET MVC Partial Views Dynamically Using jQuery

    本文转自:http://www.binaryintellect.net/articles/218ca630-ba50-48fe-af6e-6f754b5894aa.aspx Most of the t ...

  7. ASP.NET MVC Partial页输出JS

    很多情况Partial是需要引用到JS的,通常做法是吧JS在引用Partial的页面中加入JS文件或者JS代码. 前阵子网上看到一段代码可以在Partial页面中添加JS,输出道引用页面. publi ...

  8. ASP.NET MVC系列:web.config中ConnectionString aspnet_iis加密与AppSettings独立文件

    1. web.config中ConnectionString aspnet_iis加密 web.config路径:E:\Projects\Libing.Web\web.config <conne ...

  9. ASP.NET MVC做的微信WEBAPP中调用微信JSSDK扫一扫

    今天做一个项目,是在微信上用的,微信WEB APP,里面用到了调用手机摄像头扫一扫二维码的功能,记得以前某个项目里写有的,但是找不到之前那个项目源码了,想复制粘贴也复制不了了,只好对着微信的那个开发文 ...

随机推荐

  1. 自定义弹窗 VS AlertDialog分享弹窗

    一.摘要 弹窗通常用于提示用户进行某种操作,比如:点击分享按钮,弹窗分享对话框:双击返回按钮,弹窗退出对话框:下载文件,提示下载对话框等等,分享对话框/退出对话框/下载对话框,都可以直接使用Alert ...

  2. linux sort按照指定列排序

    sort怎样按指定的列排序0000 27189 41925425065f 15 419254250663 7 419254250675 5 419254250691 76 419254250693 2 ...

  3. 关于python2中的unicode和str以及python3中的str和bytes

    python3有两种表示字符序列的类型:bytes和str.前者的实例包含原始的8位值:后者的实例包含Unicode字符. python2中也有两种表示字符序列的类型,分别叫做str和unicode. ...

  4. HDU - 1430 魔板 【BFS + 康托展开 + 哈希】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路 我刚开始 想到的 就是 康托展开 但是这个题目是 多组输入 即使用 康托展开 也是会T的 ...

  5. maven导入项目时,缺少部分source folder

    今天导入公司的maven项目时,少了一些source folder,运行启动正常,但是页面打不开,找不到对应的目录文件,使用maven更新项目,重启编辑器也无效. 问题描述如图所示,缺少了图中的2个目 ...

  6. cookie补充

    之前写cookie中关于突破同源策略共享cookie说的比较含糊,此次来详细说明一下: ## 首先说一下cookie的path和domain这 两个属性值 path: path表示的此条cookie是 ...

  7. java常用注解(更新中)

    注解根据来源可分为: 系统注解(自带的,取决于JDK版本).自定义注解及第三方注解 系统注解根据用途又可分为: java内置注解和元注解 根据运行机制(保留到什么时候)可分为: 源码注解.编译注解和运 ...

  8. Delphi 运行Word VBA 宏 删除软回车

    Sub 整理网页()'整理网页:删除软回车.删除空白段.使段落文字两端对齐Selection.WholeStory        Selection.Find.ClearFormatting    S ...

  9. castle windsor学习-----Registering components by conventions

    注册多个组件 1.one-by-one注册组件可能是一项非常重复的工作,可以通过Classes或Types注册一组组件(你可以指定一些特定的特征) 三个步骤 注册多个类型通常采取以下结构 contai ...

  10. poj 3666 Making the Grade(dp离散化)

    Making the Grade Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7068   Accepted: 3265 ...