Caching: Vary by data – How does it work?
Sitecore providers different caching options for the renderings to cache the HTML. One of them is the Vary by Data. Most of the developers consider that when we enable this option for caching, the HTML of the rendered rendering is cached by the data source item and some other factors which generate the cache key.
While working on one of the projects, I came across a requirement where I’ve to cache the HTML for the rendering which has no data source option set. I was also one of those developers who considered that the data source item is required for the Vary by Data option to work.
Then I decided to refactor the Sitecore code for digging inside to learn who Sitecore caching works for the renderings.
After looking at the code I realized that there is something that my understanding is incomplete. Then I built a sample application to clear my understanding that how Vary by Data actually works.
And following are my findings that I’m sharing with you all. For the purpose of clearing understandings following is the scenario I prepared.
I created two renderings named Header and Heading. The Header component has some text and one placeholder to hold the Heading component. The Heading component is set up for caching with Vary by Data option.
In the first iteration, I set up both the component without the data source item and the cache key generated for the Heading component is as shown in the following screenshot.
As seen in the above image, the cache key has the ID of the context page, which is the page being rendered. So, if we do not set any data source item, Sitecore generates the cache key which includes the context page ID.
In the second iteration, I set the data source item for the Header component and the output was surprising as shown in the following image.
While you see the above image carefully, the Context item (Sitecore.Mvc.Presentation.RenderingContext.CurrentOrNull.ContextItem) for the Heading component is the data source item of the parent rendering instead of the context page as we saw in the first iteration. Also, the cache key contains the ID of the context item which is nothing but the data source item of the parent rendering.
In the second iteration, I set the data source item for the Heading component while keeping the data source item set for the Header component. Now, the key generated is as shown in the following image.
Now the cache key generated contains the ID of the data source item of the Heading rendering itself.
So, based on my findings following are the scenario based on which the cache key is generated for the Vary by Data option.
- If the data source of the rendering is set, then Sitecore uses the ID of that data source item to generate the cache key.
- If the data source of the rendering is not set and the data source of the parent rendering is set, then Sitecore uses the ID of the data source item of the parent rendering to generate the key.
- If data source is not set for the rendering and parent rendering(s), then Sitecore uses the ID of the context item to genrate the cache key.
Overall, I can say that we can enable the rendering cache option with Vary by Data even the data source item for the rendering is not set. However, we have to see and check the implications after enabling the cache option with Vary by Data for our requirements.
I hope my findings will help to clear the understanding about the rendering cache option Vary by Data.