From d8d9c1180366a17035ea22451f62db8c2d46a77c Mon Sep 17 00:00:00 2001 From: patrickdemooij9 Date: Sun, 20 Aug 2023 16:25:07 +0200 Subject: [PATCH] Use CacheRefreshers for load balancing setup (#222) --- .../Caching/SeoSettingsCacheRefresher.cs | 26 ++++++++++++++++++ .../SeoSettingsCacheRefresherNotification.cs | 12 +++++++++ .../Constants/CacheConstants.cs | 7 +++++ .../SeoSettingsService/SeoSettingsService.cs | 12 +++++---- .../DocumentTypeSettingsCacheRefresher.cs | 27 +++++++++++++++++++ ...tTypeSettingsCacheRefresherNotification.cs | 12 +++++++++ .../Caching/SeoValueCacheRefresher.cs | 25 +++++++++++++++++ .../SeoValueCacheRefresherNotification.cs | 12 +++++++++ .../Constants/CacheConstants.cs | 8 ++++++ .../MetaFieldsSettingsService.cs | 14 ++++++---- .../MetaFieldsValueService.cs | 13 +++++---- .../Caching/RedirectsCacheRefresher.cs | 25 +++++++++++++++++ .../RedirectsCacheRefresherNotification.cs | 12 +++++++++ .../Constants/CacheConstants.cs | 7 +++++ .../Repositories/RedirectsRepository.cs | 14 ++++++---- .../Caching/ScriptManagerCacheRefresher.cs | 25 +++++++++++++++++ ...ScriptManagerCacheRefresherNotification.cs | 12 +++++++++ .../Constants/CacheConstants.cs | 7 +++++ .../Services/ScriptManagerService.cs | 16 ++++++----- 19 files changed, 259 insertions(+), 27 deletions(-) create mode 100644 src/SeoToolkit.Umbraco.Common.Core/Caching/SeoSettingsCacheRefresher.cs create mode 100644 src/SeoToolkit.Umbraco.Common.Core/Caching/SeoSettingsCacheRefresherNotification.cs create mode 100644 src/SeoToolkit.Umbraco.Common.Core/Constants/CacheConstants.cs create mode 100644 src/SeoToolkit.Umbraco.MetaFields.Core/Caching/DocumentTypeSettingsCacheRefresher.cs create mode 100644 src/SeoToolkit.Umbraco.MetaFields.Core/Caching/DocumentTypeSettingsCacheRefresherNotification.cs create mode 100644 src/SeoToolkit.Umbraco.MetaFields.Core/Caching/SeoValueCacheRefresher.cs create mode 100644 src/SeoToolkit.Umbraco.MetaFields.Core/Caching/SeoValueCacheRefresherNotification.cs create mode 100644 src/SeoToolkit.Umbraco.MetaFields.Core/Constants/CacheConstants.cs create mode 100644 src/SeoToolkit.Umbraco.Redirects.Core/Caching/RedirectsCacheRefresher.cs create mode 100644 src/SeoToolkit.Umbraco.Redirects.Core/Caching/RedirectsCacheRefresherNotification.cs create mode 100644 src/SeoToolkit.Umbraco.Redirects.Core/Constants/CacheConstants.cs create mode 100644 src/SeoToolkit.Umbraco.ScriptManager.Core/Caching/ScriptManagerCacheRefresher.cs create mode 100644 src/SeoToolkit.Umbraco.ScriptManager.Core/Caching/ScriptManagerCacheRefresherNotification.cs create mode 100644 src/SeoToolkit.Umbraco.ScriptManager.Core/Constants/CacheConstants.cs diff --git a/src/SeoToolkit.Umbraco.Common.Core/Caching/SeoSettingsCacheRefresher.cs b/src/SeoToolkit.Umbraco.Common.Core/Caching/SeoSettingsCacheRefresher.cs new file mode 100644 index 00000000..fc5a6d66 --- /dev/null +++ b/src/SeoToolkit.Umbraco.Common.Core/Caching/SeoSettingsCacheRefresher.cs @@ -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 + { + 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); + } + } +} diff --git a/src/SeoToolkit.Umbraco.Common.Core/Caching/SeoSettingsCacheRefresherNotification.cs b/src/SeoToolkit.Umbraco.Common.Core/Caching/SeoSettingsCacheRefresherNotification.cs new file mode 100644 index 00000000..252dfe77 --- /dev/null +++ b/src/SeoToolkit.Umbraco.Common.Core/Caching/SeoSettingsCacheRefresherNotification.cs @@ -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) + { + } + } +} diff --git a/src/SeoToolkit.Umbraco.Common.Core/Constants/CacheConstants.cs b/src/SeoToolkit.Umbraco.Common.Core/Constants/CacheConstants.cs new file mode 100644 index 00000000..31c353e2 --- /dev/null +++ b/src/SeoToolkit.Umbraco.Common.Core/Constants/CacheConstants.cs @@ -0,0 +1,7 @@ +namespace SeoToolkit.Umbraco.Common.Core.Constants +{ + static internal class CacheConstants + { + public const string SeoSettings = "SeoSettingsService_"; + } +} diff --git a/src/SeoToolkit.Umbraco.Common.Core/Services/SeoSettingsService/SeoSettingsService.cs b/src/SeoToolkit.Umbraco.Common.Core/Services/SeoSettingsService/SeoSettingsService.cs index 6b34ecb6..96ecd38a 100644 --- a/src/SeoToolkit.Umbraco.Common.Core/Services/SeoSettingsService/SeoSettingsService.cs +++ b/src/SeoToolkit.Umbraco.Common.Core/Services/SeoSettingsService/SeoSettingsService.cs @@ -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; @@ -7,20 +9,20 @@ 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)); } @@ -28,7 +30,7 @@ public void ToggleSeoSettings(int contentTypeId, bool value) { _seoSettingsRepository.Toggle(contentTypeId, value); - _cache.ClearByKey($"{BaseCacheKey}{contentTypeId}"); + _distributedCache.Refresh(SeoSettingsCacheRefresher.CacheGuid, contentTypeId); } } } diff --git a/src/SeoToolkit.Umbraco.MetaFields.Core/Caching/DocumentTypeSettingsCacheRefresher.cs b/src/SeoToolkit.Umbraco.MetaFields.Core/Caching/DocumentTypeSettingsCacheRefresher.cs new file mode 100644 index 00000000..0685caf2 --- /dev/null +++ b/src/SeoToolkit.Umbraco.MetaFields.Core/Caching/DocumentTypeSettingsCacheRefresher.cs @@ -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 + { + 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); + } + } +} diff --git a/src/SeoToolkit.Umbraco.MetaFields.Core/Caching/DocumentTypeSettingsCacheRefresherNotification.cs b/src/SeoToolkit.Umbraco.MetaFields.Core/Caching/DocumentTypeSettingsCacheRefresherNotification.cs new file mode 100644 index 00000000..300d7a76 --- /dev/null +++ b/src/SeoToolkit.Umbraco.MetaFields.Core/Caching/DocumentTypeSettingsCacheRefresherNotification.cs @@ -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) + { + } + } +} diff --git a/src/SeoToolkit.Umbraco.MetaFields.Core/Caching/SeoValueCacheRefresher.cs b/src/SeoToolkit.Umbraco.MetaFields.Core/Caching/SeoValueCacheRefresher.cs new file mode 100644 index 00000000..479305e7 --- /dev/null +++ b/src/SeoToolkit.Umbraco.MetaFields.Core/Caching/SeoValueCacheRefresher.cs @@ -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 + { + 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); + } + } +} diff --git a/src/SeoToolkit.Umbraco.MetaFields.Core/Caching/SeoValueCacheRefresherNotification.cs b/src/SeoToolkit.Umbraco.MetaFields.Core/Caching/SeoValueCacheRefresherNotification.cs new file mode 100644 index 00000000..3d9901da --- /dev/null +++ b/src/SeoToolkit.Umbraco.MetaFields.Core/Caching/SeoValueCacheRefresherNotification.cs @@ -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) + { + } + } +} diff --git a/src/SeoToolkit.Umbraco.MetaFields.Core/Constants/CacheConstants.cs b/src/SeoToolkit.Umbraco.MetaFields.Core/Constants/CacheConstants.cs new file mode 100644 index 00000000..40dfc749 --- /dev/null +++ b/src/SeoToolkit.Umbraco.MetaFields.Core/Constants/CacheConstants.cs @@ -0,0 +1,8 @@ +namespace SeoToolkit.Umbraco.MetaFields.Core.Constants +{ + internal class CacheConstants + { + public const string DocumentTypeSettings = "DocumentTypeSettings_"; + public const string SeoValue = "SeoValueService_"; + } +} diff --git a/src/SeoToolkit.Umbraco.MetaFields.Core/Services/DocumentTypeSettings/MetaFieldsSettingsService.cs b/src/SeoToolkit.Umbraco.MetaFields.Core/Services/DocumentTypeSettings/MetaFieldsSettingsService.cs index 15d90513..d9aa741a 100644 --- a/src/SeoToolkit.Umbraco.MetaFields.Core/Services/DocumentTypeSettings/MetaFieldsSettingsService.cs +++ b/src/SeoToolkit.Umbraco.MetaFields.Core/Services/DocumentTypeSettings/MetaFieldsSettingsService.cs @@ -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 _repository; private readonly FieldProviderCollection _fieldProviders; + private readonly DistributedCache _distributedCache; private readonly IAppPolicyCache _cache; public MetaFieldsSettingsService(IRepository repository, FieldProviderCollection fieldProviders, - AppCaches appCaches) + AppCaches appCaches, + DistributedCache distributedCache) { _repository = repository; _fieldProviders = fieldProviders; + _distributedCache = distributedCache; _cache = appCaches.RuntimeCache; } @@ -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(_repository.Get(id)); }, TimeSpan.FromMinutes(30)).Model; @@ -54,7 +58,7 @@ public IEnumerable GetAdditionalFieldItems() private void ClearCache(int id) { - _cache.ClearByKey($"{BaseCacheKey}{id}"); + _distributedCache.Refresh(DocumentTypeSettingsCacheRefresher.CacheGuid, id); } } } diff --git a/src/SeoToolkit.Umbraco.MetaFields.Core/Services/MetaFieldsValueService/MetaFieldsValueService.cs b/src/SeoToolkit.Umbraco.MetaFields.Core/Services/MetaFieldsValueService/MetaFieldsValueService.cs index 048559f3..1bf8ae5a 100644 --- a/src/SeoToolkit.Umbraco.MetaFields.Core/Services/MetaFieldsValueService/MetaFieldsValueService.cs +++ b/src/SeoToolkit.Umbraco.MetaFields.Core/Services/MetaFieldsValueService/MetaFieldsValueService.cs @@ -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 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 values, string culture = null) @@ -56,7 +59,7 @@ private string GetCulture() private void ClearCache(int nodeId) { - _cache.ClearByKey($"{BaseCacheKey}{nodeId}"); + _distributedCache.Refresh(SeoValueCacheRefresher.CacheGuid, nodeId); } } } diff --git a/src/SeoToolkit.Umbraco.Redirects.Core/Caching/RedirectsCacheRefresher.cs b/src/SeoToolkit.Umbraco.Redirects.Core/Caching/RedirectsCacheRefresher.cs new file mode 100644 index 00000000..aba3d864 --- /dev/null +++ b/src/SeoToolkit.Umbraco.Redirects.Core/Caching/RedirectsCacheRefresher.cs @@ -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 + { + 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(); + } + } +} diff --git a/src/SeoToolkit.Umbraco.Redirects.Core/Caching/RedirectsCacheRefresherNotification.cs b/src/SeoToolkit.Umbraco.Redirects.Core/Caching/RedirectsCacheRefresherNotification.cs new file mode 100644 index 00000000..e220d82e --- /dev/null +++ b/src/SeoToolkit.Umbraco.Redirects.Core/Caching/RedirectsCacheRefresherNotification.cs @@ -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) + { + } + } +} diff --git a/src/SeoToolkit.Umbraco.Redirects.Core/Constants/CacheConstants.cs b/src/SeoToolkit.Umbraco.Redirects.Core/Constants/CacheConstants.cs new file mode 100644 index 00000000..d4cff382 --- /dev/null +++ b/src/SeoToolkit.Umbraco.Redirects.Core/Constants/CacheConstants.cs @@ -0,0 +1,7 @@ +namespace SeoToolkit.Umbraco.Redirects.Core.Constants +{ + static internal class CacheConstants + { + public const string Redirects = "redirects_"; + } +} diff --git a/src/SeoToolkit.Umbraco.Redirects.Core/Repositories/RedirectsRepository.cs b/src/SeoToolkit.Umbraco.Redirects.Core/Repositories/RedirectsRepository.cs index ca354413..b271375d 100644 --- a/src/SeoToolkit.Umbraco.Redirects.Core/Repositories/RedirectsRepository.cs +++ b/src/SeoToolkit.Umbraco.Redirects.Core/Repositories/RedirectsRepository.cs @@ -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) @@ -95,7 +99,7 @@ public IEnumerable GetAll(int pageNumber, int pageSize, out long total public IEnumerable GetAllRegexRedirects() { - return _appCaches.RuntimeCache.GetCacheItem($"{BaseCacheKey}GetRegexRedirects", () => + return _appCaches.RuntimeCache.GetCacheItem($"{CacheConstants.Redirects}GetRegexRedirects", () => { using (var scope = _scopeProvider.CreateScope(autoComplete: true)) { @@ -165,7 +169,7 @@ private Redirect ToModel(RedirectEntity entity) private void ClearCache() { - _appCaches.RuntimeCache.ClearByKey(BaseCacheKey); + _distributedCache.RefreshAll(RedirectsCacheRefresher.CacheGuid); } private Expression> GetOrderingColumn(string orderBy) diff --git a/src/SeoToolkit.Umbraco.ScriptManager.Core/Caching/ScriptManagerCacheRefresher.cs b/src/SeoToolkit.Umbraco.ScriptManager.Core/Caching/ScriptManagerCacheRefresher.cs new file mode 100644 index 00000000..b1cd3fac --- /dev/null +++ b/src/SeoToolkit.Umbraco.ScriptManager.Core/Caching/ScriptManagerCacheRefresher.cs @@ -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 + { + 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(); + } + } +} diff --git a/src/SeoToolkit.Umbraco.ScriptManager.Core/Caching/ScriptManagerCacheRefresherNotification.cs b/src/SeoToolkit.Umbraco.ScriptManager.Core/Caching/ScriptManagerCacheRefresherNotification.cs new file mode 100644 index 00000000..d406f69e --- /dev/null +++ b/src/SeoToolkit.Umbraco.ScriptManager.Core/Caching/ScriptManagerCacheRefresherNotification.cs @@ -0,0 +1,12 @@ +using Umbraco.Cms.Core.Notifications; +using Umbraco.Cms.Core.Sync; + +namespace SeoToolkit.Umbraco.ScriptManager.Core.Caching +{ + public class ScriptManagerCacheRefresherNotification : CacheRefresherNotification + { + public ScriptManagerCacheRefresherNotification(object messageObject, MessageType messageType) : base(messageObject, messageType) + { + } + } +} diff --git a/src/SeoToolkit.Umbraco.ScriptManager.Core/Constants/CacheConstants.cs b/src/SeoToolkit.Umbraco.ScriptManager.Core/Constants/CacheConstants.cs new file mode 100644 index 00000000..432b307b --- /dev/null +++ b/src/SeoToolkit.Umbraco.ScriptManager.Core/Constants/CacheConstants.cs @@ -0,0 +1,7 @@ +namespace SeoToolkit.Umbraco.ScriptManager.Core.Constants +{ + static internal class CacheConstants + { + public const string ScriptManager = "ScriptManager_"; + } +} diff --git a/src/SeoToolkit.Umbraco.ScriptManager.Core/Services/ScriptManagerService.cs b/src/SeoToolkit.Umbraco.ScriptManager.Core/Services/ScriptManagerService.cs index fd07b828..da8da355 100644 --- a/src/SeoToolkit.Umbraco.ScriptManager.Core/Services/ScriptManagerService.cs +++ b/src/SeoToolkit.Umbraco.ScriptManager.Core/Services/ScriptManagerService.cs @@ -5,19 +5,21 @@ using SeoToolkit.Umbraco.ScriptManager.Core.Interfaces; using SeoToolkit.Umbraco.ScriptManager.Core.Interfaces.Services; using SeoToolkit.Umbraco.ScriptManager.Core.Models.Business; +using SeoToolkit.Umbraco.ScriptManager.Core.Caching; +using SeoToolkit.Umbraco.ScriptManager.Core.Constants; namespace SeoToolkit.Umbraco.ScriptManager.Core.Services { public class ScriptManagerService : IScriptManagerService { - private const string BaseCacheKey = "ScriptManager_"; - private readonly IScriptRepository _scriptRepository; + private readonly DistributedCache _distributedCache; private readonly IAppPolicyCache _cache; - public ScriptManagerService(IScriptRepository scriptRepository, AppCaches appCaches) + public ScriptManagerService(IScriptRepository scriptRepository, AppCaches appCaches, DistributedCache distributedCache) { _scriptRepository = scriptRepository; + _distributedCache = distributedCache; _cache = appCaches.RuntimeCache; } @@ -51,7 +53,7 @@ public void Delete(int[] ids) public IEnumerable