Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
namespace Discovering.Sitecore10.Processors.PublishEnd
{
public class Tenant
{
public Tenant(string startPath, IEnumerable<string> endpoints)
{
this.StartPath = startPath;
this.Endpoints = endpoints;
}
public string StartPath { get; set; }
public IEnumerable<string> Endpoints { get; set; }
}
public class TriggerRenderingHostCacheClear
{
private readonly RenderingHostBatchingCacheClearer _batchedSender;
private const string TenantsConfigNodePath = "RenderingHostCacheClear.Tenants/Tenant";
private readonly IEnumerable<Tenant> _tenants;
public TriggerRenderingHostCacheClear()
{
// NOTE: use DI for HttpClient, though the event is singleton
_batchedSender = new RenderingHostBatchingCacheClearer(TimeSpan.FromSeconds(3), new HttpClient());
_tenants = this.GetTenants();
}
public void OnItemPublished(object sender, EventArgs args)
{
var itemProcessedEventArgs = args as ItemProcessedEventArgs;
var context = itemProcessedEventArgs?.Context;
if ((context.PublishContext?.DeleteCandidates?.Count ?? 0) > 0 && _batchedSender.GetEndPointsCount() <= 0)
{
_batchedSender.AddEndpoints(
this.GetTenantByItemPath(context.PublishOptions.RootItem.Paths.FullPath)?.Endpoints);
}
if (context.Result.Operation == PublishOperation.None || context.Result.Operation == PublishOperation.Skipped)
return;
_batchedSender.AddEndpoints(this.GetTenantByItemPath(context.VersionToPublish.Paths.FullPath)?.Endpoints);
}
private IEnumerable<Tenant> GetTenants()
{
return Factory
.GetConfigNodes(TenantsConfigNodePath)
?.Cast<XmlNode>()
?.Select(x => new Tenant(
XmlUtil.GetAttribute("startPath", x),
x.ChildNodes.Cast<XmlNode>().Select(e => e.InnerText)));
}
private Tenant GetTenantByItemPath(string itemPath)
{
return _tenants?.FirstOrDefault(t =>
itemPath.Equals(t.StartPath, StringComparison.CurrentCultureIgnoreCase)
|| itemPath.StartsWith(t.StartPath + "/", StringComparison.CurrentCultureIgnoreCase));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.