Getting Started with Sitecore's HTML Caching
Part 2 of my 3-Part Series on Sitecore Caching

In my last post, The Underworld of Sitecore Caching (Part 1), I went over each of the different Sitecore Caches, what they do and how they work. In this post, I will be going over HTML Caching, its use and the various OOTB options that you have for controlling it in your solution.

Making a Rendering Cacheable

For a rendering to be cacheable, it's "Cacheable" checkbox field must be checked. There are two locations where this checkbox can be set: on the rendering item, itself; and in the Control Properties dialog, in Presentation Details.

The location where a rendering is set to be cacheable determines (what I refer to as) the scope of the rendering's cacheability. I use the terms Global and Instance to describe the scope of a rendering's cacheablility:

  • Global: checked on the rendering item, itself
    • Sets the rendering to always be cached, regardless of where it's used
    • Use with caution when configuring cacheability settings for reusable renderings
  • Instance: checked in the rendering's Control Properties dialog
    • Sets the instance of the rendering to be cacheable
    • Recommended for renderings that are reused often
    • Does nothing if rendering is globally cacheable
    • Supports versioned layouts, in Sitecore 8:
      • Set for particular item versions via Final Layout
      • Set for all versions via Shared Layout
    • Can be set in the Presentation Details of an template's Standard Values (also supports versioned layouts, in SXP 8)

Screenshot of Sitecore HTML caching options on rendering item/in control propertiesScreenshot of Sitecore's Control Properties Dialog, accessed via Presentation Details

Using the Right Caching Options for your Rendering

If the only option was to make a rendering cacheable or not cacheable, the power and usability of this feature would be extremely limited. If this were the case then you wouldn't be able to cache any of your renderings that display content based on any of the following:

  • ContentSearch results
  • An assigned datasource
  • The device being viewed (if using device detection)
  • Whether or not a user has logged in
  • Rendering parameters
  • URL query string parameters
  • Data from the current user

Fortunately, Sitecore provides seven caching options that enable the use of HTML Caching for all of the above scenarios, and can be combined to account for even more complex scenarios. To use these options, simply check the corresponding box on the rendering (Global or Instance), under the "Cacheable" checkbox. The table below describes the various options that come with Sitecore OOTB, and when to use them:

Option When Enabled, ... Best Used When...
Clear on Index Update The rendering will be cleared from the HTML Cache when an item that is associated with the rendering is updated in the index. Renderings are cleared from the cache by the IndexDependentHtmlCacheManager which is triggered on the index:end and index:end:remote events in the Sitecore.ContentSearch.config patch file. Your rendering contains code that depends on an index, e.g. components that display details of an item retrieved via ContentSearch
Vary by Data Sitecore will cache the rendering's output once for each data source, including no data source. Your rendering's ouput changes based on its datasource item
Vary by Device Sitecore will cache the rendering's output once for each context device by the device's name. Your rendering's output changes based on the context device, e.g. desktop vs print vs mobile
Vary by Login Sitecore will cache the rendering's output once for when there is an authenticated visitor, and once for when there is not an authenticated visitor (anonymous users count as authenticated users) Your rendering's output changes based on whether or not the user has authenticated, e.g. rendering shows either "login" link if user is logged in, or else displays the "logout" link
Vary by Parameters Sitecore will cache the rendering's output once for each set of rendering parameters passed to the component. Your rendering's output changes based on passed rendering parameters, e.g. rendering includes support for a rendering parameter that controls text-alignment
Vary by Query String Sitecore will cache the rendering's output once for each set of parameters passed in the URL query string Your rendering's output changes based on supplied query string parameters, e.g. including a count query string parameter (i.e. controls the number of items that the rendering will display
Vary by User Sitecore will cache the rendering's output once for each context user, based on the user's domain and username Your rendering's output changes based on user-specific data and the number of active users between publishing operations is relatively small (to avoid excess memory consumption, only use this in solutions with a relatively small number of users)

As previously stated, remember that these options can be combined to satisfy more complex scenarios, as well.

UP NEXT: (Part 3) Controlling Sitecore HTML Caching Variance with the Rules Engine →

References and Additional Info

Post a Comment

  • Comment by knorock on Apr 20, 2021 - kamagra