Skip to content

Commit

Permalink
Use CacheRefreshers for load balancing setup (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdemooij9 authored Aug 20, 2023
1 parent 4213d2c commit d8d9c11
Show file tree
Hide file tree
Showing 19 changed files with 259 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using SeoToolkit.Umbraco.Common.Core.Constants;
using System;
using System.Security.Policy;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Events;

namespace SeoToolkit.Umbraco.Common.Core.Caching
{
public sealed class SeoSettingsCacheRefresher : CacheRefresherBase<SeoSettingsCacheRefresherNotification>
{
public static Guid CacheGuid = new("835839b0-457a-4a02-b5c8-3f8cfec5e350");

public override Guid RefresherUniqueId => CacheGuid;
public override string Name => "Seo Settings Cache Refresher";

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

public override void Refresh(int id)
{
AppCaches.RuntimeCache.ClearByKey($"{CacheConstants.SeoSettings}{id}");
base.Refresh(id);
}
}
}
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 SeoToolkit.Umbraco.Common.Core.Caching
{
public class SeoSettingsCacheRefresherNotification : CacheRefresherNotification
{
public SeoSettingsCacheRefresherNotification(object messageObject, MessageType messageType) : base(messageObject, messageType)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SeoToolkit.Umbraco.Common.Core.Constants
{
static internal class CacheConstants
{
public const string SeoSettings = "SeoSettingsService_";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using SeoToolkit.Umbraco.Common.Core.Caching;
using SeoToolkit.Umbraco.Common.Core.Constants;
using SeoToolkit.Umbraco.Common.Core.Repositories.SeoSettingsRepository;
using Umbraco.Cms.Core.Cache;
using Umbraco.Extensions;
Expand All @@ -7,28 +9,28 @@ namespace SeoToolkit.Umbraco.Common.Core.Services.SeoSettingsService
{
public class SeoSettingsService : ISeoSettingsService
{
private const string BaseCacheKey = "SeoSettingsService_";

private readonly ISeoSettingsRepository _seoSettingsRepository;
private readonly DistributedCache _distributedCache;
private readonly IAppPolicyCache _cache;

public SeoSettingsService(ISeoSettingsRepository seoSettingsRepository, AppCaches appCaches)
public SeoSettingsService(ISeoSettingsRepository seoSettingsRepository, AppCaches appCaches, DistributedCache distributedCache)
{
_seoSettingsRepository = seoSettingsRepository;
_distributedCache = distributedCache;
_cache = appCaches.RuntimeCache;
}

public bool IsEnabled(int contentTypeId)
{
return _cache.GetCacheItem($"{BaseCacheKey}{contentTypeId}",
return _cache.GetCacheItem($"{CacheConstants.SeoSettings}{contentTypeId}",
() => _seoSettingsRepository.IsEnabled(contentTypeId), TimeSpan.FromMinutes(10));
}

public void ToggleSeoSettings(int contentTypeId, bool value)
{
_seoSettingsRepository.Toggle(contentTypeId, value);

_cache.ClearByKey($"{BaseCacheKey}{contentTypeId}");
_distributedCache.Refresh(SeoSettingsCacheRefresher.CacheGuid, contentTypeId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using NPoco;
using SeoToolkit.Umbraco.MetaFields.Core.Constants;
using System;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Events;

namespace SeoToolkit.Umbraco.MetaFields.Core.Caching
{
public sealed class DocumentTypeSettingsCacheRefresher : CacheRefresherBase<DocumentTypeSettingsCacheRefresherNotification>
{
public static Guid CacheGuid = new("6ffa6a98-95d3-4060-bcfb-63be3875da0c");

public override Guid RefresherUniqueId => CacheGuid;

public override string Name => "Document Type Settings Cache Refresher";

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

public override void Refresh(int id)
{
AppCaches.RuntimeCache.ClearByKey($"{CacheConstants.DocumentTypeSettings}{id}");
base.Refresh(id);
}
}
}
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 SeoToolkit.Umbraco.MetaFields.Core.Caching
{
public class DocumentTypeSettingsCacheRefresherNotification : CacheRefresherNotification
{
public DocumentTypeSettingsCacheRefresherNotification(object messageObject, MessageType messageType) : base(messageObject, messageType)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using SeoToolkit.Umbraco.MetaFields.Core.Constants;
using System;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Events;

namespace SeoToolkit.Umbraco.MetaFields.Core.Caching
{
public class SeoValueCacheRefresher : CacheRefresherBase<SeoValueCacheRefresherNotification>
{
public static Guid CacheGuid = new("bf0f9b10-f56d-4385-9d48-0732c8e76846");

public override Guid RefresherUniqueId => CacheGuid;
public override string Name => "Seo Value Cache Refresher";

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

public override void Refresh(int id)
{
AppCaches.RuntimeCache.ClearByKey($"{CacheConstants.SeoValue}{id}");
base.Refresh(id);
}
}
}
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 SeoToolkit.Umbraco.MetaFields.Core.Caching
{
public class SeoValueCacheRefresherNotification : CacheRefresherNotification
{
public SeoValueCacheRefresherNotification(object messageObject, MessageType messageType) : base(messageObject, messageType)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SeoToolkit.Umbraco.MetaFields.Core.Constants
{
internal class CacheConstants
{
public const string DocumentTypeSettings = "DocumentTypeSettings_";
public const string SeoValue = "SeoValueService_";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@
using SeoToolkit.Umbraco.MetaFields.Core.Collections;
using SeoToolkit.Umbraco.MetaFields.Core.Common.FieldProviders;
using SeoToolkit.Umbraco.MetaFields.Core.Models.DocumentTypeSettings.Business;
using Microsoft.Extensions.Caching.Distributed;
using SeoToolkit.Umbraco.MetaFields.Core.Caching;
using SeoToolkit.Umbraco.MetaFields.Core.Constants;

namespace SeoToolkit.Umbraco.MetaFields.Core.Services.DocumentTypeSettings
{
public class MetaFieldsSettingsService : IMetaFieldsSettingsService
{
private const string BaseCacheKey = "DocumentTypeSettings_";

private readonly IRepository<DocumentTypeSettingsDto> _repository;
private readonly FieldProviderCollection _fieldProviders;
private readonly DistributedCache _distributedCache;
private readonly IAppPolicyCache _cache;

public MetaFieldsSettingsService(IRepository<DocumentTypeSettingsDto> repository,
FieldProviderCollection fieldProviders,
AppCaches appCaches)
AppCaches appCaches,
DistributedCache distributedCache)
{
_repository = repository;
_fieldProviders = fieldProviders;
_distributedCache = distributedCache;
_cache = appCaches.RuntimeCache;
}

Expand All @@ -41,7 +45,7 @@ public void Set(DocumentTypeSettingsDto model)

public DocumentTypeSettingsDto Get(int id)
{
return _cache.GetCacheItem($"{BaseCacheKey}{id}_Get", () =>
return _cache.GetCacheItem($"{CacheConstants.DocumentTypeSettings}{id}_Get", () =>
{
return new CachedNullableModel<DocumentTypeSettingsDto>(_repository.Get(id));
}, TimeSpan.FromMinutes(30)).Model;
Expand All @@ -54,7 +58,7 @@ public IEnumerable<FieldItemViewModel> GetAdditionalFieldItems()

private void ClearCache(int id)
{
_cache.ClearByKey($"{BaseCacheKey}{id}");
_distributedCache.Refresh(DocumentTypeSettingsCacheRefresher.CacheGuid, id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@
using Umbraco.Extensions;
using SeoToolkit.Umbraco.MetaFields.Core.Interfaces.Services;
using SeoToolkit.Umbraco.MetaFields.Core.Repositories.SeoValueRepository;
using SeoToolkit.Umbraco.MetaFields.Core.Constants;
using Microsoft.Extensions.Caching.Distributed;
using SeoToolkit.Umbraco.MetaFields.Core.Caching;

namespace SeoToolkit.Umbraco.MetaFields.Core.Services.SeoValueService
{
public class MetaFieldsValueService : IMetaFieldsValueService
{
private const string BaseCacheKey = "SeoValueService_";

private readonly IMetaFieldsValueRepository _repository;
private readonly IVariationContextAccessor _variationContextAccessor;
private readonly DistributedCache _distributedCache;
private readonly IAppPolicyCache _cache;

public MetaFieldsValueService(IMetaFieldsValueRepository repository, IVariationContextAccessor variationContextAccessor, AppCaches appCaches)
public MetaFieldsValueService(IMetaFieldsValueRepository repository, IVariationContextAccessor variationContextAccessor, AppCaches appCaches, DistributedCache distributedCache)
{
_repository = repository;
_variationContextAccessor = variationContextAccessor;
_distributedCache = distributedCache;
_cache = appCaches.RuntimeCache;
}

public Dictionary<string, object> GetUserValues(int nodeId, string culture = null)
{
var foundCulture = culture.IfNullOrWhiteSpace(GetCulture());
return _cache.GetCacheItem($"{BaseCacheKey}{nodeId}_{foundCulture}", () => _repository.GetAllValues(nodeId, foundCulture), TimeSpan.FromMinutes(30));
return _cache.GetCacheItem($"{CacheConstants.SeoValue}{nodeId}_{foundCulture}", () => _repository.GetAllValues(nodeId, foundCulture), TimeSpan.FromMinutes(30));
}

public void AddValues(int nodeId, Dictionary<string, object> values, string culture = null)
Expand Down Expand Up @@ -56,7 +59,7 @@ private string GetCulture()

private void ClearCache(int nodeId)
{
_cache.ClearByKey($"{BaseCacheKey}{nodeId}");
_distributedCache.Refresh(SeoValueCacheRefresher.CacheGuid, nodeId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using SeoToolkit.Umbraco.Redirects.Core.Constants;
using System;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Events;

namespace SeoToolkit.Umbraco.Redirects.Core.Caching
{
public sealed class RedirectsCacheRefresher : CacheRefresherBase<RedirectsCacheRefresherNotification>
{
public static Guid CacheGuid = new("3c46284f-3fad-49fe-9cad-f436f0762c5d");

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

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

public override void RefreshAll()
{
AppCaches.RuntimeCache.ClearByKey(CacheConstants.Redirects);
base.RefreshAll();
}
}
}
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 SeoToolkit.Umbraco.Redirects.Core.Caching
{
public class RedirectsCacheRefresherNotification : CacheRefresherNotification
{
public RedirectsCacheRefresherNotification(object messageObject, MessageType messageType) : base(messageObject, messageType)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SeoToolkit.Umbraco.Redirects.Core.Constants
{
static internal class CacheConstants
{
public const string Redirects = "redirects_";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,31 @@
using SeoToolkit.Umbraco.Redirects.Core.Models.Business;
using SeoToolkit.Umbraco.Redirects.Core.Models.Database;
using Umbraco.Cms.Infrastructure.Scoping;
using SeoToolkit.Umbraco.Redirects.Core.Constants;
using Microsoft.Extensions.Caching.Distributed;
using SeoToolkit.Umbraco.Redirects.Core.Caching;

namespace SeoToolkit.Umbraco.Redirects.Core.Repositories
{
public class RedirectsRepository : IRedirectsRepository
{
private const string BaseCacheKey = "redirects_";

private readonly IScopeProvider _scopeProvider;
private readonly IUmbracoContextFactory _umbracoContextFactory;
private readonly ILocalizationService _localizationService;
private readonly AppCaches _appCaches;
private readonly DistributedCache _distributedCache;

public RedirectsRepository(IScopeProvider scopeProvider,
IUmbracoContextFactory umbracoContextFactory,
ILocalizationService localizationService,
AppCaches appCaches)
AppCaches appCaches,
DistributedCache distributedCache)
{
_scopeProvider = scopeProvider;
_umbracoContextFactory = umbracoContextFactory;
_localizationService = localizationService;
_appCaches = appCaches;
_distributedCache = distributedCache;
}

public void Save(Redirect redirect)
Expand Down Expand Up @@ -95,7 +99,7 @@ public IEnumerable<Redirect> GetAll(int pageNumber, int pageSize, out long total

public IEnumerable<Redirect> GetAllRegexRedirects()
{
return _appCaches.RuntimeCache.GetCacheItem($"{BaseCacheKey}GetRegexRedirects", () =>
return _appCaches.RuntimeCache.GetCacheItem($"{CacheConstants.Redirects}GetRegexRedirects", () =>
{
using (var scope = _scopeProvider.CreateScope(autoComplete: true))
{
Expand Down Expand Up @@ -165,7 +169,7 @@ private Redirect ToModel(RedirectEntity entity)

private void ClearCache()
{
_appCaches.RuntimeCache.ClearByKey(BaseCacheKey);
_distributedCache.RefreshAll(RedirectsCacheRefresher.CacheGuid);
}

private Expression<Func<RedirectEntity, object>> GetOrderingColumn(string orderBy)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using SeoToolkit.Umbraco.ScriptManager.Core.Constants;
using System;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Events;

namespace SeoToolkit.Umbraco.ScriptManager.Core.Caching
{
public sealed class ScriptManagerCacheRefresher : CacheRefresherBase<ScriptManagerCacheRefresherNotification>
{
public static Guid CacheGuid = new("70b9222a-db9c-4da3-9c3a-63a8174cf6d8");

public override Guid RefresherUniqueId => CacheGuid;
public override string Name => "Script Manager Cache Refresher";

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

public override void RefreshAll()
{
AppCaches.RuntimeCache.ClearByKey(CacheConstants.ScriptManager);
base.RefreshAll();
}
}
}
Loading

0 comments on commit d8d9c11

Please sign in to comment.