Skip to content

Conversation

@nikolajlauridsen
Copy link
Contributor

Adds a VersionChanged method to IRepositoryCacheVersionAccessor that clears the request cache for the specific key when registering a cache version change.

Problem

Previously, certain operations would erroneously detect the cache as out of date. For example, when removing a content type:

  1. Get a content type from the cache, caching its version in the accessor
  2. Delete the content type, changing the cache version but not the cached version in the accessor
  3. Trigger a memory cache update, which requests content types
  4. Check the content type cache version against the cached version, which is now stale
  5. Register a false cache mismatch and unnecessarily roll forward the cache

This change prevents this false positive by clearing the cached version when the actual version changes.

Testing

Testing this requires some setup:

Add the following to Serilog:MinimumLevel:Override in your appsettings.json:

"Umbraco.Cms.Core.Cache": "Debug"

Update the builder in Program.cs:

builder.CreateUmbracoBuilder()
    .AddBackOffice()
    .AddWebsite()
#if UseDeliveryApi
    .AddDeliveryApi()
#endif
    .AddComposers()
    .LoadBalanceIsolatedCaches()
    .Build();

Before this change: Deleting a content type would log Cache for {EntityType} is not synced: local version {LocalVersion} does not match database version {DatabaseVersion} – this should never occur in a single server setup.

After this change: This erroneous log message no longer appears.

Copilot AI review requested due to automatic review settings November 14, 2025 09:26
Copilot finished reviewing on behalf of nikolajlauridsen November 14, 2025 09:28
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 addresses a cache synchronization issue in load-balanced environments where the request cache could contain stale cache version information, leading to false positive cache mismatch detections. The fix introduces a VersionChanged method to clear the request cache entry when cache versions are updated.

Key changes:

  • Added VersionChanged method to IRepositoryCacheVersionAccessor interface with a default no-op implementation
  • Implemented VersionChanged in RepositoryCacheVersionAccessor to remove the stale cache entry from request cache
  • Integrated the new method into RepositoryCacheVersionService.SetCacheUpdatedAsync to clear request cache after version updates

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/Umbraco.Core/Cache/IRepositoryCacheVersionAccessor.cs Added VersionChanged method to interface with default implementation; removed public modifiers for consistency
src/Umbraco.Web.Common/Cache/RepositoryCacheVersionAccessor.cs Implemented VersionChanged to remove stale cache entries with debug logging when removal fails
src/Umbraco.Core/Cache/RepositoryCacheVersionService.cs Integrated VersionChanged call after cache version updates to keep request cache in sync

Copy link
Contributor

@AndyButland AndyButland left a comment

Choose a reason for hiding this comment

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

Looks good and having followed the provided testing steps can confirm that it works as described.

@AndyButland AndyButland merged commit 2a609e1 into release/17.0 Nov 14, 2025
22 checks passed
@AndyButland AndyButland deleted the v17/hotfix/clear-request-cache-on-cache-version-update branch November 14, 2025 13:14
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