Add request caching around published content factory (#19990)
* Add request caching around published content factory. * Fixed ordering of log message parameters. Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Invert if to reduce nesting --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: mole <nikolajlauridsen@protonmail.ch>
This commit is contained in:
@@ -1,29 +1,61 @@
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Umbraco.Cms.Core.Cache;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Cms.Core.Models.PublishedContent;
|
||||
using Umbraco.Cms.Core.PublishedCache;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Infrastructure.HybridCache.Factories;
|
||||
|
||||
/// <summary>
|
||||
/// Defines a factory to create <see cref="IPublishedContent"/> and <see cref="IPublishedMember"/> from a <see cref="ContentCacheNode"/> or <see cref="IMember"/>.
|
||||
/// </summary>
|
||||
internal sealed class PublishedContentFactory : IPublishedContentFactory
|
||||
{
|
||||
private readonly IElementsCache _elementsCache;
|
||||
private readonly IVariationContextAccessor _variationContextAccessor;
|
||||
private readonly IPublishedContentTypeCache _publishedContentTypeCache;
|
||||
private readonly ILogger<PublishedContentFactory> _logger;
|
||||
private readonly AppCaches _appCaches;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PublishedContentFactory"/> class.
|
||||
/// </summary>
|
||||
public PublishedContentFactory(
|
||||
IElementsCache elementsCache,
|
||||
IVariationContextAccessor variationContextAccessor,
|
||||
IPublishedContentTypeCache publishedContentTypeCache)
|
||||
IPublishedContentTypeCache publishedContentTypeCache,
|
||||
ILogger<PublishedContentFactory> logger,
|
||||
AppCaches appCaches)
|
||||
{
|
||||
_elementsCache = elementsCache;
|
||||
_variationContextAccessor = variationContextAccessor;
|
||||
_publishedContentTypeCache = publishedContentTypeCache;
|
||||
_logger = logger;
|
||||
_appCaches = appCaches;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IPublishedContent? ToIPublishedContent(ContentCacheNode contentCacheNode, bool preview)
|
||||
{
|
||||
IPublishedContentType contentType = _publishedContentTypeCache.Get(PublishedItemType.Content, contentCacheNode.ContentTypeId);
|
||||
var cacheKey = $"{nameof(PublishedContentFactory)}DocumentCache_{contentCacheNode.Id}_{preview}";
|
||||
IPublishedContent? 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(
|
||||
"Creating IPublishedContent for document {ContentCacheNodeName} ({ContentCacheNodeId}).",
|
||||
contentCacheNode.Data?.Name ?? "No Name",
|
||||
contentCacheNode.Id);
|
||||
|
||||
IPublishedContentType contentType =
|
||||
_publishedContentTypeCache.Get(PublishedItemType.Content, contentCacheNode.ContentTypeId);
|
||||
var contentNode = new ContentNode(
|
||||
contentCacheNode.Id,
|
||||
contentCacheNode.Key,
|
||||
@@ -34,19 +66,42 @@ internal sealed class PublishedContentFactory : IPublishedContentFactory
|
||||
preview ? contentCacheNode.Data : null,
|
||||
preview ? null : contentCacheNode.Data);
|
||||
|
||||
IPublishedContent? model = GetModel(contentNode, preview);
|
||||
publishedContent = GetModel(contentNode, preview);
|
||||
|
||||
if (preview)
|
||||
{
|
||||
return model ?? GetPublishedContentAsDraft(model);
|
||||
publishedContent ??= GetPublishedContentAsDraft(publishedContent);
|
||||
}
|
||||
|
||||
return model;
|
||||
if (publishedContent is not null)
|
||||
{
|
||||
_appCaches.RequestCache.Set(cacheKey, publishedContent);
|
||||
}
|
||||
|
||||
return publishedContent;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IPublishedContent? ToIPublishedMedia(ContentCacheNode contentCacheNode)
|
||||
{
|
||||
IPublishedContentType contentType = _publishedContentTypeCache.Get(PublishedItemType.Media, contentCacheNode.ContentTypeId);
|
||||
var cacheKey = $"{nameof(PublishedContentFactory)}MediaCache_{contentCacheNode.Id}";
|
||||
IPublishedContent? 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(
|
||||
"Creating IPublishedContent for media {ContentCacheNodeName} ({ContentCacheNodeId}).",
|
||||
contentCacheNode.Data?.Name ?? "No Name",
|
||||
contentCacheNode.Id);
|
||||
|
||||
IPublishedContentType contentType =
|
||||
_publishedContentTypeCache.Get(PublishedItemType.Media, contentCacheNode.ContentTypeId);
|
||||
var contentNode = new ContentNode(
|
||||
contentCacheNode.Id,
|
||||
contentCacheNode.Key,
|
||||
@@ -57,14 +112,40 @@ internal sealed class PublishedContentFactory : IPublishedContentFactory
|
||||
null,
|
||||
contentCacheNode.Data);
|
||||
|
||||
return GetModel(contentNode, false);
|
||||
publishedContent = GetModel(contentNode, false);
|
||||
|
||||
if (publishedContent is not null)
|
||||
{
|
||||
_appCaches.RequestCache.Set(cacheKey, publishedContent);
|
||||
}
|
||||
|
||||
return publishedContent;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IPublishedMember ToPublishedMember(IMember member)
|
||||
{
|
||||
IPublishedContentType contentType = _publishedContentTypeCache.Get(PublishedItemType.Member, member.ContentTypeId);
|
||||
string cacheKey = $"{nameof(PublishedContentFactory)}MemberCache_{member.Id}";
|
||||
IPublishedMember? publishedMember = _appCaches.RequestCache.GetCacheItem<IPublishedMember?>(cacheKey);
|
||||
if (publishedMember is not null)
|
||||
{
|
||||
_logger.LogDebug(
|
||||
"Using cached IPublishedMember for member {MemberName} ({MemberId}).",
|
||||
member.Username,
|
||||
member.Id);
|
||||
|
||||
// Members are only "mapped" never cached, so these default values are a bit wierd, but they are not used.
|
||||
return publishedMember;
|
||||
}
|
||||
|
||||
_logger.LogDebug(
|
||||
"Creating IPublishedMember for member {MemberName} ({MemberId}).",
|
||||
member.Username,
|
||||
member.Id);
|
||||
|
||||
IPublishedContentType contentType =
|
||||
_publishedContentTypeCache.Get(PublishedItemType.Member, member.ContentTypeId);
|
||||
|
||||
// Members are only "mapped" never cached, so these default values are a bit weird, but they are not used.
|
||||
var contentData = new ContentData(
|
||||
member.Name,
|
||||
null,
|
||||
@@ -85,7 +166,11 @@ internal sealed class PublishedContentFactory : IPublishedContentFactory
|
||||
contentType,
|
||||
null,
|
||||
contentData);
|
||||
return new PublishedMember(member, contentNode, _elementsCache, _variationContextAccessor);
|
||||
publishedMember = new PublishedMember(member, contentNode, _elementsCache, _variationContextAccessor);
|
||||
|
||||
_appCaches.RequestCache.Set(cacheKey, publishedMember);
|
||||
|
||||
return publishedMember;
|
||||
}
|
||||
|
||||
private static Dictionary<string, PropertyData[]> GetPropertyValues(IPublishedContentType contentType, IMember member)
|
||||
@@ -134,7 +219,6 @@ internal sealed class PublishedContentFactory : IPublishedContentFactory
|
||||
_variationContextAccessor);
|
||||
}
|
||||
|
||||
|
||||
private static IPublishedContent? GetPublishedContentAsDraft(IPublishedContent? content) =>
|
||||
content == null ? null :
|
||||
// an object in the cache is either an IPublishedContentOrMedia,
|
||||
@@ -149,7 +233,7 @@ internal sealed class PublishedContentFactory : IPublishedContentFactory
|
||||
content = wrapped.Unwrap();
|
||||
}
|
||||
|
||||
if (!(content is PublishedContent inner))
|
||||
if (content is not PublishedContent inner)
|
||||
{
|
||||
throw new InvalidOperationException("Innermost content is not PublishedContent.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user