How to cache content on Sitecore wildcards and multiple renderings?

How to cache content on Sitecore wildcards and multiple renderings?

In order to increase loading performance of a website we can activate the cache.

Sitecore out of the box offers multiple caching options and all we have to do is to choose what kind of logic Sitecore should use to cache one specific rendering. Cache could vary by data, by device, by login, by param and others. Please check image below for all caching options.

A while ago I had an important task to analyze possibilities to increase the website loading performance. The HTML cache is probably the best way to do this.

The problem:

Some pages of the website were WILDCARDS. If you cached a wildcard item then you would have the same content every time you accessed that wildcard no matter if you passed different parameters to your pipeline.

So how to make Sitecore to cache a wildcard and still display the right content?


We need to override the GenerateKey method of the GenerateCacheKey RenderRendering processor.

The code below is very straight forward and it has two additional caching options:

  • Vary By URL
  • Vary By Unique ID

Vary By URL: This will create a new cache key based on each URL the user is accessing.

Vary By Unique ID: This will create a cache for rendering inserted multiple times on a page based on its ID.

namespace Feature.MyHelixFeature.Data.Cache
    public class GenerateCustomCacheKey : GenerateCacheKey
        protected override string GenerateKey(Rendering rendering, RenderRenderingArgs args)
            string baseCacheKey = base.GenerateKey(rendering, args);

            if (rendering?.RenderingItem?.InnerItem?["VaryByUrl"] == "1")
                string absolutePath = HttpContext.Current.Request.Url.AbsolutePath;
                if (!string.IsNullOrEmpty(absolutePath))
                    baseCacheKey = baseCacheKey + "_#url:" + absolutePath + "|" + Context.Site.Name;


            if (rendering?.RenderingItem?.InnerItem?["VaryByUniqueID"] == "1")
                var uniqueId = rendering.UniqueId;
                if (uniqueId != Guid.Empty)
                    baseCacheKey = baseCacheKey + "_#uniqueid:" + uniqueId.ToString() + "|" + Context.Site.Name;

            return baseCacheKey;

Now to make this code work you will also add these two new caching options on the Sitecore Cache.

Please go to: /sitecore/templates/System/Layout/Sections/Caching/Caching

Add these two Items based on this template: /sitecore/templates/System/Templates/Template field

  • Item 1: Vary By Url, Type: Checkbox
  • Item 2: Vary By Unique Id, Type: Checkbox

That is it!

Open the Rendering you want to have it cached and go to the section called “Caching”. You should be able to see the new options there.

That is it and Do not forget to set the CACHEABLE option as well.

I hope that was helpful and feel free to leave any comments.


Leave a Comment

%d bloggers like this: