diff --git a/src/Geta.NotFoundHandler.Optimizely/Core/Events/OptimizelySyncEvents.cs b/src/Geta.NotFoundHandler.Optimizely/Core/Events/OptimizelySyncEvents.cs index 53222b4e..ca2b75ae 100644 --- a/src/Geta.NotFoundHandler.Optimizely/Core/Events/OptimizelySyncEvents.cs +++ b/src/Geta.NotFoundHandler.Optimizely/Core/Events/OptimizelySyncEvents.cs @@ -3,6 +3,7 @@ using System; using EPiServer.Events.Clients; +using Geta.NotFoundHandler.Core.Providers.RegexRedirects; using Geta.NotFoundHandler.Core.Redirects; namespace Geta.NotFoundHandler.Optimizely.Core.Events @@ -12,27 +13,33 @@ public class OptimizelySyncEvents private readonly RedirectsEvents _redirectsEvents; private readonly IEventRegistry _eventRegistry; private readonly RedirectsInitializer _redirectsInitializer; + private readonly IRegexRedirectCache _regexRedirectCache; - private static readonly Guid EventId = new("{AC263F88-6C17-45A5-81E0-DCC28DF26AEF}"); + private static readonly Guid UpdateEventId = new("{AC263F88-6C17-45A5-81E0-DCC28DF26AEF}"); + private static readonly Guid RegexUpdateEventId = new("{334DF0A0-793C-4B19-A0CC-8AD63F705FDD}"); private static readonly Guid RaiserId = Guid.NewGuid(); public OptimizelySyncEvents( RedirectsEvents redirectsEvents, IEventRegistry eventRegistry, - RedirectsInitializer redirectsInitializer) + RedirectsInitializer redirectsInitializer, + IRegexRedirectCache regexRedirectCache) { _redirectsEvents = redirectsEvents; _eventRegistry = eventRegistry; _redirectsInitializer = redirectsInitializer; + _regexRedirectCache = regexRedirectCache; } public void Initialize() { - _redirectsEvents.OnUpdated += OnRedirectsUpdated; - _eventRegistry.Get(EventId).Raised += SyncEventRaised; + _redirectsEvents.OnRedirectsUpdated += OnRedirectsUpdated; + _redirectsEvents.OnRegexRedirectsUpdated += OnRegexRedirectsUpdated; + _eventRegistry.Get(UpdateEventId).Raised += SyncRedirectsUpdateEventRaised; + _eventRegistry.Get(RegexUpdateEventId).Raised += SyncRegexRedirectsUpdateEventRaised; } - private void SyncEventRaised(object sender, EPiServer.Events.EventNotificationEventArgs e) + private void SyncRedirectsUpdateEventRaised(object sender, EPiServer.Events.EventNotificationEventArgs e) { if (e.RaiserId != RaiserId) { @@ -40,9 +47,22 @@ private void SyncEventRaised(object sender, EPiServer.Events.EventNotificationEv } } + private void SyncRegexRedirectsUpdateEventRaised(object sender, EPiServer.Events.EventNotificationEventArgs e) + { + if (e.RaiserId != RaiserId) + { + _regexRedirectCache.Remove(); + } + } + private void OnRedirectsUpdated(EventArgs e) { - _eventRegistry.Get(EventId).Raise(RaiserId, EventId); + _eventRegistry.Get(UpdateEventId).Raise(RaiserId, UpdateEventId); + } + + private void OnRegexRedirectsUpdated(EventArgs e) + { + _eventRegistry.Get(RegexUpdateEventId).Raise(RaiserId, RegexUpdateEventId); } } } diff --git a/src/Geta.NotFoundHandler/Core/Providers/RegexRedirects/DefaultRegexRedirectsService.cs b/src/Geta.NotFoundHandler/Core/Providers/RegexRedirects/DefaultRegexRedirectsService.cs index 93676732..59cf0079 100644 --- a/src/Geta.NotFoundHandler/Core/Providers/RegexRedirects/DefaultRegexRedirectsService.cs +++ b/src/Geta.NotFoundHandler/Core/Providers/RegexRedirects/DefaultRegexRedirectsService.cs @@ -2,6 +2,7 @@ // Licensed under Apache-2.0. See the LICENSE file in the project root for more information using System; +using Geta.NotFoundHandler.Core.Redirects; using Geta.NotFoundHandler.Data; namespace Geta.NotFoundHandler.Core.Providers.RegexRedirects; @@ -12,17 +13,20 @@ public class DefaultRegexRedirectsService : IRegexRedirectsService private readonly IRepository _repository; private readonly IRegexRedirectLoader _redirectLoader; private readonly IRegexRedirectOrderUpdater _orderUpdater; + private readonly RedirectsEvents _redirectsEvents; public DefaultRegexRedirectsService( RegexRedirectFactory regexRedirectFactory, IRepository repository, IRegexRedirectLoader redirectLoader, - IRegexRedirectOrderUpdater orderUpdater) + IRegexRedirectOrderUpdater orderUpdater, + RedirectsEvents redirectsEvents) { _regexRedirectFactory = regexRedirectFactory; _repository = repository; _redirectLoader = redirectLoader; _orderUpdater = orderUpdater; + _redirectsEvents = redirectsEvents; } public void Create(string oldUrlRegex, string newUrlFormat, int orderNumber) @@ -30,6 +34,7 @@ public void Create(string oldUrlRegex, string newUrlFormat, int orderNumber) var regexRedirect = _regexRedirectFactory.CreateNew(oldUrlRegex, newUrlFormat, orderNumber); _repository.Save(regexRedirect); _orderUpdater.UpdateOrder(); + _redirectsEvents.RegexRedirectsUpdated(); } public void Update(Guid id, string oldUrlRegex, string newUrlFormat, int orderNumber) @@ -39,6 +44,7 @@ public void Update(Guid id, string oldUrlRegex, string newUrlFormat, int orderNu var regexRedirect = _regexRedirectFactory.Create(id, oldUrlRegex, newUrlFormat, orderNumber); _repository.Save(regexRedirect); _orderUpdater.UpdateOrder(isIncrease); + _redirectsEvents.RegexRedirectsUpdated(); } public void Delete(Guid id) @@ -46,5 +52,6 @@ public void Delete(Guid id) var regexRedirect = _regexRedirectFactory.CreateForDeletion(id); _repository.Delete(regexRedirect); _orderUpdater.UpdateOrder(); + _redirectsEvents.RegexRedirectsUpdated(); } } diff --git a/src/Geta.NotFoundHandler/Core/Providers/RegexRedirects/IRegexRedirectCache.cs b/src/Geta.NotFoundHandler/Core/Providers/RegexRedirects/IRegexRedirectCache.cs new file mode 100644 index 00000000..91f44605 --- /dev/null +++ b/src/Geta.NotFoundHandler/Core/Providers/RegexRedirects/IRegexRedirectCache.cs @@ -0,0 +1,10 @@ +// Copyright (c) Geta Digital. All rights reserved. +// Licensed under Apache-2.0. See the LICENSE file in the project root for more information + + +namespace Geta.NotFoundHandler.Core.Providers.RegexRedirects; + +public interface IRegexRedirectCache +{ + void Remove(); +} diff --git a/src/Geta.NotFoundHandler/Core/Providers/RegexRedirects/MemoryCacheRegexRedirectRepository.cs b/src/Geta.NotFoundHandler/Core/Providers/RegexRedirects/MemoryCacheRegexRedirectRepository.cs index e3071264..6e777333 100644 --- a/src/Geta.NotFoundHandler/Core/Providers/RegexRedirects/MemoryCacheRegexRedirectRepository.cs +++ b/src/Geta.NotFoundHandler/Core/Providers/RegexRedirects/MemoryCacheRegexRedirectRepository.cs @@ -8,7 +8,7 @@ namespace Geta.NotFoundHandler.Core.Providers.RegexRedirects; -public class MemoryCacheRegexRedirectRepository : IRepository, IRegexRedirectLoader, IRegexRedirectOrderUpdater +public class MemoryCacheRegexRedirectRepository : IRepository, IRegexRedirectLoader, IRegexRedirectOrderUpdater, IRegexRedirectCache { private readonly IRepository _repository; private readonly IRegexRedirectLoader _redirectLoader; @@ -47,18 +47,22 @@ public RegexRedirect Get(Guid id) public void Save(RegexRedirect entity) { _repository.Save(entity); - _cache.Remove(GetAllCacheKey); + Remove(); } public void Delete(RegexRedirect entity) { _repository.Delete(entity); - _cache.Remove(GetAllCacheKey); + Remove(); } public void UpdateOrder(bool isIncrease = false) { _orderUpdater.UpdateOrder(isIncrease); + } + + public void Remove() + { _cache.Remove(GetAllCacheKey); } } diff --git a/src/Geta.NotFoundHandler/Core/Redirects/RedirectsEvents.cs b/src/Geta.NotFoundHandler/Core/Redirects/RedirectsEvents.cs index ba07e06c..56e207fd 100644 --- a/src/Geta.NotFoundHandler/Core/Redirects/RedirectsEvents.cs +++ b/src/Geta.NotFoundHandler/Core/Redirects/RedirectsEvents.cs @@ -7,11 +7,17 @@ namespace Geta.NotFoundHandler.Core.Redirects { public class RedirectsEvents { - public event EventHandler OnUpdated; + public event EventHandler OnRedirectsUpdated; + public event EventHandler OnRegexRedirectsUpdated; public void RedirectsUpdated() { - OnUpdated?.Invoke(new EventArgs()); + OnRedirectsUpdated?.Invoke(new EventArgs()); + } + + public void RegexRedirectsUpdated() + { + OnRegexRedirectsUpdated?.Invoke(new EventArgs()); } } diff --git a/src/Geta.NotFoundHandler/Core/Redirects/RedirectsInitializer.cs b/src/Geta.NotFoundHandler/Core/Redirects/RedirectsInitializer.cs index 724b50f6..ba8ced9b 100644 --- a/src/Geta.NotFoundHandler/Core/Redirects/RedirectsInitializer.cs +++ b/src/Geta.NotFoundHandler/Core/Redirects/RedirectsInitializer.cs @@ -21,7 +21,7 @@ public RedirectsInitializer( _redirectHandler = redirectHandler; _redirectsServiceFactory = redirectsServiceFactory; _providers = providers; - redirectsEvents.OnUpdated += OnRedirectsUpdated; + redirectsEvents.OnRedirectsUpdated += OnRedirectsUpdated; } private void OnRedirectsUpdated(EventArgs e) diff --git a/src/Geta.NotFoundHandler/Infrastructure/Configuration/ServiceCollectionExtensions.cs b/src/Geta.NotFoundHandler/Infrastructure/Configuration/ServiceCollectionExtensions.cs index 0d7bbe41..8d326665 100644 --- a/src/Geta.NotFoundHandler/Infrastructure/Configuration/ServiceCollectionExtensions.cs +++ b/src/Geta.NotFoundHandler/Infrastructure/Configuration/ServiceCollectionExtensions.cs @@ -55,6 +55,11 @@ public static IServiceCollection AddNotFoundHandler( services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient( + x => new MemoryCacheRegexRedirectRepository(x.GetRequiredService(), + x.GetRequiredService(), + x.GetRequiredService(), + x.GetRequiredService())); services.AddTransient>( x => new MemoryCacheRegexRedirectRepository(x.GetRequiredService(), x.GetRequiredService(),