5

Hi On Save event i am publishing the changes to another database (another environment). Please refer Below link AutoPublishing doesn't work on Rename & Copy

How do i clear those published items from the cache in the destination .

  • If you go to the web database after the autopublishing event happens is the item updated there or does that still have the old values? – Teeknow Jun 27 '17 at 13:47
  • 2
    Which cache are you trying to clear, specifically? – Zachary Kniebel Jun 27 '17 at 14:30
6

If you are referring to HTML Cache, by default, Sitecore includes cache-clearing event handlers that clear the HTML cache when the publish:end and publish:end:remote events are raised. The handler is typed to the Sitecore.Publishing.HtmlCacheClearer class and calls the ClearCache method.

You can find the subscriptions for these events in your ShowConfig (/sitecore/admin/showconfig.aspx) by searching for publish:end and publish:end:remote.

<event name="publish:end" patch:source="Sitecore.Social.config">
  <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
    <sites hint="list">      <!-- list of sites to clear the cache for-->
      <site>website</site>
    </sites>
  </handler>
  ...
</event>
<event name="publish:end:remote" patch:source="Sitecore.ContentSearch.config">
  <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
    <sites hint="list">      <!-- list of sites to clear the cache for-->
      <site>website</site> 
    </sites>
  </handler>
  ...
</event>

Note that if you set a custom name for your site (i.e. a name other than "website") or if you have multiple sites, then you will need to make sure that these sites are included in the list of sites to be cleared.

  • 1
    Do we need this for both CM and CD servers? – Tama May 30 '19 at 14:26
0

As Zachary already mentioned, you can use the Sitecore OOTB HTML Cache agent to clear the cache. As quoted below, you mentioned

clear those published items from the cache in the destination

Using the default Sitecore HTML Cache Clearance agent, will clear all the cache for all the sites presents between the <sites> tag. I think that you can clear the cache specific to the site.

For example, if you have published an item which is present in Site A, you'll want to clear the cache for Site A instead of all sites.

You can accomplish this by implementing a handler which clears the cache for a specific site based on the item. Below is a sample code. Note that I haven't yet tested the code.

public class PublishingOnSaved
{
    public string SiteName { get; set; }

    public void OnSavedItem(object sender, EventArgs args)
    {
        var currentItem = Event.ExtractParameter(args, 0) as Item;

        if (currentItem == null)
        {
            return;
        }

        this.SiteName = GetSiteFromItem(currentItem);

        // Codes for publishing is here
    }

    public void OnSiteCacheClearance(object sender, EventArgs args)
    {
        var caches = CacheManager.GetAllCaches();

        foreach (var cache in caches)
        {
            if (cache.Name.ToLower().Contains(this.SiteName))
            {
                cache.Clear();
            }
        }
    }

    private string GetSiteFromItem(Item item)
    {
        // Add logic to get site from item. 
    }
}

Then add your handler to the publish:end and publish:end:remote.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <events>
      <event name="publish:end">
        <handler type="YourNamespaceWithClass, YourAssembly" method="OnSiteCacheClearance">
      </event>
      <event name="publish:end:remote">
        <handler type="YourNamespaceWithClass, YourAssembly" method="OnSiteCacheClearance">
      </event>
    </events>
  </sitecore>
</configuration>
  • 1
    +1 Hishaam makes a very good point, here: when the native HtmlCacheClearer goes in, it clears the cache for all of the sites listed. If you want to just clear the cache for the specific site that items were published for then you will need something custom, like Hishaam's solution, in order to do this. Remember that based on your business requirements, this solution may not be an option, e.g. if you need to render data from an item in one site on a page of another site. However, there are ways to custom code for those scenarios, as well. – Zachary Kniebel Jul 24 '17 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.