4

What options do I have for caching statically added components that render the top menu of the site. It should always highlight the selected section the visitor is currently visiting in the top right corner?

Is it Vary by data or Vary by section or can we not cache statically?

Is there any link with an example

2 Answers 2

4

What Sitecore Climber mentioned for Static Renderings in a layout is correct. However I think you also asked how you would vary the cache by a page (or section). VaryBySection doesn't exist, neither does VaryByUrl. But you can add custom Vary By logic pretty easily. On my blog: https://dylanyoung.dev/insights/customizing-html-caching-with-sitecore-xm-with-mvc/ I talk about how you can do this.

So to achieve a Vary By Url, you would need to extend the GenerateCacheKey pipeline in RenderRendering processor group, using code like this:

using Sitecore;
using Sitecore.Data.Items;
using Sitecore.Mvc.Pipelines.Response.RenderRendering;
using Sitecore.Mvc.Presentation;
using Sitecore.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Site.Infrastructure.Pipelines.Renderings
{
    public class GenerateCacheKey : 
      Sitecore.Mvc.Pipelines.Response.RenderRendering.GenerateCacheKey
    {
        private const string VARYBYURL = "VaryByUrl";

        protected override string GenerateKey(Rendering rendering, RenderRenderingArgs args)
        {
            string cacheKey = base.GenerateKey(rendering, args);
            Item renderingItem = rendering.RenderingItem.InnerItem;

            if (!string.IsNullOrEmpty(renderingItem[VARYBYURL]))
            {
                cacheKey += "_#url:" + Sitecore.Context.RawUrl;
            }

            return cacheKey;
        }
    }
}

And you would patch it by using the following code:

<?xml version="1.0"?>
    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
        <sitecore>
            <pipelines>
                <mvc.renderRendering>
                    <processor patch:instead="processor[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering.GenerateCacheKey, Sitecore.Mvc']"
               type="Site.Infrastructure.Pipelines.Renderings.GenerateCacheKey, Site.Infrastructure" />
                </mvc.renderRendering>
            </pipelines>
        </sitecore>
    </configuration>

You would need to extend /sitecore/templates/System/Layout/Sections/Caching to include the checkbox option to "VaryByUrl" for the rendering that you've defined.

0
1

You can use :

@Html.Sitecore().Rendering("your rendering definition item id", new { Cacheable = true, CacheKey = "your_rendering", Cache_VaryByData = true })

1
  • I think maybe vary by param also is required? May 22, 2017 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.