From a9636d542961541d4bb225b21c25e8bc3dbb64d9 Mon Sep 17 00:00:00 2001 From: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com> Date: Wed, 13 Nov 2024 12:04:27 +0100 Subject: [PATCH] V15: Implement not-implemented methods for media cache (#17524) * Implement not-implemented methods for media cache * Fixed test --------- Co-authored-by: Bjarke Berg --- .../PublishedCache/IMediaCacheService.cs | 1 + .../DocumentCache.cs | 34 ++++++----- .../MediaCache.cs | 58 +++++++++++++++---- .../Services/MediaCacheService.cs | 11 ++++ .../DocumentHybridCacheMockTests.cs | 7 ++- 5 files changed, 84 insertions(+), 27 deletions(-) diff --git a/src/Umbraco.Core/PublishedCache/IMediaCacheService.cs b/src/Umbraco.Core/PublishedCache/IMediaCacheService.cs index 13d654676cba..b13a4a4aac89 100644 --- a/src/Umbraco.Core/PublishedCache/IMediaCacheService.cs +++ b/src/Umbraco.Core/PublishedCache/IMediaCacheService.cs @@ -26,4 +26,5 @@ public interface IMediaCacheService Task SeedAsync(CancellationToken cancellationToken); void Rebuild(IReadOnlyCollection contentTypeIds); + IEnumerable GetByContentType(IPublishedContentType contentType); } diff --git a/src/Umbraco.PublishedCache.HybridCache/DocumentCache.cs b/src/Umbraco.PublishedCache.HybridCache/DocumentCache.cs index aab97f9b3cff..d5c173bba94f 100644 --- a/src/Umbraco.PublishedCache.HybridCache/DocumentCache.cs +++ b/src/Umbraco.PublishedCache.HybridCache/DocumentCache.cs @@ -14,11 +14,22 @@ public sealed class DocumentCache : IPublishedContentCache { private readonly IDocumentCacheService _documentCacheService; private readonly IPublishedContentTypeCache _publishedContentTypeCache; - - public DocumentCache(IDocumentCacheService documentCacheService, IPublishedContentTypeCache publishedContentTypeCache) + private readonly IDocumentNavigationQueryService _documentNavigationQueryService; + private readonly IDocumentUrlService _documentUrlService; + private readonly Lazy _publishedUrlProvider; + + public DocumentCache( + IDocumentCacheService documentCacheService, + IPublishedContentTypeCache publishedContentTypeCache, + IDocumentNavigationQueryService documentNavigationQueryService, + IDocumentUrlService documentUrlService, + Lazy publishedUrlProvider) { _documentCacheService = documentCacheService; _publishedContentTypeCache = publishedContentTypeCache; + _documentNavigationQueryService = documentNavigationQueryService; + _documentUrlService = documentUrlService; + _publishedUrlProvider = publishedUrlProvider; } public async Task GetByIdAsync(int id, bool? preview = null) => await _documentCacheService.GetByIdAsync(id, preview); @@ -65,21 +76,17 @@ public DocumentCache(IDocumentCacheService documentCacheService, IPublishedConte return GetById(guidUdi.Guid); } - [Obsolete("Scheduled for removal, use IDocumentNavigationQueryService instead in v17")] public IEnumerable GetAtRoot(bool preview, string? culture = null) { - IDocumentNavigationQueryService navigationService = StaticServiceProvider.Instance.GetRequiredService(); - navigationService.TryGetRootKeys(out IEnumerable rootKeys); + _documentNavigationQueryService.TryGetRootKeys(out IEnumerable rootKeys); IEnumerable rootContent = rootKeys.Select(key => GetById(preview, key)).WhereNotNull(); return culture is null ? rootContent : rootContent.Where(x => x.IsInvariantOrHasCulture(culture)); } - [Obsolete("Scheduled for removal, use IDocumentNavigationQueryService instead in v17")] public IEnumerable GetAtRoot(string? culture = null) { - IDocumentNavigationQueryService navigationService = StaticServiceProvider.Instance.GetRequiredService(); - navigationService.TryGetRootKeys(out IEnumerable rootKeys); + _documentNavigationQueryService.TryGetRootKeys(out IEnumerable rootKeys); IEnumerable rootContent = rootKeys.Select(key => GetById(key)).WhereNotNull(); return culture is null ? rootContent : rootContent.Where(x => x.IsInvariantOrHasCulture(culture)); @@ -89,7 +96,7 @@ public IEnumerable GetAtRoot(string? culture = null) public bool HasContent(bool preview) => HasContent(); [Obsolete("Scheduled for removal in v17")] - public bool HasContent() => StaticServiceProvider.Instance.GetRequiredService().HasAny(); + public bool HasContent() => _documentUrlService.HasAny(); [Obsolete] public IEnumerable GetByContentType(IPublishedContentType contentType) @@ -98,26 +105,23 @@ public IEnumerable GetByContentType(IPublishedContentType con [Obsolete("Use IPublishedUrlProvider.GetUrl instead, scheduled for removal in v17")] public IPublishedContent? GetByRoute(bool preview, string route, bool? hideTopLevelNode = null, string? culture = null) { - IDocumentUrlService documentUrlService = StaticServiceProvider.Instance.GetRequiredService(); - Guid? key = documentUrlService.GetDocumentKeyByRoute(route, culture, null, preview); + Guid? key = _documentUrlService.GetDocumentKeyByRoute(route, culture, null, preview); return key is not null ? GetById(preview, key.Value) : null; } [Obsolete("Use IPublishedUrlProvider.GetUrl instead, scheduled for removal in v17")] public IPublishedContent? GetByRoute(string route, bool? hideTopLevelNode = null, string? culture = null) { - IDocumentUrlService documentUrlService = StaticServiceProvider.Instance.GetRequiredService(); - Guid? key = documentUrlService.GetDocumentKeyByRoute(route, culture, null, false); + Guid? key = _documentUrlService.GetDocumentKeyByRoute(route, culture, null, false); return key is not null ? GetById(key.Value) : null; } [Obsolete("Use IPublishedUrlProvider.GetUrl instead, scheduled for removal in v17")] public string? GetRouteById(bool preview, int contentId, string? culture = null) { - IPublishedUrlProvider publishedUrlProvider = StaticServiceProvider.Instance.GetRequiredService(); IPublishedContent? content = GetById(preview, contentId); - return content is not null ? publishedUrlProvider.GetUrl(content, UrlMode.Relative, culture) : null; + return content is not null ? _publishedUrlProvider.Value.GetUrl(content, UrlMode.Relative, culture) : null; } [Obsolete("Use IPublishedUrlProvider.GetUrl instead, scheduled for removal in v17")] diff --git a/src/Umbraco.PublishedCache.HybridCache/MediaCache.cs b/src/Umbraco.PublishedCache.HybridCache/MediaCache.cs index 7afcde8fb346..4cb70638d1a4 100644 --- a/src/Umbraco.PublishedCache.HybridCache/MediaCache.cs +++ b/src/Umbraco.PublishedCache.HybridCache/MediaCache.cs @@ -1,19 +1,22 @@ using Umbraco.Cms.Core; using Umbraco.Cms.Core.Models.PublishedContent; using Umbraco.Cms.Core.PublishedCache; -using Umbraco.Cms.Infrastructure.HybridCache.Services; +using Umbraco.Cms.Core.Services.Navigation; +using Umbraco.Extensions; namespace Umbraco.Cms.Infrastructure.HybridCache; -public class MediaCache : IPublishedMediaCache +public sealed class MediaCache : IPublishedMediaCache { private readonly IMediaCacheService _mediaCacheService; private readonly IPublishedContentTypeCache _publishedContentTypeCache; + private readonly IMediaNavigationQueryService _mediaNavigationQueryService; - public MediaCache(IMediaCacheService mediaCacheService, IPublishedContentTypeCache publishedContentTypeCache) + public MediaCache(IMediaCacheService mediaCacheService, IPublishedContentTypeCache publishedContentTypeCache, IMediaNavigationQueryService mediaNavigationQueryService) { _mediaCacheService = mediaCacheService; _publishedContentTypeCache = publishedContentTypeCache; + _mediaNavigationQueryService = mediaNavigationQueryService; } public async Task GetByIdAsync(int id) => await _mediaCacheService.GetByIdAsync(id); @@ -37,20 +40,53 @@ public MediaCache(IMediaCacheService mediaCacheService, IPublishedContentTypeCac public IPublishedContentType GetContentType(string alias) => _publishedContentTypeCache.Get(PublishedItemType.Media, alias); - // FIXME - these need to be removed when removing nucache - public IPublishedContent? GetById(bool preview, Udi contentId) => throw new NotImplementedException(); + [Obsolete("Scheduled for removal in v17")] + public IPublishedContent? GetById(bool preview, Udi contentId) + { + if(contentId is not GuidUdi guidUdi) + { + throw new NotSupportedException("Only GuidUdi is supported"); + } - public IPublishedContent? GetById(Udi contentId) => throw new NotImplementedException(); + return GetById(preview, guidUdi.Guid); + } - public IEnumerable GetAtRoot(bool preview, string? culture = null) => throw new NotImplementedException(); + [Obsolete("Scheduled for removal in v17")] + public IPublishedContent? GetById(Udi contentId) + { + if(contentId is not GuidUdi guidUdi) + { + throw new NotSupportedException("Only GuidUdi is supported"); + } - public IEnumerable GetAtRoot(string? culture = null) => throw new NotImplementedException(); + return GetById(guidUdi.Guid); + } - public bool HasContent(bool preview) => throw new NotImplementedException(); + public IEnumerable GetAtRoot(bool preview, string? culture = null) + { + _mediaNavigationQueryService.TryGetRootKeys(out IEnumerable rootKeys); - public bool HasContent() => throw new NotImplementedException(); + IEnumerable rootContent = rootKeys.Select(key => GetById(preview, key)).WhereNotNull(); + return culture is null ? rootContent : rootContent.Where(x => x.IsInvariantOrHasCulture(culture)); + } + public IEnumerable GetAtRoot(string? culture = null) + { + _mediaNavigationQueryService.TryGetRootKeys(out IEnumerable rootKeys); + + IEnumerable rootContent = rootKeys.Select(key => GetById(key)).WhereNotNull(); + return culture is null ? rootContent : rootContent.Where(x => x.IsInvariantOrHasCulture(culture)); + } + + [Obsolete("Media does not support preview, this method will be removed in future versions")] + public bool HasContent(bool preview) => HasContent(); + + public bool HasContent() + { + _mediaNavigationQueryService.TryGetRootKeys(out IEnumerable rootKeys); + return rootKeys.Any(); + } public IEnumerable GetByContentType(IPublishedContentType contentType) => - throw new NotImplementedException(); + _mediaCacheService.GetByContentType(contentType); } diff --git a/src/Umbraco.PublishedCache.HybridCache/Services/MediaCacheService.cs b/src/Umbraco.PublishedCache.HybridCache/Services/MediaCacheService.cs index 5061c0938629..af5396262e9e 100644 --- a/src/Umbraco.PublishedCache.HybridCache/Services/MediaCacheService.cs +++ b/src/Umbraco.PublishedCache.HybridCache/Services/MediaCacheService.cs @@ -259,6 +259,17 @@ public void Rebuild(IReadOnlyCollection contentTypeIds) scope.Complete(); } + public IEnumerable GetByContentType(IPublishedContentType contentType) + { + using ICoreScope scope = _scopeProvider.CreateCoreScope(); + IEnumerable nodes = _databaseCacheRepository.GetContentByContentTypeKey([contentType.Key], ContentCacheDataSerializerEntityType.Media); + scope.Complete(); + + return nodes + .Select(x => _publishedContentFactory.ToIPublishedContent(x, x.IsDraft).CreateModel(_publishedModelFactory)) + .WhereNotNull(); + } + private HybridCacheEntryOptions GetEntryOptions(Guid key) { if (SeedKeys.Contains(key)) diff --git a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheMockTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheMockTests.cs index 848643d03b64..065e2db3cd7a 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheMockTests.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/DocumentHybridCacheMockTests.cs @@ -6,6 +6,7 @@ using Umbraco.Cms.Core.Models.ContentPublishing; using Umbraco.Cms.Core.Models.PublishedContent; using Umbraco.Cms.Core.PublishedCache; +using Umbraco.Cms.Core.Routing; using Umbraco.Cms.Core.Scoping; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Core.Services.Navigation; @@ -107,7 +108,11 @@ public void SetUp() GetRequiredService(), GetRequiredService()); - _mockedCache = new DocumentCache(_mockDocumentCacheService, GetRequiredService()); + _mockedCache = new DocumentCache(_mockDocumentCacheService, + GetRequiredService(), + GetRequiredService(), + GetRequiredService(), + new Lazy(GetRequiredService)); } // We want to be able to alter the settings for the providers AFTER the test has started