Skip to content

Instantly share code, notes, and snippets.

@robearlam
Created April 15, 2017 03:26
Show Gist options
  • Save robearlam/6721dcee5252ccdb8abd7edf819f6395 to your computer and use it in GitHub Desktop.
Save robearlam/6721dcee5252ccdb8abd7edf819f6395 to your computer and use it in GitHub Desktop.
Customsing the Sitecore HTML cache clearer
<settings>
<!--
A pipe separated list of content ID's. When a publish occurs if the item
being published is one of these items,
or a child of one of these items then the HTML cache isn't cleared.
-->
<setting name="CustomHtmlCacheClearer.IgnoreItems" value="/sitecore/content"/>
</settings>
<events>
<event name="publish:end">
<handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
<patch:delete />
</handler>
<handler type="CustomHtmlCacheClearer.HtmlCacheClearer, CustomHtmlCacheClearer" 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">
<patch:delete />
</handler>
<handler type="CustomHtmlCacheClearer.HtmlCacheClearer, CustomHtmlCacheClearer" method="ClearCache">
<sites hint="list">
<site>website</site>
</sites>
</handler>
</event>
<event name="indexing:end">
<handler type="CustomHtmlCacheClearer.IndexCacheClearer, CustomHtmlCacheClearer" method="Clear"/>
</event>
</events>
public class HtmlCacheClearer : Sitecore.Publishing.HtmlCacheClearer
{
private const string SettingName = "CustomHtmlCacheClearer.IgnoreItems";
public new void ClearCache(object sender, EventArgs args)
{
if (sender == null)
{
return;
}
var sitecoreEventArgs = args as SitecoreEventArgs;
if (sitecoreEventArgs == null || !sitecoreEventArgs.Parameters.Any())
{
return;
}
var publisher = sitecoreEventArgs.Parameters[0] as Publisher;
if (publisher == null)
{
return;
}
if (IsCacheClearRequired(publisher.Options))
{
base.ClearCache(sender, args);
}
}
protected bool IsCacheClearRequired(PublishOptions publishOptions)
{
var exemptPaths = Sitecore.Configuration.Settings.GetSetting(SettingName);
if (string.IsNullOrWhiteSpace(exemptPaths))
{
return true;
}
return exemptPaths.Split('|')
.All(excemptPath => !publishOptions.RootItem.Paths.FullPath.StartsWith(excemptPath,StringComparison.OrdinalIgnoreCase));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment