3

I have an action that uses outputcaching

On the view that is rendered by the action I display the current time to check if the output comes from the cache:

   <!-- CacheCheck <%= DateTime.Now.ToString()%>-->

I realized that the page is not cached as long as specified in the web.config when the site comes under some pressure. Is there a way I can check why the content of the cache is being removed? I usually use something like:

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

But I dont find a way to do this with outputcaching.

Here is my code:

[OutputCache(CacheProfile = "HomeCaching")]
public ActionResult Index(HomeViewModel model)

It is set in web.config like this:

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <clear />
      <add name="HomeCaching" enabled="false" duration="120" varyByCustom="homecaching" location="Server" />
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

The varyByCustom Method woks like this:

    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);
    }
Mathias F
  • 15,906
  • 22
  • 89
  • 159

0 Answers0