Tekcent
Get in touch
Engineering

Optimize Sitecore Performance with Cache Clearing On Content Update

June 14, 2023

Sitecore 10.1's selective cache-clearing invalidates only the HTML affected by a content change, instead of the whole cache.

Careful control of the cache behavior can efficiently reduce the load on the server to improve the user experience.

Sitecore is a powerful platform for building websites that deliver personalized and relevant experiences to visitors. However, to achieve optimal performance, Sitecore developers need to configure HTML caching properly for their renderings and components. HTML caching is an output cache that caches renderings (not whole pages). It helps reduce the server load and improve the response time by serving cached content instead of executing code or querying databases every time a page is requested.

Nevertheless, HTML caching also comes with some challenges. One of them is how to clear the cache when content changes. In previous versions of Sitecore prior to 10.1, the behaviour was to clear the entire HTML cache on every publish event, regardless of whether the publishing content is associated to the rendering or not, which could cause performance issues and unnecessary overhead on first loading of the page.

Using the Clear On Content Update option

Configure your site with both the cacheHtml property and the enablePartialHtmlCacheClear property set to true.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <sites>
      <site name="your_website">
        <patch:attribute name="cacheHtml">true</patch:attribute>
        <patch:attribute name="enablePartialHtmlCacheClear">true</patch:attribute>
      </site>
    </sites>
  </sitecore>
</configuration>

From Sitecore rendering definition item (globally) or from rendering detail on presentation detail of a page item (locally), add caching options to the renderings Sitecore caches output for.

Sitecore will generate a cache key based on selected caching options. The way Sitecore 10.1 generates the cache key is the same as previous Sitecore versions:

The "Cacheable" option is required if you want to enable cache for your rendering, the cache key is: {Renderer Cache Key}_#lang:{Language Culture Code}[_#area:{MVC area}]

  • If "Vary By Data" is selected then append: _#data:{Path of the Datasource Item}
  • If "Vary By Device" is selected then append: _#dev:{Device Name}
  • If "Vary By Login" is selected then append: _#login:{True|False}
  • If "Vary By User" is selected then append: _#user:{Username}
  • If "Vary By Parm" is selected then append: _#parm:{Rendering Parameters}
  • If "Vary By Query String" is selected then append: _#qs:{Request Query String}
  • If "Clear on Index Update" is selected then append: _#index

Set "Cache Clearing Behavior" to "Clear on content update" (default value is "Clear on publish" which acts the same as previous Sitecore versions that clear cache on every publish event).

Configure rendering content dependencies — you can select zero, one or more options:

  • Ancestors — The rendering uses ancestors of the data source item or items.
  • Parent — The rendering uses the parent of the data source item or items.
  • Siblings — The rendering uses siblings of the data source item or items.
  • Related Items — The rendering uses related items of the data source item or items.
  • Recursive Related Items — When you use this option with other options (except Related Items), it indicates that related items of that option's items are also used by the rendering. This option is designed specifically to work with other lineage options.
  • Children — The rendering uses direct children of the data source item or items.
  • Descendants — The rendering uses descendants of the data source item or items.

Refer to the "Using the Clear On Content Update option → Example scenario" section of the Sitecore documentation for a better understanding of these options by example.

Once you have followed the steps you have enabled cache clearing on content update for your renderings, which is much more effective than ever before!

How does it work

The below diagram shows how Sitecore processes rendering a component.

When you enable the feature "Clear on content update" on your rendering, the system runs the N1 block. This block collects all the content dependencies of the data source items of the rendering, based on the options you select on the "Content Dependencies" above.

Then, the system runs the N2 block. This block creates a map that links each content dependency to the corresponding rendering cache key.

Sitecore has added a new event handler to:

  • item:saved
  • item:saved:remote
  • item:deleted
  • item:deleted:remote
<handler type="Sitecore.Publishing.PartialHtmlCacheClearer, Sitecore.Kernel" method="OnItemEvent" resolve="true" />

Final thoughts

Now, when content changes (updated or deleted either locally or in a remote database) Sitecore looks at the map and clears all related cache entries. In the example, once the Sitecore item with ID "item_id_01" is changed, Sitecore clears the cache entry of the matching key "cache_key".

This feature is a great improvement since Sitecore 10.1 as it allows developers to control the cache behavior more easily and efficiently.

It also helps to reduce the load on the server and improve the user experience. We recommend using this feature whenever possible and testing it thoroughly before deploying it to a Production environment.

Insights

Ready for change?

If the time is now for you to transform the way you do business, we’re here to help — every step of the way.

Get in touch