This post has been migrated from www.experimentsincode.com, we apologise if some of the images or content is missing

This post has been migrated, original date 10 Nov 2009 After editing content and publishing it the HTML cache is cleared so that the changes can be seen. The clearing of the cache is performed by the HtmlCacheClearer class that is configured as a handler on the publish end event:
<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">         <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">          <sites hint="list">             <site>website</site>           </sites>         </handler>       </event>
So what does this mean? Well in Sitecore 6 the links to items are managed differently; in early versions if you added a link in the Rich Text editor it would enter the URL to the item. This mean if you later moved an item in the content tree any links to it would break. However in Sitecore 6 it inserts a specially formatted link that contains the Guid of the item you want to link to, then when the item is rendered the special link is replaced with the actual link to the item. To speed up the replacing of the special link with the actual item URL Sitecore creates a cache of the item Guid's and URL;s called the PathCache. However when you publish this cache is not cleared, this means that if you move any items in the content tree and then publish any links to that item will break because the page will render the cached paths and not the new paths. This will happen until the application pool is restarted. The solution is to create our own cache clearing method that clears both the path and html caches:
    public class CacheClearer : Sitecore.Publishing.HtmlCacheClearer
    {
        new public void ClearCache(object sender, EventArgs args)
        {

            Sites.Cast().ToList().ForEach(x =>
            {
                Sitecore.Sites.SiteContext site = Sitecore.Configuration.Factory.GetSite(x);
                if (site != null)
                {
                    Sitecore.Caching.PathCache cache = Sitecore.Caching.CacheManager.GetPathCache(site.Database);
                    if(cache != null)
                        cache.Clear();

                }
            });

            base.ClearCache(sender, args);
        }

    }
Then alter the web.config to use our new class instead of Sitecore's:
      <event name="publish:end">
        <handler type="Test.CacheClearer, Test" method="ClearCache">
          <sites hint="list">
            <site>website</site>
          </sites>
        </handler>
      </event>
I hope this helps someone because this seems to be an oversight on Sitecore's part.
comments powered by Disqus