MVC中配置OutputCache的VaryByParam参数无效的问题
在项目使用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的值的,所以才会出现我们测试时,对每个参数都进行缓存的情况。
解决办法:
- 把VaryByParam 就直接写入代码, 其他的参数在配置中来完成如:OutputCache(CacheProfile = "faceProfile", VaryByParam = "none")
- 自己实现一个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参数无效的问题的更多相关文章
- 【已解决】React中配置Sass引入.scss文件无效
React中配置Sass引入.scss文件无效 在react中使用sass时,引入.scss文件失效 尝试很多方法没法解决,最终找到解决方法,希望能帮助正在坑里挣扎的筒子~ 在node_modules ...
- MVC中URL传多个参数
1.mvc中url传递多个参数不能直接使用&,会报错(从客户端(&)中检测到有潜在危险的 Request.Path 值) 方法①:使用?---/Home/Index/?id=xxx&a ...
- React中配置Sass引入.scss文件无效
React中配置Sass引入.scss文件无效 在react中使用sass时,引入.scss文件失效尝试很多方法没法解决,最终找到解决方法,希望能帮助正在坑里挣扎的筒子~ 在node_modules文 ...
- 在Asp.Net MVC 中配置 Serilog
Serilog 是一种非常简便记录log 的处理方式,使用Serilog可以生成本地的text文件, 也可以通过 Seq 来在Web界面中查看具体的log内容. 接下来就简单的介绍一下在Asp.Net ...
- MVC中的奇葩错误,参数转对象
在使用MVC中遇到一个神奇的错误,特此记录(我在用MVC4时遇到) 上面两张图就是一个变量名进行了修改,其他不变!form里面的参数也是一样的!喜欢尝试的可以尝试一下! 我的变量使用action时出现 ...
- 获取MVC中Controller下的Action参数异常
我现在做的一个项目有一个这样的需求, 比如有一个页面需要一个Guid类型的参数: public ActionResult Index(Guid id) { //doing something ... ...
- mvc中的OutputCache
mvc4中有一个标记属性OutputCache,用来对ActionResult结果进行缓存,如何理解呢?概括地说,就是当你的请求参数没有发生变化时,直接从缓存中取结果,不会再走服务端的Action代码 ...
- 在Spring MVC 中配置自定义的类型转换器
方法一: 实现spring mvc 自带的 Formatter 接口 1.创建一个类来实现Formatter接口 import org.springframework.format.Formatter ...
- Asp.net MVC中文件上传的参数转对象的方法
参照博友的.NET WebApi上传文件接口(带其他参数)实现文件上传并带参数,当需要多个参数时,不想每次都通过HttpContext.Request.Params去取值,就针对HttpRequest ...
随机推荐
- 用UseMiddleware扩展方法注册中间件类
用UseMiddleware扩展方法注册中间件类 .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件 ...
- 爬虫框架YayCrawler
爬虫框架YayCrawler 各位好!从今天起,我将用几个篇幅的文字向大家介绍一下我的一个开源作品——YayCrawler,其在GitHub上的网址是:https://github.com/liush ...
- Liunx+C编程一站式学习
Liunx+C编程一站式学习这本书有什么特点?面向什么样的读者?这本书最初是为某培训班的嵌入式系统Linux工程师就业班课程量身定做的教材之一.该课程是为期四个月的全日制职业培训,要求学员毕业时具备非 ...
- 开心菜鸟系列----函数作用域(javascript入门篇)
1 <!DOCTYPE html> 2 <html> 3 <script src="./jquery-1.7.2.js"></ ...
- WINDOWS 2012忘记密码之后。。。
要使用windows server 2012安装DVD,选择光盘引导进入 进入修复系统 选择疑难解答,进入界面 选择命令提示符 切换目录至系统目录 执行move命令 执行 copy命令 重新启动系统, ...
- UI设计(流程/界面)设计规范
1.UI设计基本概念与流程 1.1 目的 规范公司UI设计流程,使UI设计师参与到产品设计整个环节中来,对产品的易用性进行全流程负责,使UI设计的流程规范化,保证UI设计流程的可操作性. 1.2范围 ...
- 使用itextsharp创建PDF文档——图片集合
文档管理系统中 ,扫描模块将文档或证件扫描后.为了便于保存多个图片,拟将多个图片生成一个PDF文档进行保存. 这里我们就需要PDF生成工具了.你可以在这里下载.PDFCreator 主要使用了开源工具 ...
- linux下的守护进程及会话、进程组
守护进程.会话.进程组网上有许多不错的资料.我也是网上搜罗了一堆,加上自己的理解.不敢说原创,只是写在这怕自己忘记罢了.才疏学浅,难免有错误,欢迎大家指正.下面这篇写很不错,大家可以去看看:http: ...
- input表单 radio对象的判断选择
/*选择约见对象*/ var lenth=$('input[name="single"]').length; var len=0; $('input[name="sing ...
- jmock2.5基本教程(转)
原文:http://www.cnblogs.com/zfc2201/archive/2011/12/30/2307970.html jmock2.5基本教程 目录 第0章 概述 第1章 jmock初体 ...