Skip to content

Commit

Permalink
Merge pull request #68 from Geta/regex-sync
Browse files Browse the repository at this point in the history
Regex sync
  • Loading branch information
marisks authored Dec 19, 2022
2 parents d915358 + 026632f commit d4bbe53
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,37 +13,56 @@ 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)
{
_redirectsInitializer.Initialize();
}
}

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,24 +13,28 @@ public class DefaultRegexRedirectsService : IRegexRedirectsService
private readonly IRepository<RegexRedirect> _repository;
private readonly IRegexRedirectLoader _redirectLoader;
private readonly IRegexRedirectOrderUpdater _orderUpdater;
private readonly RedirectsEvents _redirectsEvents;

public DefaultRegexRedirectsService(
RegexRedirectFactory regexRedirectFactory,
IRepository<RegexRedirect> 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)
{
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)
Expand All @@ -39,12 +44,14 @@ 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)
{
var regexRedirect = _regexRedirectFactory.CreateForDeletion(id);
_repository.Delete(regexRedirect);
_orderUpdater.UpdateOrder();
_redirectsEvents.RegexRedirectsUpdated();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Geta.NotFoundHandler.Core.Providers.RegexRedirects;

public class MemoryCacheRegexRedirectRepository : IRepository<RegexRedirect>, IRegexRedirectLoader, IRegexRedirectOrderUpdater
public class MemoryCacheRegexRedirectRepository : IRepository<RegexRedirect>, IRegexRedirectLoader, IRegexRedirectOrderUpdater, IRegexRedirectCache
{
private readonly IRepository<RegexRedirect> _repository;
private readonly IRegexRedirectLoader _redirectLoader;
Expand Down Expand Up @@ -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);
}
}
10 changes: 8 additions & 2 deletions src/Geta.NotFoundHandler/Core/Redirects/RedirectsEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public RedirectsInitializer(
_redirectHandler = redirectHandler;
_redirectsServiceFactory = redirectsServiceFactory;
_providers = providers;
redirectsEvents.OnUpdated += OnRedirectsUpdated;
redirectsEvents.OnRedirectsUpdated += OnRedirectsUpdated;
}

private void OnRedirectsUpdated(EventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public static IServiceCollection AddNotFoundHandler(
services.AddTransient<RegexRedirectFactory>();
services.AddTransient<INotFoundHandler, RegexRedirectNotFoundHandler>();
services.AddTransient<SqlRegexRedirectRepository>();
services.AddTransient<IRegexRedirectCache>(
x => new MemoryCacheRegexRedirectRepository(x.GetRequiredService<SqlRegexRedirectRepository>(),
x.GetRequiredService<SqlRegexRedirectRepository>(),
x.GetRequiredService<SqlRegexRedirectRepository>(),
x.GetRequiredService<IMemoryCache>()));
services.AddTransient<IRepository<RegexRedirect>>(
x => new MemoryCacheRegexRedirectRepository(x.GetRequiredService<SqlRegexRedirectRepository>(),
x.GetRequiredService<SqlRegexRedirectRepository>(),
Expand Down

0 comments on commit d4bbe53

Please sign in to comment.