在项目使用OutputCacheAttribute是遇到了问题,当我想在配置文件web.config中配置OutputCache的VaryByParam时竟然不起作用,下面是相关代码:

文件FaceController.cs

    [OutputCache(CacheProfile = "faceProfile")]
public ActionResult Index()
{
return View();
}

文件index.cshtml

<h2>@DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")</h2>

web.config的cache配置

  <system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="faceProfile" duration ="180" varyByParam="none" enabled="true"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
<system.web>

下面是我的测试:

请求 /face/index 页面,输出: 2014-08-02 18:40:56.184

再次请求 /face/index 页面,输出: 2014-08-02 18:40:56.184 (不变)

因为我指定了varyByParam="none",所以我添加参数或改变参数,输出的时间应该不变才对,可是:

请求 /face/index/?p=19999 页面,输出: 2014-08-02 18:42:29.720 (变了)

请求 /face/index/?p=10000 页面,输出: 2014-08-02 18:43:30.981 (变了)

请求 /face/index/?abcd 页面,输出: 2014-08-02 18:44:00.440 (变了)

请求结果随着参数的变化而变化,所以它应该是为每个参数都缓存了一个版本相当于设置了varyByParam="*"

从测试结果可以看出配置文件中设置的duration ="180"有起到作用,而varyByParam="none"没有起到作用.

然后我试着把varyByParam="none"直接写到代码里:

    [OutputCache( Duration=180,VaryByParam="none")]
public ActionResult Index()
{
return View();
}

这次结果它正确进行缓存页面,没有为每个参数缓存一个版本,这使我很困惑,然后我看了一下OutputCacheAttribute的源码,这里我贴出的是部分关键源码:

public class OutputCacheAttribute : ActionFilterAttribute, IExceptionFilter
{ private OutputCacheParameters _cacheSettings = new OutputCacheParameters { VaryByParam = "*" };
//_cacheSettings中的每个属性都以 CacheProfile 属性的方式再包装了一遍
//其余类似的属性我就不贴出来了
public string CacheProfile
{
get{return _cacheSettings.CacheProfile ?? String.Empty; }
set{_cacheSettings.CacheProfile = value;}
}
//省去了无关代码
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
//省去了无关代码 using (OutputCachedPage page = new OutputCachedPage(_cacheSettings))
{
page.ProcessRequest(HttpContext.Current);
}
}
private sealed class OutputCachedPage : Page
{
private OutputCacheParameters _cacheSettings;
public OutputCachedPage(OutputCacheParameters cacheSettings)
{
// Tracing requires Page IDs to be unique.
ID = Guid.NewGuid().ToString();
_cacheSettings = cacheSettings;
}
protected override void FrameworkInitialize()
{
// when you put the <%@ OutputCache %> directive on a page, the generated code calls InitOutputCache() from here
base.FrameworkInitialize();
InitOutputCache(_cacheSettings);
}
}
}

由上面的代码可以看出_cacheSettings根本就没从配置文件里读取,而是直接由 OutputCache( Duration=180,VaryByParam="none") 这样设置进去的,然后直接传人Page.InitOutputCache(cacheSettings)中,可见它直接使用asp.net以前的方式OutputCacheModule来实现缓存的,InitOutputCache中应该有去加载配置里面的设置, 然后看看cacheSettings的相应字段是否已经有合适的值了,如果没有则使用配置里边的值。而我们在使用OutputCache时,如果没有传人VaryByParam值,则它默认的值就为"*"(从cacheSettings初始化时看出来的) 。故代码:

    [OutputCache(CacheProfile = "faceProfile")]
public ActionResult Index()
{
return View();
}

相当于

    [OutputCache(CacheProfile = "faceProfile",VaryByParam = "*")]
public ActionResult Index()
{
return View();
}

配置里的VaryByParam的值是不会覆盖代码里面的VaryByParam的值的,所以才会出现我们测试时,对每个参数都进行缓存的情况。

解决办法:

  1. 把VaryByParam 就直接写入代码, 其他的参数在配置中来完成如:OutputCache(CacheProfile = "faceProfile", VaryByParam = "none")
  2. 自己实现一个OutputCacheAttritube

    下面就是我把OutputCacheAttritube源码做一些修改后满足自己需求的MySimpleCacheAttribute,不过它不适用于ChildAction,但我可以在配置文件中控制VaryByParam参数,改造后的代码如下:
namespace Mvc.Cache
{
using System.Web.Mvc;
using System.Web.UI;
using System;
using System.Web;
public class MySimpleCacheAttribute : ActionFilterAttribute
{
private OutputCacheParameters _cacheSettings = new OutputCacheParameters();
public MySimpleCacheAttribute()
{
}
public string CacheProfile
{
get
{
return _cacheSettings.CacheProfile ?? String.Empty;
}
set
{
_cacheSettings.CacheProfile = value;
}
} internal OutputCacheParameters CacheSettings
{
get
{
return _cacheSettings;
}
}
public int Duration
{
get
{
return _cacheSettings.Duration;
}
set
{
_cacheSettings.Duration = value;
}
}
public OutputCacheLocation Location
{
get
{
return _cacheSettings.Location;
}
set
{
_cacheSettings.Location = value; }
}
public bool NoStore
{
get
{
return _cacheSettings.NoStore;
}
set
{
_cacheSettings.NoStore = value; }
}
public string SqlDependency
{
get
{
return _cacheSettings.SqlDependency ?? String.Empty;
}
set
{
_cacheSettings.SqlDependency = value;
}
}
public string VaryByContentEncoding
{
get
{
return _cacheSettings.VaryByContentEncoding ?? String.Empty;
}
set
{
_cacheSettings.VaryByContentEncoding = value;
}
}
public string VaryByCustom
{
get
{
return _cacheSettings.VaryByCustom ?? String.Empty;
}
set
{
_cacheSettings.VaryByCustom = value;
}
}
public string VaryByHeader
{
get
{
return _cacheSettings.VaryByHeader ?? String.Empty;
}
set
{
_cacheSettings.VaryByHeader = value;
}
}
public string VaryByParam
{
get
{
return _cacheSettings.VaryByParam ?? String.Empty;
}
set
{
_cacheSettings.VaryByParam = value;
}
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (!filterContext.IsChildAction)
{
using (OutputCachedPage page = new OutputCachedPage(_cacheSettings))
{
page.ProcessRequest(HttpContext.Current);
}
}
}
private sealed class OutputCachedPage : Page
{
private OutputCacheParameters _cacheSettings; public OutputCachedPage(OutputCacheParameters cacheSettings)
{
// Tracing requires Page IDs to be unique.
ID = Guid.NewGuid().ToString();
_cacheSettings = cacheSettings;
} protected override void FrameworkInitialize()
{
base.FrameworkInitialize();
InitOutputCache(_cacheSettings);
}
}
}
}

