We have extended HtmlCacheClearer
in our solution. We are able to clear site cache using CacheManager.GetHtmlCache(site).Clear()
.
We wanted to clear individual Rendering/Component cache. How can we do that ?
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.
Sign up to join this communityWe have extended HtmlCacheClearer
in our solution. We are able to clear site cache using CacheManager.GetHtmlCache(site).Clear()
.
We wanted to clear individual Rendering/Component cache. How can we do that ?
In order to accomplish this, you will need to use the method HtmlCache.RemoveKeysContaining()
and pass in a string to use to find keys you want to be removed.
Here's an example cache clearer that works for us (tested with Sitecore 8.1 Update-3).
You should customize the ClearCacheForItem()
method to contain logic to parse the item that's been passed in and decide how you want to clear the cache for that item. This will depend on how you've set up your presentation caching (By Data, By Parameter, By Query String, etc.).
using System;
using System.Collections.Generic;
using System.Linq;
using Sitecore.Caching;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Events;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Events;
using Sitecore.Publishing;
using Sitecore.Sites;
namespace Custom.Web.Events.PublishEnd
{
public class CustomHtmlCacheClearer : HtmlCacheClearer
{
public new void ClearCache(object sender, EventArgs args)
{
Item rootItem = null;
bool deep = false;
var remoteEventArgs = args as PublishEndRemoteEventArgs;
if (remoteEventArgs != null)
{
var rootItemId = remoteEventArgs.RootItemId;
var rootItemDatabase = remoteEventArgs.TargetDatabaseName;
Database database;
if (Factory.GetDatabaseNames().Any(name => name.Equals(rootItemDatabase, StringComparison.OrdinalIgnoreCase)))
database = Factory.GetDatabase(rootItemDatabase);
else
database = Factory.GetDatabase("web");
if (database != null)
{
rootItem = database.GetItem(new ID(rootItemId));
}
deep = remoteEventArgs.Deep;
}
else
{
var publisher = Event.ExtractParameter(args, 0) as Publisher;
if (publisher != null)
{
rootItem = publisher.Options.RootItem;
if (rootItem != null)
{
deep = publisher.Options.Deep;
}
}
}
if (rootItem != null)
{
Assert.ArgumentNotNull(sender, nameof(sender));
Assert.ArgumentNotNull(args, nameof(args));
Log.Info($"[CustomHtmlCacheClearer] Selectively clearing HTML caches for all requested Sitecore sites (total: {this.Sites.Count}).", this);
IList<HtmlCache> caches = new List<HtmlCache>(this.Sites.Count);
foreach (object siteNameObj in this.Sites)
{
var siteName = siteNameObj as string;
if (siteName != null)
{
SiteContext site = Factory.GetSite(siteName);
if (site != null)
{
HtmlCache htmlCache = CacheManager.GetHtmlCache(site);
if (htmlCache != null)
{
caches.Add(htmlCache);
}
}
}
}
if (caches.Count > 0)
{
ClearCachesForItem(caches, rootItem, deep);
}
Log.Info($"[CustomHtmlCacheClearer] Done clearing HTML caches.", this);
}
}
protected void ClearCacheForItem(IEnumerable<HtmlCache> htmlCaches, Item item)
{
// ---------------------------------------------------------------------------------------
// This is the method you should customize to define how you want to clear the cache keys.
// Do you need to clear items including a rendering parameter? Just the item path?
// ---------------------------------------------------------------------------------------
Log.Info($"[CustomHtmlCacheClearer] removing HTML cached entries for item ({item.Paths.FullPath}, {item.ID})", this);
foreach (HtmlCache htmlCache in htmlCaches)
{
htmlCache.RemoveKeysContaining(item.Paths.FullPath);
}
}
}
}
If you have any custom site definitions that you're using to host web pages, make sure you add them to the <sites hint="list">
element.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<events>
<event name="publish:end">
<handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
<patch:delete />
</handler>
<handler type="Custom.Web.Events.PublishEnd.CustomHtmlCacheClearer, Custom.Web" 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="Custom.Web.Events.PublishEnd.CustomHtmlCacheClearer, Custom.Web" method="ClearCache">
<sites hint="list">
<site>website</site>
</sites>
</handler>
</event>
</events>
</sitecore>
</configuration>