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.
This commit is contained in:
@@ -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<IPublishedContent?>(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<IPublishedContent?>(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<IPublishedContent?>(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<IPublishedContent?>(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<IPublishedMember?>(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<IPublishedMember?>(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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user