A smarter Sitecore HTML Cache Clearer

Mark Gibbons
3 min readApr 22, 2020

Update: Sitecore 10.1+ has finally shipped with a superior cache clearing mechanism called “Clear On Content Update”, so this article is now mostly obsolete and only relevant if you are stuck on an old Sitecore version. However the new “Clear On Content Update” does require some additional configuration. Documentation is here.

Essentially my dream wishlist section at the end of this article got implemented into XM / XP. I’d like to think that this blog post might have prompted the XM product team to revisit this.

Sitecore 9.2 and 9.3 includes a “smart” HTML cache clearer. Let’s take a look at what makes it “smart”.

From the HTML caching documentation:

Sitecore clears caches for all sites you configure with the value of the cacheHtml property as true. If you do not want the cache for a site to be cleared when you publish you can add the preventHtmlCacheClear attribute to the site definition.

So the main change they’ve made here is that we no longer need to patch into the publish:end event for every site we define. Does that make this HTML cache clearer “smart”?

I think a smart cache clearer (of any kind — not just HTML) should at the very least try to detect which site(s) are affected by the publish and only clear affected site caches.

This is not a new concept and is in this blog post from 8 years ago by John West, another post from Mark Stiles 9 years ago, and has also been blogged again nearly 6 years ago. So it is somewhat surprising that this has not made it into the product especially when there has been some recent work done in this area.

Let’s make a better Smart HTML cache clearer

I’m essentially updating that original blog post here by incorporating the changes in Sitecore 9.2 and 9.3.

Some notes:

  • The config is for Sitecore 9.2, if you are on 9.3 it just needs a tweak to change the reference of Sitecore.Publishing.HtmlCacheClearer to Sitecore.Publishing.SmartHtmlCacheClearer. This is because in 9.2 it is still using the old handler, even though the code for the new one is there.
  • The code also handles RenderingParametersCacheClearer as it is trivial to also clear the site specific Rendering Parameters Cache at the same time.
  • The config also patches out the SXA multisite HTML cache clearer as this supersedes the need for it.

The perfect Sitecore Cache Clearer

If we could dream up what the perfect Sitecore cache clearer would look like, I think it should start from the cache key strategy. Every cache in the system should have a smart cache key strategy that contains everything we need to be able to selectively clear specific cache entries from the cache.

It would be no doubt a non-trivial challenge to find such a cache key strategy. For example we might have a change in page item {abc} for site example. The cache key could have example-{abc} which then allows us to easily clear that entry if that single item gets published. This gets complicated when we take into account datasource items, rendering parameters, composite renderings and so on. So I might really just be dreaming that this could be possible — but where there is a will, there is a way.

What about the Item Cache? This seems to be also currently cleared for all sites on publish. It looks like this logic is deep in the Sitecore kernel so I don’t see anyway to optimize this — but I hope this can be revisited.

--

--

Mark Gibbons

Technical Architect @ Aceik | Sitecore Technology MVP 2020 - 2024 with a love for all things #Sitecore / Twitter twitter.com/markgibbons25