Skip to content

Hybrid Cache: Clear published content cache on content type change#21225

Merged
Zeegaan merged 2 commits intomainfrom
v17/bugfix/save-doc-type-breaks-document-rendering
Dec 22, 2025
Merged

Hybrid Cache: Clear published content cache on content type change#21225
Zeegaan merged 2 commits intomainfrom
v17/bugfix/save-doc-type-breaks-document-rendering

Conversation

@AndyButland
Copy link
Contributor

@AndyButland AndyButland commented Dec 20, 2025

Issue

I found this issue when testing the results of performance improvements for save of content types in #21207, but as it's a clear regression in 17 we should look to review and release it earlier.

To reproduce:

  • Have a rendered document using InMemoryAuto for models builder.
  • Save the document type the document is based on on the backoffice.
  • Refresh the rendered page and note a null reference exception error.
  • Clearing the memory cache or database cache doesn't help, it's only a restart that allows the page to render correctly again,

Cause

  • The root cause is that the _publishedContentCache (introduced in Performance: Cache published content instances at cache service level #20681) caches IPublishedContent objects which hold references to IPublishedContentType. When a content type is modified, these cached objects become stale.
  • The fix clears the entire _publishedContentCache at the end of the Rebuild method in both DocumentCacheService and MediaCacheService

Technical Details

The _publishedContentCache is a ConcurrentDictionary<string, IPublishedContent> that caches fully-constructed IPublishedContent objects for performance. However, these objects contain references to their IPublishedContentType, which becomes stale when the content type is modified.

A filtered/targeted clear approach was attempted but has inherent race conditions due to the non-atomic check-then-act pattern. I was also concerned that content types can be used in various places in a document (elements, compositions etc.). So the atomic Clear() looks to be the only reliable solution.

Test plan

  • Save a document type that has existing documents
  • Verify documents based on that type render correctly without restart

🤖 Generated with Claude Code with human updates.

Copilot AI review requested due to automatic review settings December 20, 2025 14:42
Copy link
Contributor

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 fixes an issue where saving a document type causes documents based on that type to show null model errors in Razor rendering. The root cause is that the _publishedContentCache caches IPublishedContent objects which hold references to IPublishedContentType. When a content type is modified, these cached objects become stale and must be cleared.

Key Changes

  • Adds _publishedContentCache.Clear() at the end of the Rebuild method in DocumentCacheService to clear stale published content when document types change
  • Adds the same cache clearing logic to MediaCacheService to handle media type changes

Reviewed changes

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

File Description
src/Umbraco.PublishedCache.HybridCache/Services/DocumentCacheService.cs Clears the entire published content cache after rebuilding when document types are modified
src/Umbraco.PublishedCache.HybridCache/Services/MediaCacheService.cs Clears the entire published content cache after rebuilding when media types are modified

Copy link
Member

@Zeegaan Zeegaan left a comment

Choose a reason for hiding this comment

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

This makes sense to me, we should really be looking into not having the _publishedContentCache, and instead entirely caching IPublishContent via the hybrid cache instead, this might cause som breaking changes, but effectively we're double caching published content, just to avoid having to rebuild from the factory every time, which is not ideal 🙈

@Zeegaan Zeegaan merged commit 396ebdd into main Dec 22, 2025
26 checks passed
@Zeegaan Zeegaan deleted the v17/bugfix/save-doc-type-breaks-document-rendering branch December 22, 2025 06:14
Zeegaan pushed a commit that referenced this pull request Dec 22, 2025
…1225)

* Clear the cacje level published content cache on content type change.

* Apply suggestions from code review

Co-authored-by: Copilot <[email protected]>

---------

Co-authored-by: Copilot <[email protected]>
(cherry picked from commit 396ebdd)
@Zeegaan
Copy link
Member

Zeegaan commented Dec 22, 2025

Cherry picked for 17.1 🍒

@AndyButland AndyButland changed the title HybridCache: Clear published content cache on content type change Hybrid Cache: Clear published content cache on content type change Dec 23, 2025
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