Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. using System.Linq;
  2. using Umbraco.Core.Composing;
  3. using Umbraco.Core.Logging;
  4. using Umbraco.Core.Services.Changes;
  5. using Umbraco.Web.Cache;
  6. using Umbraco.Web.PublishedCache;
  7.  
  8. using uSync8.BackOffice;
  9.  
  10. namespace uSync8.EventTriggers
  11. {
  12. /// <summary>
  13. /// Composer to register the events.
  14. /// </summary>
  15. [ComposeBefore(typeof(uSyncBackOfficeComposer))]
  16. public class EventTriggersComposer : ComponentComposer<EventTriggersComponent> { }
  17.  
  18. /// <summary>
  19. /// Component, will trigger a cache rebuild when an import is completed. (and there are changes)
  20. /// </summary>
  21. public class EventTriggersComponent : IComponent
  22. {
  23. private readonly IPublishedSnapshotService snapshotService;
  24.  
  25. public EventTriggersComponent(IPublishedSnapshotService snapshotService)
  26. {
  27. this.snapshotService = snapshotService;
  28. }
  29.  
  30. public void Initialize()
  31. {
  32. uSyncService.ImportComplete += BulkEventComplete;
  33. }
  34.  
  35. private void BulkEventComplete(uSyncBulkEventArgs e)
  36. {
  37. if (e.Actions.Any(x => x.Change > uSync8.Core.ChangeType.NoChange))
  38. {
  39. // change happened. - rebuild
  40. snapshotService.Rebuild();
  41.  
  42. // then refresh the cache :
  43.  
  44. // there is a function for this but it is internal, so we have extracted bits.
  45. // mimics => DistributedCache.RefreshAllPublishedSnapshot
  46.  
  47. RefreshContentCache(Umbraco.Web.Composing.Current.DistributedCache);
  48. RefreshMediaCache(Umbraco.Web.Composing.Current.DistributedCache);
  49. RefreshAllDomainCache(Umbraco.Web.Composing.Current.DistributedCache);
  50. }
  51. }
  52.  
  53. private void RefreshContentCache(DistributedCache dc) {
  54. var payloads = new[] { new ContentCacheRefresher.JsonPayload(0, TreeChangeTypes.RefreshAll) };
  55. Umbraco.Web.Composing.Current.DistributedCache.RefreshByPayload(ContentCacheRefresher.UniqueId, payloads);
  56. }
  57.  
  58. private void RefreshMediaCache(DistributedCache dc) {
  59. var payloads = new[] { new MediaCacheRefresher.JsonPayload(0, TreeChangeTypes.RefreshAll) };
  60. dc.RefreshByPayload(MediaCacheRefresher.UniqueId, payloads);
  61. }
  62.  
  63. public void RefreshAllDomainCache(DistributedCache dc)
  64. {
  65. var payloads = new[] { new DomainCacheRefresher.JsonPayload(0, DomainChangeTypes.RefreshAll) };
  66. dc.RefreshByPayload(DomainCacheRefresher.UniqueId, payloads);
  67. }
  68.  
  69.  
  70. public void Terminate()
  71. {
  72. // end
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement