Saturday, October 15, 2016

#Sitecore Cache Clear Event for Multisite Solutions


Hi All,
In one of our project we have requirement for apply cache in our site. In our #Sitecore solution we have multiple Site-Nodes. Like –
Sitecore
                -content
                                -site-node1
                                                -home
                                -site-node2
                                                -home

In our sitedefinition.config I have 2 sites with the name –
      1)      Site1
      2)      Site2

I have applied cache on item’s Templates presentation. Now I need to clear cache.

To clear cache I have created a patch config. I used following event –

<sitecore>
    <events>
      <event name="publish:end">
        <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
            <sites hint="list">
            <site>Site1</site>
            <site>Site2</site>
          </sites>
        </handler>
      </event>
      <event name="publish:end:remote">
        <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
          <sites hint="list">
            <patch:delete />
          </sites>
          <sites hint="list">
            <site>Site1</site>
            <site>Site2</site>
          </sites>
        </handler>
      </event>
    </events>
  </sitecore>

This event clears the cache. But after some testing I found that after publishing only cache is clearing for Site2 not for Site1. To verify this, I opened the url http://hostname/sitecore/admin/showconfig.aspx and search for “publish:end” term, found the entry of Site2 only in cache clear event. Now this is the problem. Need to find a solution for this.

We need to open url http://hostname/sitecore/admin/cache.aspx every time to clear the cache, but that is not good practice. So after some digging on the google to find the solution. But finally I did it.
We need to add one more sites section in the same patch config file.
<sites hint="list">
    <patch:delete />
 </sites>

Following is the solution-

<sitecore>
    <events>
      <event name="publish:end">
        <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
          <sites hint="list">
            <patch:delete />
          </sites>
          <sites hint="list">
            <site>Site1</site>
            <site>Site2</site>
          </sites>

        </handler>
      </event>
      <event name="publish:end:remote">
        <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
          <sites hint="list">
            <patch:delete />
          </sites>
          <sites hint="list">
            <site>Site1</site>
            <site>Site2</site>
          </sites>
        </handler>
      </event>
    </events>
  </sitecore>

After adding this sites section, I again opened the url http://hostname/sitecore/admin/showconfig.aspx and search for publish:end term and look what I saw, I could see both sites entries (Site1 and Site2). But I needed to verify this by publishing an item as well and it worked finally for both the sites.

Happy Coding :)