LibrarySites.Banner

Clear Output Caches for All Managed Sites after Publication with the Sitecore ASP.NET CMS

This blog post provides a prototype solution that clears the output caches for all of the sites managed by an instance of the Sitecore ASP.NET web Content Management System without the need to specify those sites individually.

Update 13.December.2012: For updates to the code provided in this blog post, see the last two blog posts linked at the end of this page.

The default implementation of the event handler (the Sitecore.Publishing.HtmlCache class) for the publish:end and publish:end:remote clears the output caches for the sites specified for the event handler. For example, with the default configuration:

<event name="publish:end">
  <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
    <sites hint="list">
      <site>website</site>
    </sites>
  </handler>
</event>
<event name="publish:end:remote">
  <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
    <sites hint="list">
      <site>website</site>
    </sites>
  </handler>
</event>

After publication, these event handlers clear the output cache for the managed site named website. If you add and rename sites, you need to remember to update these configurations. This could be frustrating and even error-prone if you have a number of Sitecore instances and add, delete, or rename managed sites frequently.

Instead, you might want to simply clear the output caches for all managed sites whenever you publish. You can achieve this objective with an override of this event handlers based on the following prototype:

namespace Sitecore.Sharedsource.Publishing
{
  using System;
  
  using SC = Sitecore;
  
  public class HtmlCacheClearer : SC.Publishing.HtmlCacheClearer
  {
    public void ClearCaches(object sender, EventArgs args)
    {
      SC.Diagnostics.Assert.ArgumentNotNull(sender, "sender");
      SC.Diagnostics.Assert.ArgumentNotNull(args, "args");
      string[] siteNames;
  
      if (this.Sites.Count > 0)
      {
        siteNames = (string[])this.Sites.ToArray();
      }
      else
      {
        siteNames = SC.Configuration.Factory.GetSiteNames();
      }
  
      SC.Diagnostics.Log.Info(
          this + " clearing HTML caches; " + siteNames.Length + " possible sites.",
          this);
  
      foreach (string siteName in siteNames)
      {
        SC.Diagnostics.Assert.IsNotNullOrEmpty(siteName, "siteName");
        SC.Sites.SiteContext site = SC.Configuration.Factory.GetSite(siteName);
        SC.Diagnostics.Assert.IsNotNull(site, "siteName: " + siteName);
  
        if (!site.CacheHtml)
        {
          continue;
        }
  
        SC.Caching.HtmlCache htmlCache = SC.Caching.CacheManager.GetHtmlCache(
          site);
        SC.Diagnostics.Assert.IsNotNull(htmlCache, "htmlCache for " + siteName);
  
        if (htmlCache.InnerCache.Count < 1)
        {
          SC.Diagnostics.Log.Info(
              this + " no entries in output cache for " + siteName,
              this);
          continue;
        }
  
        SC.Diagnostics.Log.Info(
            this + " clearing output cache for " + siteName,
            this);
          htmlCache.Clear();
      }
  
      SC.Diagnostics.Log.Info(this + " done.", this);
    }
  }
}

If you use the <sites> element within the <handler> element to specify sites, the ClearCaches() method clears the output caches for those sites. Otherwise, it clears the output caches for all sites that cache output and have entries in those caches. Note that the default implementation uses the method name ClearCache() and I named the method ClearCaches(). Therefore you can also use the ClearCache() method to get the default functionality.

You can use a Web.config include file (/App_Config/Include/Sitecore.Sharedsource.HtmlCacheClearer.config in my case) such as the following to enable this event handler for these events:

<configuration xmlns:patch="https://www.sitecore.com/xmlconfig/">
  <sitecore>
    <events>
      <event name="publish:end">
        <handler type="Sitecore.Sharedsource.Publishing.HtmlCacheClearer, Sitecore.Sharedsource"
                 patch:instead="handler[@type='Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel']">
          <patch:attribute name="method">ClearCaches</patch:attribute>
          <sites>
            <patch:delete />
          </sites>
        </handler>
      </event>
      <event name="publish:end:remote">
        <handler type="Sitecore.Sharedsource.Publishing.HtmlCacheClearer, Sitecore.Sharedsource"
                 patch:instead="handler[@type='Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel']">
          <patch:attribute name="method">ClearCaches</patch:attribute>
          <sites>
            <patch:delete />
          </sites>
        </handler>
      </event>
    </events>
  </sitecore>
</configuration>

This overrides the default handlers for these events (it could instead delete the defaults handlers and add new ones), updates the method attribute of the <handler> element from ClearCache to ClearCaches, and removes the <sites> elements from within those <handler> elements.

Resources