Sitecore Partial HTML Cache

Created: 16 Sep 2021, last update: 30 Jan 2022

Sitecore Partial HTML Cache explained

In Sitecore 10.1 there is new Cache the Partial HTML cache see: https://doc.sitecore.com/en/developers/101/sitecore-experience-manager/configure-html-caching.html

The Partial HTML cache is similar to the HTML Cache it caches the html output from a rendering. The difference is the way the cache is flushed the HTML cache is flushed after a publish. The partial HTML cache flush is triggered by an item change. And the flush do only flush the keys that or depend on the item, configured by the content dependencies from the rendering.

Note: you need to enable the PartialHtmlCacheClear of your website, by default it is off.

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <sites>
      <site name="website">
      	<patch:attribute name="enablePartialHtmlCacheClear">true</patch:attribute>
      </site>
    </sites>
  </sitecore>
</configuration>

Tools:

With the build in Sitecore admin tool /sitecore/admin/cache.aspx you can see All caches and the size, you can use it for tuning the cache size and to flush all caches.

Caching manager

https://github.com/markstiles/SitecoreCachingManager

Sitecore Caching manager is recently updated to work With Sitecore 10.1 with this tool you can easily see the cache keys. Also possible to search for keys and also to delete the keys from the search result. When developing or figuring out, more targeted clearing rather than clearing all caches can be very helpful, including understanding what's stored in the cache.

Sitecore Rocks

When working with Visual Studio, Sitecore Rocks have also a Cache tool, with this tool you can also see al cache keys and filter. No option to clear.

How it works

The Partial Html cache uses the same cache keys as the normal Html cache, this key depends on the selected Caching options on the rendering like var By Device. See the pictures above for examples.

Flush, on then items event item:deleted and item:saved there is the <handler type="Sitecore.Publishing.PartialHtmlCacheClearer, Sitecore.Kernel" method="OnItemEvent" resolve="true" /> This is used to flush the cache for the item and dependencies. Unfortunately, the logic around the flush is not public. Also, you can not use this pipeline processor in another pipeline because it needs as input an item and therefore the code is specific to certain pipelines. It is checking the pipeline name so try to use in another does not work.

If you want to trigger the flush you can raise an item:save event like I did in Flush HTML cache on Sitecore Forms Submit

The partial Html cache is accessible as a standard cache with string key, ICache<string>

Sample code:

//get the partial html cache from site with name website
var iCachePartialHtml = Sitecore.Caching.CacheManager.FindCacheByName<string>("website[partial html]");

// get all cache keys
iCachePartialHtml.GetCacheKeys();

//delete 1 cache record with specific name 
iCachePartialHtml.Remove("controller::a, b#c_#lang:NL-NL_#data:sitecore://web/{ADABE907-437B-40F1-843F-4FC61923025F}?lang=nl-NL&ver=1");