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 .
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 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.
publish:end:remote
event is for.
Commented
Nov 23, 2020 at 18:15
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>
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.
Commented
Jul 24, 2017 at 19:09
In Sitecore 10.1 a new rendering parameter is added under the caching section. Setting the Cache Clearing Behavior field to Clear on publish would clear the rendering cache on publish of the item. PFB the screenshot