Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Last active September 26, 2019 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KevinJump/6721e7964516e439dc798cc01bd78815 to your computer and use it in GitHub Desktop.
Save KevinJump/6721e7964516e439dc798cc01bd78815 to your computer and use it in GitHub Desktop.
uSync 8.2 Cache rebuild
using System.Linq;
using Umbraco.Core.Composing;
using Umbraco.Core.Logging;
using Umbraco.Core.Services.Changes;
using Umbraco.Web.Cache;
using Umbraco.Web.PublishedCache;
using uSync8.BackOffice;
namespace uSync8.EventTriggers
{
/// <summary>
/// Composer to register the events.
/// </summary>
[ComposeBefore(typeof(uSyncBackOfficeComposer))]
public class EventTriggersComposer : ComponentComposer<EventTriggersComponent> { }
/// <summary>
/// Component, will trigger a cache rebuild when an import is completed. (and there are changes)
/// </summary>
public class EventTriggersComponent : IComponent
{
private readonly IPublishedSnapshotService snapshotService;
public EventTriggersComponent(IPublishedSnapshotService snapshotService)
{
this.snapshotService = snapshotService;
}
public void Initialize()
{
uSyncService.ImportComplete += BulkEventComplete;
}
private void BulkEventComplete(uSyncBulkEventArgs e)
{
if (e.Actions.Any(x => x.Change > uSync8.Core.ChangeType.NoChange))
{
// change happened. - rebuild
snapshotService.Rebuild();
// then refresh the cache :
// there is a function for this but it is internal, so we have extracted bits.
// mimics => DistributedCache.RefreshAllPublishedSnapshot
RefreshContentCache(Umbraco.Web.Composing.Current.DistributedCache);
RefreshMediaCache(Umbraco.Web.Composing.Current.DistributedCache);
RefreshAllDomainCache(Umbraco.Web.Composing.Current.DistributedCache);
}
}
private void RefreshContentCache(DistributedCache dc) {
var payloads = new[] { new ContentCacheRefresher.JsonPayload(0, TreeChangeTypes.RefreshAll) };
Umbraco.Web.Composing.Current.DistributedCache.RefreshByPayload(ContentCacheRefresher.UniqueId, payloads);
}
private void RefreshMediaCache(DistributedCache dc) {
var payloads = new[] { new MediaCacheRefresher.JsonPayload(0, TreeChangeTypes.RefreshAll) };
dc.RefreshByPayload(MediaCacheRefresher.UniqueId, payloads);
}
public void RefreshAllDomainCache(DistributedCache dc)
{
var payloads = new[] { new DomainCacheRefresher.JsonPayload(0, DomainChangeTypes.RefreshAll) };
dc.RefreshByPayload(DomainCacheRefresher.UniqueId, payloads);
}
public void Terminate()
{
// end
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment