Kyle Kingsbury A Sitecore web development blog

Pattern for implementing a custom HtmlCacheClearer


While there are many blog posts detailing how to implement a custom HtmlCacheClearer, some of these blog posts omit the fact that the out of the box HtmlCacheClearer can be used with multiple type of EventArgs. The reason for multiple types of EventArgs is due to the separation of how the cache is being cleared through the event manager, either from a local event or from a remote event. Note that generally local events are executed on the machine that initiated the action, whereas, a remote event will be executed on the machine that is listening to these events – rule of thumb, your CM server listens to local events and your CD server will listen to the remote events. You typically only care about accessing the EventArgs when you need to know more about what parameters were used when clearing the cache, such as the Target database, the language, the root item, etc.

With this post, I want to provide a base implementation of the HtmlCacheClearer that provides a clean separation for processing local and remote events.

The base implementation exposes separate methods for clearing local and remote events. To implement a custom HtmlCacheClearer we simply need to create a derived type of BaseHtmlCacheClearer and set the generic types we are targeting. For instance, we could do the following:

As a bonus, here is another example of an implementation using the BaseHtmlCacheClearer that pulls parameters from both EventArgs and centralizes the logic for executing your custom code:

Hope you enjoyed this post!

Add comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

By Kyle Kingsbury
Kyle Kingsbury A Sitecore web development blog
Sitecore MVP 2019 - Technology

Recent Posts