我有一个使用outputcaching
的动作在操作呈现的视图上,我显示当前时间以检查输出是否来自缓存:
<!-- CacheCheck <%= DateTime.Now.ToString()%>-->
我意识到,只要web.config中指定该网站受到一些压力,页面就不会被缓存。 有没有办法可以检查为什么要删除缓存的内容?我通常使用类似的东西:
HttpRuntime.Cache.Add(manufacturersCacheKey, manufacturers, null, DateTime.MaxValue, TimeSpan.FromDays(10), CacheItemPriority.Default, new CacheItemRemovedCallback(this.RemovedCallback));
public void RemovedCallback(String cacheKey, Object cacheValue, CacheItemRemovedReason reason)
{
_log.InfoFormat("RemovedCallback Cache Bereich {0} Grund: {1}", cacheKey, reason);
}
但是我找不到使用outputcaching的方法。
这是我的代码:
[OutputCache(CacheProfile = "HomeCaching")]
public ActionResult Index(HomeViewModel model)
在web.config中设置如下:
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<clear />
<add name="HomeCaching" enabled="false" duration="120" varyByCustom="homecaching" location="Server" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
varyByCustom方法就像这样:
public override string GetVaryByCustomString(HttpContext context, string arg)
{
if (arg.ToLower() == "homecaching")
{
string rval = "";
HttpCookie cookieBrowserOverride = context.Request.Cookies.Get(".ASPXBrowserOverride");
if (context.Request.Browser.IsMobileDevice && cookieBrowserOverride == null
|| !context.Request.Browser.IsMobileDevice && cookieBrowserOverride != null
)
{
rval = "mobile-";
}
rval += context.Request.Url.ToString();
HttpCookie cookieASPXAUTH = context.Request.Cookies.Get(".ASPXAUTH");
if (cookieASPXAUTH != null)
{
if (cookieASPXAUTH.Value.Length > 0)
{
rval = rval + "auth";
}
}
if (rval.Length > 0)
{
return rval;
}
return "";
}
return base.GetVaryByCustomString(context, arg);
}