我正在我的MVC网站中使用OutputCache属性,如下所示:
[OutputCache(Duration = 5000,
VaryByParam = "name;region;model;id;op;content;featured;isStarred;page;size;")]
然而,有时我想完全绕过输出缓存并强制从数据库获取数据。这在我的测试环境中尤为重要,因为我不断地将新数据加载到数据库进行测试。
在这种情况下,有没有办法绕过缓存呢?
谢谢。
我正在我的MVC网站中使用OutputCache属性,如下所示:
[OutputCache(Duration = 5000,
VaryByParam = "name;region;model;id;op;content;featured;isStarred;page;size;")]
然而,有时我想完全绕过输出缓存并强制从数据库获取数据。这在我的测试环境中尤为重要,因为我不断地将新数据加载到数据库进行测试。
在这种情况下,有没有办法绕过缓存呢?
谢谢。
不必在属性中指定所有的输出缓存参数,而是可以使用一个OutputCache模板。
输出缓存模板允许您将所有的输出缓存设置放在web.config文件中,并给该模板命名,然后从属性中指向该模板。
一旦设置完成,您可以修改用于调试的web.config文件中的缓存持续时间,以便只有1秒。显然,您会让生产环境的web.config文件缓存持续时间更长。
有关模板的更多信息,请查看http://msdn.microsoft.com/en-us/library/hdxfb6cy.aspx
<caching>
<outputCache enableOutputCache="false" />
</caching>
System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/");
System.Web.Configuration.OutputCacheSection cacheSection = (System.Web.Configuration.OutputCacheSection)config.GetSection("system.web/caching/outputCache");
cacheSection.EnableOutputCache = true/false;
config.Save();
这可能只适用于您的开发机器。大多数服务器都设置为不允许编辑 machine.config 中的该部分。
enableOutputCache="false"
吗? - MaslowResponse.AddCacheItemDependency("Pages");
public ActionResult RefreshCache()
{
HttpContext.Cache.Insert("Pages", DateTime.Now, null,
DateTime.MaxValue, TimeSpan.Zero,
CacheItemPriority.NotRemovable,
null);
Logger.Info("Cleansed cache");
return RedirectToAction("HubContent");
}
VaryByParam
”:[OutputCache(Duration=5000,VaryByParam="YourExistingParams;CacheBypass")]
当您想要绕过缓存时,只需传递CacheBypass
参数的值为DateTime.UtcNow.Ticks
(或任何随机值)=>例如:http://localhost/?CacheBypass=1234567890
*
),这样您就不必输入所有参数。这将缓存任何参数。但请谨慎使用[OutputCache(Duration=5000,VaryByParam="*")]
- blockloop