Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
namespace Discovering.Sitecore10.Processors.PublishEnd.Services
{
public class RenderingHostBatchingCacheClearer
{
private readonly HttpClient _httpClient;
private readonly object _lock = new object();
private readonly Timer _timer;
private readonly HashSet<string> _endpoints = new HashSet<string>();
public RenderingHostBatchingCacheClearer(TimeSpan batchingTimeout, HttpClient httpClient)
{
_httpClient = httpClient;
_timer = new Timer(batchingTimeout.TotalMilliseconds)
{
AutoReset = false,
Enabled = false
};
_timer.Elapsed += (s, a) => { TriggerCacheClear(); };
}
public void AddEndpoints(IEnumerable<string> endpoints)
{
if (endpoints == null || !endpoints.Any())
return;
lock (_lock)
foreach (var endpoint in endpoints)
_endpoints.Add(endpoint);
_timer.Stop();
_timer.Start();
}
public int GetEndPointsCount()
{
lock (_lock)
return _endpoints.Count;
}
private async void TriggerCacheClear()
{
string[] endpoints;
lock (_lock)
{
if (_endpoints.Count == 0)
return;
endpoints = _endpoints.ToArray();
}
foreach (var endpoint in endpoints)
{
try
{
var response = await _httpClient.GetAsync(endpoint);
if (!response.IsSuccessStatusCode)
continue;
}
catch (Exception e)
{
Log.Error(e.Message, e, this);
}
finally
{
// remove items from the list if calling the endpoint returned with successful status or exception happened
lock (_lock)
_endpoints.Remove(endpoint);
}
}
}
}
}
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.