Skip to content

Commit

Permalink
Merge pull request #25 from patrickdemooij9/feature/LoadBalancingCache
Browse files Browse the repository at this point in the history
Make sure to clear cache for load balancing websites
  • Loading branch information
patrickdemooij9 authored Jun 27, 2022
2 parents fcfcc1b + 8adb03e commit 43cba67
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Sync;

namespace SimpleRedirects.Core.Notifications
{
public class RedirectCacheRefresherNotification : CacheRefresherNotification
{
public RedirectCacheRefresherNotification(object messageObject, MessageType messageType) : base(messageObject, messageType)
{
}
}
}
9 changes: 6 additions & 3 deletions source/SimpleRedirects.Core/RedirectRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using SimpleRedirects.Core.Extensions;
using SimpleRedirects.Core.Models;
using SimpleRedirects.Core.Utilities.Caching;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Extensions;

Expand All @@ -15,14 +16,16 @@ namespace SimpleRedirects.Core
public class RedirectRepository
{
private readonly ICacheManager _cacheManager;
private const string CacheCategoryKey = "Redirects";
private readonly DistributedCache _distributedCache;
public const string CacheCategoryKey = "Redirects";

private readonly IScopeProvider _scopeProvider;

public RedirectRepository(IScopeProvider scopeProvider, ICacheManager cacheManager)
public RedirectRepository(IScopeProvider scopeProvider, ICacheManager cacheManager, DistributedCache distributedCache)
{
_scopeProvider = scopeProvider;
_cacheManager = cacheManager;
_distributedCache = distributedCache;
}

/// <summary>
Expand Down Expand Up @@ -195,7 +198,7 @@ public Redirect FindRedirect(Uri oldUrl)
/// </summary>
public void ClearCache()
{
_cacheManager.ClearByKeyPrefix(CacheCategoryKey);
_distributedCache.RefreshAll(RedirectCacheRefresher.UniqueId);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion source/SimpleRedirects.Core/SimpleRedirects.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<ContentTargetFolders>.</ContentTargetFolders>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using SimpleRedirects.Core.Notifications;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Events;

namespace SimpleRedirects.Core.Utilities.Caching
{
public class RedirectCacheRefresher : CacheRefresherBase<RedirectCacheRefresherNotification>
{
public static readonly Guid UniqueId = Guid.Parse("fe3847bc-80c4-4ce0-abde-551bb409599a");

public RedirectCacheRefresher(AppCaches appCaches, IEventAggregator eventAggregator, ICacheRefresherNotificationFactory factory) : base(appCaches, eventAggregator, factory)
{
}

public override Guid RefresherUniqueId => UniqueId;
public override string Name => "Redirects Cache Refresher";

public override void RefreshAll()
{
AppCaches.RuntimeCache.ClearByKey(RedirectRepository.CacheCategoryKey);
base.RefreshAll();
}
}
}

0 comments on commit 43cba67

Please sign in to comment.