-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add request caching around published content factory #19990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nikolajlauridsen
merged 3 commits into
main
from
v16/improvement/add-request-cache-for-published-content-factory
Aug 27, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 14 additions & 1 deletion
15
src/Umbraco.PublishedCache.HybridCache/Factories/IPublishedContentFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,25 @@ | ||
| using Umbraco.Cms.Core.Models; | ||
| using Umbraco.Cms.Core.Models; | ||
| using Umbraco.Cms.Core.Models.PublishedContent; | ||
|
|
||
| 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 interface IPublishedContentFactory | ||
| { | ||
| /// <summary> | ||
| /// Converts a <see cref="ContentCacheNode"/> to an <see cref="IPublishedContent"/> if document type. | ||
| /// </summary> | ||
| IPublishedContent? ToIPublishedContent(ContentCacheNode contentCacheNode, bool preview); | ||
|
|
||
| /// <summary> | ||
| /// Converts a <see cref="ContentCacheNode"/> to an <see cref="IPublishedContent"/> of media type. | ||
| /// </summary> | ||
| IPublishedContent? ToIPublishedMedia(ContentCacheNode contentCacheNode); | ||
|
|
||
| /// <summary> | ||
| /// Converts a <see cref="IMember"/> to an <see cref="IPublishedMember"/>. | ||
| /// </summary> | ||
| IPublishedMember ToPublishedMember(IMember member); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
....Integration/Umbraco.PublishedCache.HybridCache/Factories/PublishedContentFactoryTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| using NUnit.Framework; | ||
| using Umbraco.Cms.Core; | ||
| using Umbraco.Cms.Core.Cache; | ||
| using Umbraco.Cms.Core.Models.PublishedContent; | ||
| using Umbraco.Cms.Core.Services; | ||
| using Umbraco.Cms.Infrastructure.HybridCache; | ||
| using Umbraco.Cms.Infrastructure.HybridCache.Factories; | ||
| using Umbraco.Cms.Tests.Common.Builders; | ||
| using Umbraco.Cms.Tests.Common.Builders.Extensions; | ||
| using Umbraco.Cms.Tests.Common.Testing; | ||
| using Umbraco.Cms.Tests.Integration.Testing; | ||
|
|
||
| namespace Umbraco.Cms.Tests.Integration.Umbraco.PublishedCache.HybridCache; | ||
|
|
||
| [TestFixture] | ||
| [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] | ||
| internal sealed class PublishedContentFactoryTests : UmbracoIntegrationTestWithContent | ||
| { | ||
| private IPublishedContentFactory PublishedContentFactory => GetRequiredService<IPublishedContentFactory>(); | ||
|
|
||
| private IPublishedValueFallback PublishedValueFallback => GetRequiredService<IPublishedValueFallback>(); | ||
|
|
||
| private IMediaService MediaService => GetRequiredService<IMediaService>(); | ||
|
|
||
| private IMediaTypeService MediaTypeService => GetRequiredService<IMediaTypeService>(); | ||
|
|
||
| private IMemberService MemberService => GetRequiredService<IMemberService>(); | ||
|
|
||
| private IMemberTypeService MemberTypeService => GetRequiredService<IMemberTypeService>(); | ||
|
|
||
| protected override void CustomTestSetup(IUmbracoBuilder builder) | ||
| { | ||
| var requestCache = new DictionaryAppCache(); | ||
| var appCaches = new AppCaches( | ||
| NoAppCache.Instance, | ||
| requestCache, | ||
| new IsolatedCaches(type => NoAppCache.Instance)); | ||
| builder.Services.AddUnique(appCaches); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Can_Create_Published_Content_For_Document() | ||
| { | ||
| var contentCacheNode = new ContentCacheNode | ||
| { | ||
| Id = Textpage.Id, | ||
| Key = Textpage.Key, | ||
| ContentTypeId = Textpage.ContentType.Id, | ||
| CreateDate = Textpage.CreateDate, | ||
| CreatorId = Textpage.CreatorId, | ||
| SortOrder = Textpage.SortOrder, | ||
| Data = new ContentData( | ||
| Textpage.Name, | ||
| "text-page", | ||
| Textpage.VersionId, | ||
| Textpage.UpdateDate, | ||
| Textpage.WriterId, | ||
| Textpage.TemplateId, | ||
| true, | ||
| new Dictionary<string, PropertyData[]> | ||
| { | ||
| { | ||
| "title", new[] | ||
| { | ||
| new PropertyData | ||
| { | ||
| Value = "Test title", | ||
| Culture = string.Empty, | ||
| Segment = string.Empty, | ||
| }, | ||
| } | ||
| }, | ||
| }, | ||
| null), | ||
| }; | ||
| var result = PublishedContentFactory.ToIPublishedContent(contentCacheNode, false); | ||
| Assert.IsNotNull(result); | ||
| Assert.AreEqual(Textpage.Id, result.Id); | ||
| Assert.AreEqual(Textpage.Name, result.Name); | ||
| Assert.AreEqual("Test title", result.Properties.Single(x => x.Alias == "title").Value<string>(PublishedValueFallback)); | ||
|
|
||
| // Verify that requesting the same content again returns the same instance (from request cache). | ||
| var result2 = PublishedContentFactory.ToIPublishedContent(contentCacheNode, false); | ||
| Assert.AreSame(result, result2); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Can_Create_Published_Content_For_Media() | ||
| { | ||
| var mediaType = new MediaTypeBuilder().Build(); | ||
| mediaType.AllowedAsRoot = true; | ||
| await MediaTypeService.CreateAsync(mediaType, Constants.Security.SuperUserKey); | ||
|
|
||
| var media = new MediaBuilder() | ||
| .WithMediaType(mediaType) | ||
| .WithName("Media 1") | ||
| .Build(); | ||
| MediaService.Save(media); | ||
|
|
||
| var contentCacheNode = new ContentCacheNode | ||
| { | ||
| Id = media.Id, | ||
| Key = media.Key, | ||
| ContentTypeId = media.ContentType.Id, | ||
| Data = new ContentData( | ||
| media.Name, | ||
| null, | ||
| 0, | ||
| media.UpdateDate, | ||
| media.WriterId, | ||
| null, | ||
| false, | ||
| new Dictionary<string, PropertyData[]>(), | ||
| null), | ||
| }; | ||
| var result = PublishedContentFactory.ToIPublishedMedia(contentCacheNode); | ||
| Assert.IsNotNull(result); | ||
| Assert.AreEqual(media.Id, result.Id); | ||
| Assert.AreEqual(media.Name, result.Name); | ||
|
|
||
| // Verify that requesting the same content again returns the same instance (from request cache). | ||
| var result2 = PublishedContentFactory.ToIPublishedMedia(contentCacheNode); | ||
| Assert.AreSame(result, result2); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Can_Create_Published_Member_For_Member() | ||
| { | ||
| var memberType = new MemberTypeBuilder().Build(); | ||
| await MemberTypeService.CreateAsync(memberType, Constants.Security.SuperUserKey); | ||
|
|
||
| var member = new MemberBuilder() | ||
| .WithMemberType(memberType) | ||
| .WithName("Member 1") | ||
| .Build(); | ||
| MemberService.Save(member); | ||
|
|
||
| var result = PublishedContentFactory.ToPublishedMember(member); | ||
| Assert.IsNotNull(result); | ||
| Assert.AreEqual(member.Id, result.Id); | ||
| Assert.AreEqual(member.Name, result.Name); | ||
|
|
||
| // Verify that requesting the same content again returns the same instance (from request cache). | ||
| var result2 = PublishedContentFactory.ToPublishedMember(member); | ||
| Assert.AreSame(result, result2); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.