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 b8b61cd326)
This commit is contained in:
Andy Butland
2025-08-14 08:13:35 +01:00
committed by Zeegaan
parent 56569af0f9
commit 229f1e0ce0
3 changed files with 30 additions and 16 deletions

View File

@@ -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);

View File

@@ -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<int>? 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<IContent> query = SqlContext.Query<IContent>();
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<int>? contentTypeIds)
private void RebuildMediaDbCache(IContentCacheDataSerializer serializer, int groupSize, IReadOnlyCollection<int>? 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<IMedia> query = SqlContext.Query<IMedia>();
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<int>? contentTypeIds)
private void RebuildMemberDbCache(IContentCacheDataSerializer serializer, int groupSize, IReadOnlyCollection<int>? 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<IMember> query = SqlContext.Query<IMember>();
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(...)
}

View File

@@ -258,7 +258,7 @@ internal sealed class MediaCacheService : IMediaCacheService
public void Rebuild(IReadOnlyCollection<int> contentTypeIds)
{
using ICoreScope scope = _scopeProvider.CreateCoreScope();
_databaseCacheRepository.Rebuild(contentTypeIds.ToList());
_databaseCacheRepository.Rebuild(mediaTypeIds: contentTypeIds.ToList());
IEnumerable<Guid> mediaTypeKeys = contentTypeIds.Select(x => _idKeyMap.GetKeyForId(x, UmbracoObjectTypes.MediaType))
.Where(x => x.Success)