Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Mar 20, 2019 @ 14:54
    Ismail Mayat
    0

    donut cache invalidate cache

    So I am working on an inherited site that has overridden default render mvc controller with custom one. That has mapping code that hydrates model. This all works however its slow and once site starts to get 100+ users it falls over. So caching was added however we need to invalidate the cache of items when they are published.

    The cache attribute is set to varyByCustom="url" so this will generate a donut cache key like

    [email protected]#url=url=http://mysite.local/#
    

    Now in my on published event i try to clear the cache like so :

                    cacheManager.RemoveItem(item.ContentType.Alias, item.ContentType.Alias);
    

    The item is not cleared because the generated key looks like:

    [email protected]#
    

    So I updated my code to clear cache like so:

                    var routeDictionary = new RouteValueDictionary { { "url", "url=" + helper.NiceUrlWithDomain(item.Id) } };
    
                string key = cacheManager.KeyBuilder.BuildKey(item.ContentType.Alias, item.ContentType.Alias, routeDictionary);
    
                LogHelper.Info<AppEvents>("removing donut cache item with key " + key);
    
                cacheManager.RemoveItem(item.ContentType.Alias, item.ContentType.Alias, routeDictionary);
    

    and that works. However this assumes you have domain set on root node of site. Ideally I want the key to be generated based on node id. Does anyone know how to do this? I am guessing you create your own donut cache attribute?

    Regards

    Ismail

  • kows 81 posts 151 karma points c-trib
    Mar 20, 2019 @ 15:10
    kows
    101

    I'll give an example setup:

    On Action/Controller:

    [DonutOutputCacheAttribute(CacheProfile = "ContentPage")]
    

    In web.config:

      <system.web>
        <caching>
          <outputCache enableOutputCache="true" />
          <outputCacheSettings>
            <outputCacheProfiles>
              <add name="ContentPage" duration="3600" varyByCustom="nodeid" />
            </outputCacheProfiles>
          </outputCacheSettings>
        </caching>
      </system.web>
    

    In Global.asax:

    public override string GetVaryByCustomString(HttpContext context, string custom)
            {
                var result = base.GetVaryByCustomString(context, custom);
                if (string.IsNullOrEmpty(custom))
                {
                    return result;
                }
    
                if (UmbracoContext.Current.IsFrontEndUmbracoRequest)
                {
                    var keys = custom.Split(';');
                    foreach (var key in keys)
                    {
                        switch (key)
                        {
                            case "nodeid":
                                result += $"pageid={UmbracoContext.Current.PageId}";
                                break;
                        }
                    }
                }
    
                return result;
            }
    

    So basically on your caching profiles you can configure which custom keys should be used for creating the caching key.

    The GetVaryByCustomString-method allows providing the logic for those custom keys.

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Mar 20, 2019 @ 15:44
    Ismail Mayat
    0

    you my friend are a legend that works a treat.

Please Sign in or register to post replies

Write your reply to:

Draft