Skip to content

Commit

Permalink
V15: Implement not-implemented methods for media cache (#17524)
Browse files Browse the repository at this point in the history
* Implement not-implemented methods for media cache

* Fixed test

---------

Co-authored-by: Bjarke Berg <mail@bergmania.dk>
  • Loading branch information
Zeegaan and bergmania authored Nov 13, 2024
1 parent 8c025ec commit a9636d5
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/Umbraco.Core/PublishedCache/IMediaCacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ public interface IMediaCacheService
Task SeedAsync(CancellationToken cancellationToken);

void Rebuild(IReadOnlyCollection<int> contentTypeIds);
IEnumerable<IPublishedContent> GetByContentType(IPublishedContentType contentType);
}
34 changes: 19 additions & 15 deletions src/Umbraco.PublishedCache.HybridCache/DocumentCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IPublishedUrlProvider> _publishedUrlProvider;

public DocumentCache(
IDocumentCacheService documentCacheService,
IPublishedContentTypeCache publishedContentTypeCache,
IDocumentNavigationQueryService documentNavigationQueryService,
IDocumentUrlService documentUrlService,
Lazy<IPublishedUrlProvider> publishedUrlProvider)
{
_documentCacheService = documentCacheService;
_publishedContentTypeCache = publishedContentTypeCache;
_documentNavigationQueryService = documentNavigationQueryService;
_documentUrlService = documentUrlService;
_publishedUrlProvider = publishedUrlProvider;
}

public async Task<IPublishedContent?> GetByIdAsync(int id, bool? preview = null) => await _documentCacheService.GetByIdAsync(id, preview);
Expand Down Expand Up @@ -65,21 +76,17 @@ public DocumentCache(IDocumentCacheService documentCacheService, IPublishedConte
return GetById(guidUdi.Guid);
}

[Obsolete("Scheduled for removal, use IDocumentNavigationQueryService instead in v17")]
public IEnumerable<IPublishedContent> GetAtRoot(bool preview, string? culture = null)
{
IDocumentNavigationQueryService navigationService = StaticServiceProvider.Instance.GetRequiredService<IDocumentNavigationQueryService>();
navigationService.TryGetRootKeys(out IEnumerable<Guid> rootKeys);
_documentNavigationQueryService.TryGetRootKeys(out IEnumerable<Guid> rootKeys);

IEnumerable<IPublishedContent> 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<IPublishedContent> GetAtRoot(string? culture = null)
{
IDocumentNavigationQueryService navigationService = StaticServiceProvider.Instance.GetRequiredService<IDocumentNavigationQueryService>();
navigationService.TryGetRootKeys(out IEnumerable<Guid> rootKeys);
_documentNavigationQueryService.TryGetRootKeys(out IEnumerable<Guid> rootKeys);

IEnumerable<IPublishedContent> rootContent = rootKeys.Select(key => GetById(key)).WhereNotNull();
return culture is null ? rootContent : rootContent.Where(x => x.IsInvariantOrHasCulture(culture));
Expand All @@ -89,7 +96,7 @@ public IEnumerable<IPublishedContent> GetAtRoot(string? culture = null)
public bool HasContent(bool preview) => HasContent();

[Obsolete("Scheduled for removal in v17")]
public bool HasContent() => StaticServiceProvider.Instance.GetRequiredService<IDocumentUrlService>().HasAny();
public bool HasContent() => _documentUrlService.HasAny();

[Obsolete]
public IEnumerable<IPublishedContent> GetByContentType(IPublishedContentType contentType)
Expand All @@ -98,26 +105,23 @@ public IEnumerable<IPublishedContent> 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<IDocumentUrlService>();
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<IDocumentUrlService>();
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<IPublishedUrlProvider>();
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")]
Expand Down
58 changes: 47 additions & 11 deletions src/Umbraco.PublishedCache.HybridCache/MediaCache.cs
Original file line number Diff line number Diff line change
@@ -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<IPublishedContent?> GetByIdAsync(int id) => await _mediaCacheService.GetByIdAsync(id);
Expand All @@ -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<IPublishedContent> 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<IPublishedContent> GetAtRoot(string? culture = null) => throw new NotImplementedException();
return GetById(guidUdi.Guid);
}

public bool HasContent(bool preview) => throw new NotImplementedException();
public IEnumerable<IPublishedContent> GetAtRoot(bool preview, string? culture = null)
{
_mediaNavigationQueryService.TryGetRootKeys(out IEnumerable<Guid> rootKeys);

public bool HasContent() => throw new NotImplementedException();
IEnumerable<IPublishedContent> rootContent = rootKeys.Select(key => GetById(preview, key)).WhereNotNull();
return culture is null ? rootContent : rootContent.Where(x => x.IsInvariantOrHasCulture(culture));
}

public IEnumerable<IPublishedContent> GetAtRoot(string? culture = null)
{
_mediaNavigationQueryService.TryGetRootKeys(out IEnumerable<Guid> rootKeys);

IEnumerable<IPublishedContent> 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<Guid> rootKeys);
return rootKeys.Any();
}

public IEnumerable<IPublishedContent> GetByContentType(IPublishedContentType contentType) =>
throw new NotImplementedException();
_mediaCacheService.GetByContentType(contentType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,17 @@ public void Rebuild(IReadOnlyCollection<int> contentTypeIds)
scope.Complete();
}

public IEnumerable<IPublishedContent> GetByContentType(IPublishedContentType contentType)
{
using ICoreScope scope = _scopeProvider.CreateCoreScope();
IEnumerable<ContentCacheNode> 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,7 +108,11 @@ public void SetUp()
GetRequiredService<IPublishedModelFactory>(),
GetRequiredService<IPreviewService>());

_mockedCache = new DocumentCache(_mockDocumentCacheService, GetRequiredService<IPublishedContentTypeCache>());
_mockedCache = new DocumentCache(_mockDocumentCacheService,
GetRequiredService<IPublishedContentTypeCache>(),
GetRequiredService<IDocumentNavigationQueryService>(),
GetRequiredService<IDocumentUrlService>(),
new Lazy<IPublishedUrlProvider>(GetRequiredService<IPublishedUrlProvider>));
}

// We want to be able to alter the settings for the providers AFTER the test has started
Expand Down

0 comments on commit a9636d5

Please sign in to comment.