使用:

    [MySimpleCache(CacheProfile = "faceProfile")]
public ActionResult Index()
{
return View();
}

MVC中配置OutputCache的VaryByParam参数无效的问题的更多相关文章

  1. 【已解决】React中配置Sass引入.scss文件无效

    React中配置Sass引入.scss文件无效 在react中使用sass时,引入.scss文件失效 尝试很多方法没法解决,最终找到解决方法,希望能帮助正在坑里挣扎的筒子~ 在node_modules ...

  2. MVC中URL传多个参数

    1.mvc中url传递多个参数不能直接使用&,会报错(从客户端(&)中检测到有潜在危险的 Request.Path 值) 方法①:使用?---/Home/Index/?id=xxx&a ...

  3. React中配置Sass引入.scss文件无效

    React中配置Sass引入.scss文件无效 在react中使用sass时,引入.scss文件失效尝试很多方法没法解决,最终找到解决方法,希望能帮助正在坑里挣扎的筒子~ 在node_modules文 ...

  4. 在Asp.Net MVC 中配置 Serilog

    Serilog 是一种非常简便记录log 的处理方式,使用Serilog可以生成本地的text文件, 也可以通过 Seq 来在Web界面中查看具体的log内容. 接下来就简单的介绍一下在Asp.Net ...

  5. MVC中的奇葩错误,参数转对象

    在使用MVC中遇到一个神奇的错误,特此记录(我在用MVC4时遇到) 上面两张图就是一个变量名进行了修改,其他不变!form里面的参数也是一样的!喜欢尝试的可以尝试一下! 我的变量使用action时出现 ...

  6. 获取MVC中Controller下的Action参数异常

    我现在做的一个项目有一个这样的需求, 比如有一个页面需要一个Guid类型的参数: public ActionResult Index(Guid id) { //doing something ... ...

  7. mvc中的OutputCache

    mvc4中有一个标记属性OutputCache,用来对ActionResult结果进行缓存,如何理解呢?概括地说,就是当你的请求参数没有发生变化时,直接从缓存中取结果,不会再走服务端的Action代码 ...

  8. 在Spring MVC 中配置自定义的类型转换器

    方法一: 实现spring mvc 自带的 Formatter 接口 1.创建一个类来实现Formatter接口 import org.springframework.format.Formatter ...

  9. Asp.net MVC中文件上传的参数转对象的方法

    参照博友的.NET WebApi上传文件接口(带其他参数)实现文件上传并带参数,当需要多个参数时,不想每次都通过HttpContext.Request.Params去取值,就针对HttpRequest ...

随机推荐

  1. jquery 替换元素函数

    1.replaceWith()使用括号内的内容替换所选择的内容.$("#div").replaceWith("<div id="div2"> ...

  2. Android中focusable属性的妙用——底层按钮的实现

    http://www.cnblogs.com/kofi1122/archive/2011/03/22/1991828.html http://www.juziku.com/weizhishi/3077 ...

  3. HDU 4611Balls Rearrangement(思维)

    Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  4. 【转】如何测试CTS4.0 -- 不错

    原文网址:http://blog.csdn.net/subsist/article/details/7209341/ CTS4.0测试步骤 V1.2 (更新到CTS4.0 R3)     第一:平台准 ...

  5. eclipse编辑工具小结

    eclipse编辑工具小结 这两天从myeclipse转入eclipse,整体感觉不错,速度更快些,也没在出现各种意外的调试错误.不能断点等情况,并且对整个编辑环境的使用有了更深入的认识,再次对主要几 ...

  6. vs2005中无法修改控件ID

    方法一:撤换到源代码模式下,通过代码更改id 方法二: 1.关闭VS2005: 2.删除目录 C:\Documents and Settings\Administrator\Local Setting ...

  7. MCM1988 问题B_lingo_装货问题

    两辆平板车的装货问题有七种规格的包装箱要装到两辆铁路平板车上去包装箱的宽和高是一样的但厚度(t,以厘米计)及重量(,以公斤计)是不同的.下表给出了每种包装箱的厚度重量以及数量每辆平板车有10.2 米长 ...

  8. PhpForm表单验证

    <!DOCTYPE HTML> <html> <meta http-equiv="Content-Type" content="text/h ...

  9. Word Break II 解答

    Question Given a string s and a dictionary of words dict, add spaces in s to construct a sentence wh ...

  10. ListView之ArrayAdapter

    ArrayAdapter 普通的显示listView子项,安卓的内置对象 使用方法: /* ListView :列表 通常有两个职责: a.将数据填充到布局 b.处理点击事件 一个ListView创建 ...