Copied to clipboard

Flag this post as spam?

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


  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jun 10, 2016 @ 15:59
    Simon Dingley
    0

    CachedPartial Contextual Keys

    I have a multi site install with about 7 different languages, the templates etc are common across all sites so the navigation etc needs to be unique across the different sites. This has historically been a problem with the wrong navigation being cached and displayed on the wrong sites sometimes. I've just completed an upgrade to 7.4.3 and the problem has reared it's ugly head again.

    Could someone please clarify that the following should cache the FooterSectionLinks.cshtml partial uniquely per hostname because it doesn't always seem to?

    @Html.CachedPartial("~/Views/Partials/Navigation/FooterSectionLinks.cshtml", Model.Content, 604800, contextualKeyBuilder: (model, viewData) => Request.Url.Host)
    
  • David Peck 690 posts 1896 karma points c-trib
    Jun 29, 2016 @ 15:29
    David Peck
    0

    I can't answer you question, but an alternative approach would be to use Donut Caching. If you've not used, this essentially just allows you to cache Actions. If you can pass the Action the correct info as parameters, then you could easily then just VaryByParam on the DonutOutputCacheAttribute. If you can't then you may need to do VaryByCustom.

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 13, 2016 @ 11:33
    Simon Dingley
    0

    Thanks but that's not an option in this current implementation right now but certainly a consideration for the future.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Jul 13, 2016 @ 12:03
    Dave Woestenborghs
    0

    Hi Simon,

    Does each language have a different host name ?

    Dave

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 13, 2016 @ 12:07
    Simon Dingley
    0

    It does yes.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Jul 13, 2016 @ 12:09
    Dave Woestenborghs
    0

    Hmm..

    Your code seems okay. Can you reproduce it on a clean 7.4.3 install ?

    Dave

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 13, 2016 @ 12:24
    Simon Dingley
    0

    I've not tried no but then I can't even replicate it successfully on the site having the issues either, it seems pretty random.

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Aug 10, 2016 @ 10:49
    Simon Dingley
    0

    Are the keys built with the contextualKeyBuilder appended to the Umbraco generated keys or do they need to be a complete key and for instance include the node id if we want them to vary by page?

    I've not really found any documentation on using contextualKeyBuilder.

    Thanks, Simon

  • David Peck 690 posts 1896 karma points c-trib
    Aug 10, 2016 @ 11:04
    David Peck
    0

    I don't know the answer, but I've assumed that they don't include Umbraco's default ones. Adding keys other than the ones you add could prevent you from caching as you'd wish.

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Aug 10, 2016 @ 11:19
    Simon Dingley
    0

    I thought I'd check the source and it seems it is appended. See the following implementation:

    public static IHtmlString CachedPartial(
                this HtmlHelper htmlHelper, 
                string partialViewName, 
                object model, 
                int cachedSeconds,
                bool cacheByPage = false,
                bool cacheByMember = false,
                ViewDataDictionary viewData = null,
                Func<object, ViewDataDictionary, string> contextualKeyBuilder = null)
            {
                var cacheKey = new StringBuilder(partialViewName);
                if (cacheByPage)
                {
                    if (UmbracoContext.Current == null)
                    {
                        throw new InvalidOperationException("Cannot cache by page if the UmbracoContext has not been initialized, this parameter can only be used in the context of an Umbraco request");
                    }
                    cacheKey.AppendFormat("{0}-", UmbracoContext.Current.PageId);
                }
                if (cacheByMember)
                {
                    var currentMember = Member.GetCurrentMember();
                    cacheKey.AppendFormat("m{0}-", currentMember == null ? 0 : currentMember.Id);
                }
                if (contextualKeyBuilder != null)
                {
                    var contextualKey = contextualKeyBuilder(model, viewData);
                    cacheKey.AppendFormat("c{0}-", contextualKey);
                }
                return ApplicationContext.Current.ApplicationCache.CachedPartialView(htmlHelper, partialViewName, model, cachedSeconds, cacheKey.ToString(), viewData);
            }
    

    Whilst this does confirm what I suspected it unfortunately doesn't explain the recurring issue we keep having.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Aug 10, 2016 @ 11:38
    Dan Diplo
    1

    I use this approach on a multi-lingual site for caching partials once per culture (which is effectively once per site since each site has it's own culture settings).

    @Html.CachedPartial("MyPartial", Model.Content, 3700, contextualKeyBuilder: (model, viewData) => PageContext.Page.Culture)
    

    Seems to work well and caches the partial once per site.

Please Sign in or register to post replies

Write your reply to:

Draft