From 87aff770548d3d8b897e920e5b1b176ff2c05d10 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Mon, 11 Nov 2024 12:52:11 +0100 Subject: [PATCH] Explicit cache entry settings and expose cache settings in json schema (#17480) * Changed the cache entry settings to be explicit and exposed it in the schema file * commit of tests * Fix seed options --------- Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com> --- src/Umbraco.Core/Constants-Configuration.cs | 8 -------- .../UmbracoBuilder.Configuration.cs | 4 ---- src/Umbraco.Core/Models/CacheEntrySettings.cs | 1 + src/Umbraco.Core/Models/CacheSettings.cs | 9 +++++++++ .../Services/DocumentCacheService.cs | 14 +++++++------- .../Services/MediaCacheService.cs | 14 +++++++------- .../DocumentHybridCacheMockTests.cs | 4 +--- tools/Umbraco.JsonSchema/UmbracoCmsSchema.cs | 2 ++ 8 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/Umbraco.Core/Constants-Configuration.cs b/src/Umbraco.Core/Constants-Configuration.cs index a569110f01..60b3397eeb 100644 --- a/src/Umbraco.Core/Constants-Configuration.cs +++ b/src/Umbraco.Core/Constants-Configuration.cs @@ -66,7 +66,6 @@ public static partial class Constants public const string ConfigPackageManifests = ConfigPrefix + "PackageManifests"; public const string ConfigWebhook = ConfigPrefix + "Webhook"; public const string ConfigCache = ConfigPrefix + "Cache"; - public const string ConfigCacheEntry = ConfigCache + ":Entry"; public static class NamedOptions { @@ -80,13 +79,6 @@ public static partial class Constants public const string MemberTypes = "MemberTypes"; } - - public static class CacheEntry - { - public const string Document = "Document"; - - public const string Media = "Media"; - } } } } diff --git a/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.Configuration.cs b/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.Configuration.cs index f393a9cedb..f5aad4d0fb 100644 --- a/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.Configuration.cs +++ b/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.Configuration.cs @@ -104,10 +104,6 @@ public static partial class UmbracoBuilderExtensions builder.Services.Configure( Constants.Configuration.NamedOptions.InstallDefaultData.MemberTypes, builder.Config.GetSection($"{Constants.Configuration.ConfigInstallDefaultData}:{Constants.Configuration.NamedOptions.InstallDefaultData.MemberTypes}")); - builder.Services.Configure(Constants.Configuration.NamedOptions.CacheEntry.Media, - builder.Config.GetSection($"{Constants.Configuration.ConfigCacheEntry}:{Constants.Configuration.NamedOptions.CacheEntry.Media}")); - builder.Services.Configure(Constants.Configuration.NamedOptions.CacheEntry.Document, - builder.Config.GetSection($"{Constants.Configuration.ConfigCacheEntry}:{Constants.Configuration.NamedOptions.CacheEntry.Document}")); return builder; } diff --git a/src/Umbraco.Core/Models/CacheEntrySettings.cs b/src/Umbraco.Core/Models/CacheEntrySettings.cs index 21748b73f5..e0015505ab 100644 --- a/src/Umbraco.Core/Models/CacheEntrySettings.cs +++ b/src/Umbraco.Core/Models/CacheEntrySettings.cs @@ -16,4 +16,5 @@ public class CacheEntrySettings [DefaultValue(StaticSeedCacheDuration)] public TimeSpan SeedCacheDuration { get; set; } = TimeSpan.Parse(StaticSeedCacheDuration); + } diff --git a/src/Umbraco.Core/Models/CacheSettings.cs b/src/Umbraco.Core/Models/CacheSettings.cs index f478756f0c..e5c0d8827e 100644 --- a/src/Umbraco.Core/Models/CacheSettings.cs +++ b/src/Umbraco.Core/Models/CacheSettings.cs @@ -25,4 +25,13 @@ public class CacheSettings [Obsolete("Use Cache:Entry:Document:SeedCacheDuration instead")] [DefaultValue(StaticSeedCacheDuration)] public TimeSpan SeedCacheDuration { get; set; } = TimeSpan.Parse(StaticSeedCacheDuration); + + public CacheEntry Entry { get; set; } = new CacheEntry(); + + public class CacheEntry + { + public CacheEntrySettings Document { get; set; } = new CacheEntrySettings(); + + public CacheEntrySettings Media { get; set; } = new CacheEntrySettings(); + } } diff --git a/src/Umbraco.PublishedCache.HybridCache/Services/DocumentCacheService.cs b/src/Umbraco.PublishedCache.HybridCache/Services/DocumentCacheService.cs index dc7885ee38..e3bb827393 100644 --- a/src/Umbraco.PublishedCache.HybridCache/Services/DocumentCacheService.cs +++ b/src/Umbraco.PublishedCache.HybridCache/Services/DocumentCacheService.cs @@ -24,7 +24,7 @@ internal sealed class DocumentCacheService : IDocumentCacheService private readonly IEnumerable _seedKeyProviders; private readonly IPublishedModelFactory _publishedModelFactory; private readonly IPreviewService _previewService; - private readonly CacheEntrySettings _cacheEntrySettings; + private readonly CacheSettings _cacheSettings; private HashSet? _seedKeys; private HashSet SeedKeys { @@ -54,7 +54,7 @@ internal sealed class DocumentCacheService : IDocumentCacheService IPublishedContentFactory publishedContentFactory, ICacheNodeFactory cacheNodeFactory, IEnumerable seedKeyProviders, - IOptionsMonitor cacheEntrySettings, + IOptions cacheSettings, IPublishedModelFactory publishedModelFactory, IPreviewService previewService) { @@ -67,7 +67,7 @@ internal sealed class DocumentCacheService : IDocumentCacheService _seedKeyProviders = seedKeyProviders; _publishedModelFactory = publishedModelFactory; _previewService = previewService; - _cacheEntrySettings = cacheEntrySettings.Get(Constants.Configuration.NamedOptions.CacheEntry.Document); + _cacheSettings = cacheSettings.Value; } public async Task GetByKeyAsync(Guid key, bool? preview = null) @@ -221,8 +221,8 @@ internal sealed class DocumentCacheService : IDocumentCacheService private HybridCacheEntryOptions GetSeedEntryOptions() => new() { - Expiration = _cacheEntrySettings.SeedCacheDuration, - LocalCacheExpiration = _cacheEntrySettings.SeedCacheDuration + Expiration = _cacheSettings.Entry.Document.SeedCacheDuration, + LocalCacheExpiration = _cacheSettings.Entry.Document.SeedCacheDuration }; private HybridCacheEntryOptions GetEntryOptions(Guid key) @@ -234,8 +234,8 @@ internal sealed class DocumentCacheService : IDocumentCacheService return new HybridCacheEntryOptions { - Expiration = _cacheEntrySettings.RemoteCacheDuration, - LocalCacheExpiration = _cacheEntrySettings.LocalCacheDuration, + Expiration = _cacheSettings.Entry.Document.RemoteCacheDuration, + LocalCacheExpiration = _cacheSettings.Entry.Document.LocalCacheDuration, }; } diff --git a/src/Umbraco.PublishedCache.HybridCache/Services/MediaCacheService.cs b/src/Umbraco.PublishedCache.HybridCache/Services/MediaCacheService.cs index 12327489b8..5061c09386 100644 --- a/src/Umbraco.PublishedCache.HybridCache/Services/MediaCacheService.cs +++ b/src/Umbraco.PublishedCache.HybridCache/Services/MediaCacheService.cs @@ -23,7 +23,7 @@ internal class MediaCacheService : IMediaCacheService private readonly ICacheNodeFactory _cacheNodeFactory; private readonly IEnumerable _seedKeyProviders; private readonly IPublishedModelFactory _publishedModelFactory; - private readonly CacheEntrySettings _cacheEntrySettings; + private readonly CacheSettings _cacheSettings; private HashSet? _seedKeys; private HashSet SeedKeys @@ -55,7 +55,7 @@ internal class MediaCacheService : IMediaCacheService ICacheNodeFactory cacheNodeFactory, IEnumerable seedKeyProviders, IPublishedModelFactory publishedModelFactory, - IOptionsMonitor cacheEntrySettings) + IOptions cacheSettings) { _databaseCacheRepository = databaseCacheRepository; _idKeyMap = idKeyMap; @@ -65,7 +65,7 @@ internal class MediaCacheService : IMediaCacheService _cacheNodeFactory = cacheNodeFactory; _seedKeyProviders = seedKeyProviders; _publishedModelFactory = publishedModelFactory; - _cacheEntrySettings = cacheEntrySettings.Get(Constants.Configuration.NamedOptions.CacheEntry.Media); + _cacheSettings = cacheSettings.Value; } public async Task GetByKeyAsync(Guid key) @@ -268,16 +268,16 @@ internal class MediaCacheService : IMediaCacheService return new HybridCacheEntryOptions { - Expiration = _cacheEntrySettings.RemoteCacheDuration, - LocalCacheExpiration = _cacheEntrySettings.LocalCacheDuration, + Expiration = _cacheSettings.Entry.Media.RemoteCacheDuration, + LocalCacheExpiration = _cacheSettings.Entry.Media.LocalCacheDuration, }; } private HybridCacheEntryOptions GetSeedEntryOptions() => new() { - Expiration = _cacheEntrySettings.SeedCacheDuration, - LocalCacheExpiration = _cacheEntrySettings.SeedCacheDuration, + Expiration = _cacheSettings.SeedCacheDuration, + LocalCacheExpiration = _cacheSettings.SeedCacheDuration, }; private string GetCacheKey(Guid key, bool preview) => preview ? $"{key}+draft" : $"{key}"; diff --git a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheMockTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheMockTests.cs index 5c38d4e4b0..848643d03b 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheMockTests.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheMockTests.cs @@ -94,8 +94,6 @@ public class DocumentHybridCacheMockTests : UmbracoIntegrationTestWithContent }); _mockedNucacheRepository.Setup(r => r.DeleteContentItemAsync(It.IsAny())); - var optionsMonitorMock = new Mock>(); - optionsMonitorMock.Setup(x => x.Get(It.IsAny())).Returns(new CacheEntrySettings()); _mockDocumentCacheService = new DocumentCacheService( _mockedNucacheRepository.Object, @@ -105,7 +103,7 @@ public class DocumentHybridCacheMockTests : UmbracoIntegrationTestWithContent GetRequiredService(), GetRequiredService(), GetSeedProviders(), - optionsMonitorMock.Object, + new OptionsWrapper(new CacheSettings()), GetRequiredService(), GetRequiredService()); diff --git a/tools/Umbraco.JsonSchema/UmbracoCmsSchema.cs b/tools/Umbraco.JsonSchema/UmbracoCmsSchema.cs index ae54bd51da..7f02686ae6 100644 --- a/tools/Umbraco.JsonSchema/UmbracoCmsSchema.cs +++ b/tools/Umbraco.JsonSchema/UmbracoCmsSchema.cs @@ -1,5 +1,6 @@ using Umbraco.Cms.Core.Configuration; using Umbraco.Cms.Core.Configuration.Models; +using Umbraco.Cms.Core.Models; internal class UmbracoCmsSchema { @@ -79,5 +80,6 @@ internal class UmbracoCmsSchema public required MarketplaceSettings Marketplace { get; set; } public required WebhookSettings Webhook { get; set; } + public required CacheSettings Cache { get; set; } } }