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

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: http://sitecoremaster.com/caching/customizing-html-caching-with-sitecore-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.

1

You can use :

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

  • I think maybe vary by param also is required? – Hishaam Namooya May 22 '17 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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