From 91ce5180639fb0945d0a05d998942845601cce22 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Thu, 4 Sep 2025 10:13:48 +0200 Subject: [PATCH] Verify that the request cache is available before using it in PublishedContentFactory (#20062) * Verify that the request cache is available before using it in PublishedContentFactory. * Initialized variables as null. --- .../Factories/PublishedContentFactory.cs | 63 ++++++++++++------- .../Factories/PublishedContentFactoryTests.cs | 6 +- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/Umbraco.PublishedCache.HybridCache/Factories/PublishedContentFactory.cs b/src/Umbraco.PublishedCache.HybridCache/Factories/PublishedContentFactory.cs index 5f33705716..68f4d8d80f 100644 --- a/src/Umbraco.PublishedCache.HybridCache/Factories/PublishedContentFactory.cs +++ b/src/Umbraco.PublishedCache.HybridCache/Factories/PublishedContentFactory.cs @@ -39,14 +39,18 @@ internal sealed class PublishedContentFactory : IPublishedContentFactory public IPublishedContent? ToIPublishedContent(ContentCacheNode contentCacheNode, bool preview) { var cacheKey = $"{nameof(PublishedContentFactory)}DocumentCache_{contentCacheNode.Id}_{preview}"; - IPublishedContent? publishedContent = _appCaches.RequestCache.GetCacheItem(cacheKey); - if (publishedContent is not null) + IPublishedContent? publishedContent = null; + if (_appCaches.RequestCache.IsAvailable) { - _logger.LogDebug( - "Using cached IPublishedContent for document {ContentCacheNodeName} ({ContentCacheNodeId}).", - contentCacheNode.Data?.Name ?? "No Name", - contentCacheNode.Id); - return publishedContent; + publishedContent = _appCaches.RequestCache.GetCacheItem(cacheKey); + if (publishedContent is not null) + { + _logger.LogDebug( + "Using cached IPublishedContent for document {ContentCacheNodeName} ({ContentCacheNodeId}).", + contentCacheNode.Data?.Name ?? "No Name", + contentCacheNode.Id); + return publishedContent; + } } _logger.LogDebug( @@ -73,7 +77,7 @@ internal sealed class PublishedContentFactory : IPublishedContentFactory publishedContent ??= GetPublishedContentAsDraft(publishedContent); } - if (publishedContent is not null) + if (_appCaches.RequestCache.IsAvailable && publishedContent is not null) { _appCaches.RequestCache.Set(cacheKey, publishedContent); } @@ -85,14 +89,18 @@ internal sealed class PublishedContentFactory : IPublishedContentFactory public IPublishedContent? ToIPublishedMedia(ContentCacheNode contentCacheNode) { var cacheKey = $"{nameof(PublishedContentFactory)}MediaCache_{contentCacheNode.Id}"; - IPublishedContent? publishedContent = _appCaches.RequestCache.GetCacheItem(cacheKey); - if (publishedContent is not null) + IPublishedContent? publishedContent = null; + if (_appCaches.RequestCache.IsAvailable) { - _logger.LogDebug( - "Using cached IPublishedContent for media {ContentCacheNodeName} ({ContentCacheNodeId}).", - contentCacheNode.Data?.Name ?? "No Name", - contentCacheNode.Id); - return publishedContent; + publishedContent = _appCaches.RequestCache.GetCacheItem(cacheKey); + if (publishedContent is not null) + { + _logger.LogDebug( + "Using cached IPublishedContent for media {ContentCacheNodeName} ({ContentCacheNodeId}).", + contentCacheNode.Data?.Name ?? "No Name", + contentCacheNode.Id); + return publishedContent; + } } _logger.LogDebug( @@ -114,7 +122,7 @@ internal sealed class PublishedContentFactory : IPublishedContentFactory publishedContent = GetModel(contentNode, false); - if (publishedContent is not null) + if (_appCaches.RequestCache.IsAvailable && publishedContent is not null) { _appCaches.RequestCache.Set(cacheKey, publishedContent); } @@ -126,15 +134,19 @@ internal sealed class PublishedContentFactory : IPublishedContentFactory public IPublishedMember ToPublishedMember(IMember member) { string cacheKey = $"{nameof(PublishedContentFactory)}MemberCache_{member.Id}"; - IPublishedMember? publishedMember = _appCaches.RequestCache.GetCacheItem(cacheKey); - if (publishedMember is not null) + IPublishedMember? publishedMember = null; + if (_appCaches.RequestCache.IsAvailable) { - _logger.LogDebug( - "Using cached IPublishedMember for member {MemberName} ({MemberId}).", - member.Username, - member.Id); + publishedMember = _appCaches.RequestCache.GetCacheItem(cacheKey); + if (publishedMember is not null) + { + _logger.LogDebug( + "Using cached IPublishedMember for member {MemberName} ({MemberId}).", + member.Username, + member.Id); - return publishedMember; + return publishedMember; + } } _logger.LogDebug( @@ -168,7 +180,10 @@ internal sealed class PublishedContentFactory : IPublishedContentFactory contentData); publishedMember = new PublishedMember(member, contentNode, _elementsCache, _variationContextAccessor); - _appCaches.RequestCache.Set(cacheKey, publishedMember); + if (_appCaches.RequestCache.IsAvailable) + { + _appCaches.RequestCache.Set(cacheKey, publishedMember); + } return publishedMember; } diff --git a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/Factories/PublishedContentFactoryTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/Factories/PublishedContentFactoryTests.cs index 3d7be44d69..f9f604658c 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/Factories/PublishedContentFactoryTests.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/Factories/PublishedContentFactoryTests.cs @@ -32,9 +32,9 @@ internal sealed class PublishedContentFactoryTests : UmbracoIntegrationTestWithC { var requestCache = new DictionaryAppCache(); var appCaches = new AppCaches( - NoAppCache.Instance, - requestCache, - new IsolatedCaches(type => NoAppCache.Instance)); + NoAppCache.Instance, + requestCache, + new IsolatedCaches(type => NoAppCache.Instance)); builder.Services.AddUnique(appCaches); }