9

If I can set a rendering cache to Vary By Data, meaning the cache will vary for a given component with a different data source set, why am I not just setting every component to be Cacheable with Vary By Data?

13

Vary By options each serve different use cases, and are not always applicable when caching a rendering.

If a component doesn't have a data source then Vary By Data will cache its output by the Context Item. This would cause a cached instance of this component for every page.

If a component outputs the same markup everywhere, then you don't want to apply any Vary By to the cache key, which will cause its output to be cached globally for the Context Language.

Multiple cache options are available out of the box for various purposes, and it's good to understand how the cache keys are generated so each component can be cached in the most efficient way:

Vary by Data

The cache key is: controller::[Controller]#[Controller Action]#lang:[Language Culture Code]#data:[Path of the Datasource Item]

Since, the path to each data source item is different, the output cache varies by change in Datasource.

Vary by Device

The cache key is: controller::[Controller]#[Controller Action]#lang:[Language Culture Code]#dev:Responsive

We can output different cache keys for different Sitecore Devices.

Vary by Login

controller::[Controller]#[Controller Action]#lang:[Language Culture Code]#ogin:False

We can output different cache keys based on if user is logged in or not.

Vary by Parameters

The cache key is: controller::[Controller]#[Controller Action]#lang:[Language Culture Code]#parm:[Key=Value]

We can output different cache for the same rendering based on the different rendering parameters.

Vary by Query String

The cache key is: controller::[Controller]#[Controller Action]#lang:[Language Culture Code]#qs:[request.QueryString]

Vary by User

The cache key is: controller::[Controller]#[Controller Action]#lang:[Language Culture Code]#user:extranet\Anonymous

3

That is a very good question :) If you are not using the caching values on your rendering, you are missing out on some awesome performance gains. You would want to make careful use of the vary by settings to make sure that your component is cached properly.

For example, sure your rendering may use datasources, but it might also vary the content based on the logged in status of the user, or based on the user uniquely - so you could add in vary by user or vary by login.

You have to be aware of how this would affect personalization and MV testing too. On a component with rendering rules, the Vary by data will not work as expected. Fortunately with MVC implementations, Sitecore have made it nice and easy to add your own vary by options in. Its all to do with how the cache key is generated, which is done in the appropriately named GenerateCacheKey class in the RenderRendering pipeline.

I think the main reason that I see why devs don't add caching options is that they don't fully understand how the vary by options work. It can be a lot of effort to calculate exactly how the options need to be set and that it can cause bugs when they are not set correctly. But its definitely worth putting the effort in for the performance gains you will get.

References:

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.