-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Repository Caches: Fix GUID read repository cache key collision causing GetAll failures (closes #21756)
#21762
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
AndyButland
merged 8 commits into
main
from
v17/bugfix/21756-fix-data-type-service-get-all-by-keys
Feb 19, 2026
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
941db12
Fix GUID read repository cache key collision with int-keyed repositor…
AndyButland 5cc2cc8
Remove GUID read repository for templates.
AndyButland cd62e34
Ensure GUID read repository cache keys are invalidated.
AndyButland 7b6b031
Further optimisation of by GUD GetAll reads.
AndyButland c5cf2ac
Move default repository cache timespan to a centralised constant.
AndyButland e89403b
Further use of centralised constant.
AndyButland 9fc756b
Merge branch 'main' into v17/bugfix/21756-fix-data-type-service-get-a…
AndyButland f9beebd
Add GetGuidKey<T>(Guid id) and update callers to use it.
AndyButland 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
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
131 changes: 131 additions & 0 deletions
131
src/Umbraco.Infrastructure/Cache/GuidReadRepositoryCachePolicy.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,131 @@ | ||
| // Copyright (c) Umbraco. | ||
| // See LICENSE for more details. | ||
|
|
||
| using Umbraco.Cms.Core.Models.Entities; | ||
| using Umbraco.Cms.Core.Persistence.Repositories; | ||
| using Umbraco.Cms.Core.Scoping; | ||
| using Umbraco.Cms.Infrastructure.Scoping; | ||
| using Umbraco.Extensions; | ||
|
|
||
| namespace Umbraco.Cms.Core.Cache; | ||
|
|
||
| /// <summary> | ||
| /// A cache policy for GUID-keyed read repositories that share an isolated cache | ||
| /// with their parent int-keyed repository. | ||
| /// </summary> | ||
| /// <typeparam name="TEntity">The type of the entity.</typeparam> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// GUID-keyed read repositories and their parent int-keyed repositories both resolve | ||
| /// to the same <see cref="IAppPolicyCache" /> via <c>IsolatedCaches.GetOrCreate<TEntity>()</c>. | ||
| /// If both used <see cref="DefaultRepositoryCachePolicy{TEntity, TId}" />, they would share | ||
| /// the same cache key prefix (<c>"uRepo_{TypeName}_"</c>), causing the int-keyed repository's | ||
| /// prefix-based count validation to always fail (finding 2× the expected entries). | ||
| /// </para> | ||
| /// <para> | ||
| /// This policy uses a separate prefix (<c>"uRepoGuid_{TypeName}_"</c>) so that GUID-keyed | ||
| /// entries don't interfere with the int-keyed repository's cache operations. | ||
| /// </para> | ||
| /// <para> | ||
| /// For <see cref="GetAll" /> without specific IDs, this policy always delegates to the | ||
| /// repository rather than caching the full set, since the parent int-keyed repository | ||
| /// already handles full-set caching with proper count validation. | ||
| /// </para> | ||
| /// </remarks> | ||
| internal sealed class GuidReadRepositoryCachePolicy<TEntity> : RepositoryCachePolicyBase<TEntity, Guid> | ||
| where TEntity : class, IEntity | ||
| { | ||
| internal static string GuidCacheKeyPrefix { get; } = RepositoryCacheKeys.GetGuidKey<TEntity>(); | ||
|
|
||
| public GuidReadRepositoryCachePolicy( | ||
| IAppPolicyCache cache, | ||
| IScopeAccessor scopeAccessor, | ||
| IRepositoryCacheVersionService repositoryCacheVersionService, | ||
| ICacheSyncService cacheSyncService) | ||
| : base(cache, scopeAccessor, repositoryCacheVersionService, cacheSyncService) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override TEntity? Get(Guid id, Func<Guid, TEntity?> performGet, Func<Guid[]?, IEnumerable<TEntity>?> performGetAll) | ||
| { | ||
| EnsureCacheIsSynced(); | ||
|
|
||
| var cacheKey = GuidCacheKeyPrefix + id; | ||
| TEntity? fromCache = Cache.GetCacheItem<TEntity>(cacheKey); | ||
|
|
||
| if (fromCache is not null) | ||
| { | ||
| return fromCache; | ||
| } | ||
|
|
||
| TEntity? entity = performGet(id); | ||
|
|
||
| if (entity is { HasIdentity: true }) | ||
| { | ||
| Cache.Insert(cacheKey, () => entity, TimeSpan.FromMinutes(5), true); | ||
| } | ||
|
|
||
| return entity; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override TEntity? GetCached(Guid id) | ||
| { | ||
| EnsureCacheIsSynced(); | ||
| return Cache.GetCacheItem<TEntity>(GuidCacheKeyPrefix + id); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override bool Exists(Guid id, Func<Guid, bool> performExists, Func<Guid[], IEnumerable<TEntity>?> performGetAll) | ||
| { | ||
| EnsureCacheIsSynced(); | ||
|
|
||
| TEntity? fromCache = Cache.GetCacheItem<TEntity>(GuidCacheKeyPrefix + id); | ||
| return fromCache is not null || performExists(id); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override TEntity[] GetAll(Guid[]? ids, Func<Guid[]?, IEnumerable<TEntity>?> performGetAll) | ||
| { | ||
| EnsureCacheIsSynced(); | ||
|
|
||
| // For specific IDs, try cache first. | ||
| if (ids?.Length > 0) | ||
| { | ||
| TEntity[] cached = ids | ||
| .Select(id => Cache.GetCacheItem<TEntity>(GuidCacheKeyPrefix + id)) | ||
| .WhereNotNull() | ||
| .ToArray(); | ||
|
|
||
| if (cached.Length == ids.Length) | ||
| { | ||
| return cached; | ||
| } | ||
| } | ||
|
|
||
| // For the full set (no IDs), always delegate to the repository. | ||
| // The parent int-keyed repository handles full-set caching with count validation. | ||
| return performGetAll(ids)?.WhereNotNull().ToArray() ?? Array.Empty<TEntity>(); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override void Create(TEntity entity, Action<TEntity> persistNew) | ||
| => throw new InvalidOperationException("This method won't be implemented."); | ||
|
|
||
| /// <inheritdoc /> | ||
| public override void Update(TEntity entity, Action<TEntity> persistUpdated) | ||
| => throw new InvalidOperationException("This method won't be implemented."); | ||
|
|
||
| /// <inheritdoc /> | ||
| public override void Delete(TEntity entity, Action<TEntity> persistDeleted) | ||
| => throw new InvalidOperationException("This method won't be implemented."); | ||
|
|
||
| /// <inheritdoc /> | ||
| public override void ClearAll() => Cache.ClearByKey(GuidCacheKeyPrefix); | ||
|
|
||
| /// <summary> | ||
| /// Gets the GUID-prefixed cache key for the given entity key. | ||
| /// </summary> | ||
| internal static string GetCacheKey(Guid key) => GuidCacheKeyPrefix + key; | ||
| } | ||
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.