From 229f1e0ce08a67c456bc48883c4c1632262ff39b Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Thu, 14 Aug 2025 08:13:35 +0100 Subject: [PATCH] Fixed behaviour on database cache rebuild to update only for requested content types (#19905) Fixed behaviour on database cache rebuild to update only for requested content types. (cherry picked from commit b8b61cd326048380b762f4104c6178e4fcdde4f2) --- .../DatabaseCacheRebuilder.cs | 2 +- .../Persistence/DatabaseCacheRepository.cs | 42 ++++++++++++------- .../Services/MediaCacheService.cs | 2 +- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/Umbraco.PublishedCache.HybridCache/DatabaseCacheRebuilder.cs b/src/Umbraco.PublishedCache.HybridCache/DatabaseCacheRebuilder.cs index b61daf7f7c..5b1512ef50 100644 --- a/src/Umbraco.PublishedCache.HybridCache/DatabaseCacheRebuilder.cs +++ b/src/Umbraco.PublishedCache.HybridCache/DatabaseCacheRebuilder.cs @@ -120,7 +120,7 @@ internal sealed class DatabaseCacheRebuilder : IDatabaseCacheRebuilder private Task PerformRebuild() { using ICoreScope scope = _coreScopeProvider.CreateCoreScope(); - _databaseCacheRepository.Rebuild(); + _databaseCacheRepository.Rebuild([], [], []); // If the serializer type has changed, we also need to update it in the key value store. var currentSerializerValue = _keyValueService.GetValue(NuCacheSerializerKey); diff --git a/src/Umbraco.PublishedCache.HybridCache/Persistence/DatabaseCacheRepository.cs b/src/Umbraco.PublishedCache.HybridCache/Persistence/DatabaseCacheRepository.cs index b72af50a59..6e1df8b5d0 100644 --- a/src/Umbraco.PublishedCache.HybridCache/Persistence/DatabaseCacheRepository.cs +++ b/src/Umbraco.PublishedCache.HybridCache/Persistence/DatabaseCacheRepository.cs @@ -101,10 +101,11 @@ internal sealed class DatabaseCacheRepository : RepositoryBase, IDatabaseCacheRe | ContentCacheDataSerializerEntityType.Media | ContentCacheDataSerializerEntityType.Member); - // If contentTypeIds, mediaTypeIds and memberTypeIds are null, truncate table as all records will be deleted (as these 3 are the only types in the table). - if (contentTypeIds != null && !contentTypeIds.Any() - && mediaTypeIds != null && !mediaTypeIds.Any() - && memberTypeIds != null && !memberTypeIds.Any()) + // If contentTypeIds, mediaTypeIds and memberTypeIds are all non-null but empty, + // truncate the table as all records will be deleted (as these 3 are the only types in the table). + if (contentTypeIds is not null && contentTypeIds.Count == 0 && + mediaTypeIds is not null && mediaTypeIds.Count == 0 && + memberTypeIds is not null && memberTypeIds.Count == 0) { if (Database.DatabaseType == DatabaseType.SqlServer2012) { @@ -280,10 +281,15 @@ internal sealed class DatabaseCacheRepository : RepositoryBase, IDatabaseCacheRe // assumes content tree lock private void RebuildContentDbCache(IContentCacheDataSerializer serializer, int groupSize, IReadOnlyCollection? contentTypeIds) { + if (contentTypeIds is null) + { + return; + } + Guid contentObjectType = Constants.ObjectTypes.Document; // remove all - if anything fails the transaction will rollback - if (contentTypeIds == null || contentTypeIds.Count == 0) + if (contentTypeIds.Count == 0) { // must support SQL-CE Database.Execute( @@ -310,7 +316,7 @@ WHERE cmsContentNu.nodeId IN ( // insert back - if anything fails the transaction will rollback IQuery query = SqlContext.Query(); - if (contentTypeIds != null && contentTypeIds.Count > 0) + if (contentTypeIds.Count > 0) { query = query.WhereIn(x => x.ContentTypeId, contentTypeIds); // assume number of ctypes won't blow IN(...) } @@ -345,13 +351,17 @@ WHERE cmsContentNu.nodeId IN ( } // assumes media tree lock - private void RebuildMediaDbCache(IContentCacheDataSerializer serializer, int groupSize, - IReadOnlyCollection? contentTypeIds) + private void RebuildMediaDbCache(IContentCacheDataSerializer serializer, int groupSize, IReadOnlyCollection? contentTypeIds) { + if (contentTypeIds is null) + { + return; + } + Guid mediaObjectType = Constants.ObjectTypes.Media; // remove all - if anything fails the transaction will rollback - if (contentTypeIds is null || contentTypeIds.Count == 0) + if (contentTypeIds.Count == 0) { // must support SQL-CE Database.Execute( @@ -378,7 +388,7 @@ WHERE cmsContentNu.nodeId IN ( // insert back - if anything fails the transaction will rollback IQuery query = SqlContext.Query(); - if (contentTypeIds is not null && contentTypeIds.Count > 0) + if (contentTypeIds.Count > 0) { query = query.WhereIn(x => x.ContentTypeId, contentTypeIds); // assume number of ctypes won't blow IN(...) } @@ -398,13 +408,17 @@ WHERE cmsContentNu.nodeId IN ( } // assumes member tree lock - private void RebuildMemberDbCache(IContentCacheDataSerializer serializer, int groupSize, - IReadOnlyCollection? contentTypeIds) + private void RebuildMemberDbCache(IContentCacheDataSerializer serializer, int groupSize, IReadOnlyCollection? contentTypeIds) { + if (contentTypeIds is null) + { + return; + } + Guid memberObjectType = Constants.ObjectTypes.Member; // remove all - if anything fails the transaction will rollback - if (contentTypeIds == null || contentTypeIds.Count == 0) + if (contentTypeIds.Count == 0) { // must support SQL-CE Database.Execute( @@ -431,7 +445,7 @@ WHERE cmsContentNu.nodeId IN ( // insert back - if anything fails the transaction will rollback IQuery query = SqlContext.Query(); - if (contentTypeIds != null && contentTypeIds.Count > 0) + if (contentTypeIds.Count > 0) { query = query.WhereIn(x => x.ContentTypeId, contentTypeIds); // assume number of ctypes won't blow IN(...) } diff --git a/src/Umbraco.PublishedCache.HybridCache/Services/MediaCacheService.cs b/src/Umbraco.PublishedCache.HybridCache/Services/MediaCacheService.cs index 0a8283279a..29095b1d04 100644 --- a/src/Umbraco.PublishedCache.HybridCache/Services/MediaCacheService.cs +++ b/src/Umbraco.PublishedCache.HybridCache/Services/MediaCacheService.cs @@ -258,7 +258,7 @@ internal sealed class MediaCacheService : IMediaCacheService public void Rebuild(IReadOnlyCollection contentTypeIds) { using ICoreScope scope = _scopeProvider.CreateCoreScope(); - _databaseCacheRepository.Rebuild(contentTypeIds.ToList()); + _databaseCacheRepository.Rebuild(mediaTypeIds: contentTypeIds.ToList()); IEnumerable mediaTypeKeys = contentTypeIds.Select(x => _idKeyMap.GetKeyForId(x, UmbracoObjectTypes.MediaType)) .Where(x => x.Success)