3

我有一个使用输出缓存的操作

在操作呈现的视图上,我显示当前时间以检查输出是否来自缓存:

   <!-- 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);
}

但是我找不到通过输出缓存来做到这一点的方法。

这是我的代码:

[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);
    }
4

0 回答 0