Skip to content

Conversation

AndyButland
Copy link
Contributor

@AndyButland AndyButland commented Aug 25, 2025

Prerequisites

  • I have added steps to test this contribution in the description below

Relates to #18892

Description

We've had some discussion on the linked issue and internally about the potential performance concern of having to translate from the object stored into the hybrid cache and the IPublishedContent instance. It can be argued if this is likely to be a concern in typical page generation, but, as noted, it seems straightforward to at least request cache this.

As such I've applied that in this PR.

Given it's only cached for the request, we don't have to be concerned about cache invalidation.

Looking at a small site - the Umbraco starter kit - it does seem we can instantiate the same IPublishedContent multiple times in a single page request.

When this update is applied, loading a product page, which has the navigation bar, from the starter kit creates 9 IPublishedContent instances. Without the request cache, it creates 35.

And so this should have a positive, even if often small or even negligible, impact - and I can't see it can cause any harm.

Testing

Verify pages render from the cache as expected.
Using a breakpoint in PublishedContentFactory verify that the items are retrieved from the cache on all but the first time for a page request.

@Copilot Copilot AI review requested due to automatic review settings August 25, 2025 13:50
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements request-level caching for the PublishedContentFactory to improve performance by avoiding repeated instantiation of the same IPublishedContent objects within a single request. The cache reduces content instantiation from 35 to 9 instances when loading a product page in the Umbraco starter kit.

  • Adds request caching to ToIPublishedContent, ToIPublishedMedia, and ToPublishedMember methods
  • Introduces comprehensive integration tests to verify caching behavior
  • Adds XML documentation to improve code maintainability

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
src/Umbraco.PublishedCache.HybridCache/Factories/PublishedContentFactory.cs Implements request caching logic and adds comprehensive logging and documentation
src/Umbraco.PublishedCache.HybridCache/Factories/IPublishedContentFactory.cs Adds XML documentation for the interface methods
tests/Umbraco.Tests.Integration/Umbraco.PublishedCache.HybridCache/Factories/PublishedContentFactoryTests.cs New integration test file verifying caching behavior for all factory methods

AndyButland and others added 2 commits August 25, 2025 15:55
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@nikolajlauridsen
Copy link
Contributor

Looks good, tests good 👍 I Inverted the if statements to make the code more readable 😄

@nikolajlauridsen nikolajlauridsen enabled auto-merge (squash) August 27, 2025 10:43
@nikolajlauridsen nikolajlauridsen merged commit f0c5ecf into main Aug 27, 2025
24 of 25 checks passed
@nikolajlauridsen nikolajlauridsen deleted the v16/improvement/add-request-cache-for-published-content-factory branch August 27, 2025 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants