Skip to content

Commit 229f1e0

Browse files
AndyButlandZeegaan
authored andcommitted
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 b8b61cd)
1 parent 56569af commit 229f1e0

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

src/Umbraco.PublishedCache.HybridCache/DatabaseCacheRebuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public async Task RebuildDatabaseCacheIfSerializerChangedAsync()
120120
private Task PerformRebuild()
121121
{
122122
using ICoreScope scope = _coreScopeProvider.CreateCoreScope();
123-
_databaseCacheRepository.Rebuild();
123+
_databaseCacheRepository.Rebuild([], [], []);
124124

125125
// If the serializer type has changed, we also need to update it in the key value store.
126126
var currentSerializerValue = _keyValueService.GetValue(NuCacheSerializerKey);

src/Umbraco.PublishedCache.HybridCache/Persistence/DatabaseCacheRepository.cs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,11 @@ public void Rebuild(
101101
| ContentCacheDataSerializerEntityType.Media
102102
| ContentCacheDataSerializerEntityType.Member);
103103

104-
// 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).
105-
if (contentTypeIds != null && !contentTypeIds.Any()
106-
&& mediaTypeIds != null && !mediaTypeIds.Any()
107-
&& memberTypeIds != null && !memberTypeIds.Any())
104+
// If contentTypeIds, mediaTypeIds and memberTypeIds are all non-null but empty,
105+
// truncate the table as all records will be deleted (as these 3 are the only types in the table).
106+
if (contentTypeIds is not null && contentTypeIds.Count == 0 &&
107+
mediaTypeIds is not null && mediaTypeIds.Count == 0 &&
108+
memberTypeIds is not null && memberTypeIds.Count == 0)
108109
{
109110
if (Database.DatabaseType == DatabaseType.SqlServer2012)
110111
{
@@ -280,10 +281,15 @@ await Database.InsertOrUpdateAsync(
280281
// assumes content tree lock
281282
private void RebuildContentDbCache(IContentCacheDataSerializer serializer, int groupSize, IReadOnlyCollection<int>? contentTypeIds)
282283
{
284+
if (contentTypeIds is null)
285+
{
286+
return;
287+
}
288+
283289
Guid contentObjectType = Constants.ObjectTypes.Document;
284290

285291
// remove all - if anything fails the transaction will rollback
286-
if (contentTypeIds == null || contentTypeIds.Count == 0)
292+
if (contentTypeIds.Count == 0)
287293
{
288294
// must support SQL-CE
289295
Database.Execute(
@@ -310,7 +316,7 @@ SELECT id FROM umbracoNode
310316

311317
// insert back - if anything fails the transaction will rollback
312318
IQuery<IContent> query = SqlContext.Query<IContent>();
313-
if (contentTypeIds != null && contentTypeIds.Count > 0)
319+
if (contentTypeIds.Count > 0)
314320
{
315321
query = query.WhereIn(x => x.ContentTypeId, contentTypeIds); // assume number of ctypes won't blow IN(...)
316322
}
@@ -345,13 +351,17 @@ SELECT id FROM umbracoNode
345351
}
346352

347353
// assumes media tree lock
348-
private void RebuildMediaDbCache(IContentCacheDataSerializer serializer, int groupSize,
349-
IReadOnlyCollection<int>? contentTypeIds)
354+
private void RebuildMediaDbCache(IContentCacheDataSerializer serializer, int groupSize, IReadOnlyCollection<int>? contentTypeIds)
350355
{
356+
if (contentTypeIds is null)
357+
{
358+
return;
359+
}
360+
351361
Guid mediaObjectType = Constants.ObjectTypes.Media;
352362

353363
// remove all - if anything fails the transaction will rollback
354-
if (contentTypeIds is null || contentTypeIds.Count == 0)
364+
if (contentTypeIds.Count == 0)
355365
{
356366
// must support SQL-CE
357367
Database.Execute(
@@ -378,7 +388,7 @@ SELECT id FROM umbracoNode
378388

379389
// insert back - if anything fails the transaction will rollback
380390
IQuery<IMedia> query = SqlContext.Query<IMedia>();
381-
if (contentTypeIds is not null && contentTypeIds.Count > 0)
391+
if (contentTypeIds.Count > 0)
382392
{
383393
query = query.WhereIn(x => x.ContentTypeId, contentTypeIds); // assume number of ctypes won't blow IN(...)
384394
}
@@ -398,13 +408,17 @@ SELECT id FROM umbracoNode
398408
}
399409

400410
// assumes member tree lock
401-
private void RebuildMemberDbCache(IContentCacheDataSerializer serializer, int groupSize,
402-
IReadOnlyCollection<int>? contentTypeIds)
411+
private void RebuildMemberDbCache(IContentCacheDataSerializer serializer, int groupSize, IReadOnlyCollection<int>? contentTypeIds)
403412
{
413+
if (contentTypeIds is null)
414+
{
415+
return;
416+
}
417+
404418
Guid memberObjectType = Constants.ObjectTypes.Member;
405419

406420
// remove all - if anything fails the transaction will rollback
407-
if (contentTypeIds == null || contentTypeIds.Count == 0)
421+
if (contentTypeIds.Count == 0)
408422
{
409423
// must support SQL-CE
410424
Database.Execute(
@@ -431,7 +445,7 @@ SELECT id FROM umbracoNode
431445

432446
// insert back - if anything fails the transaction will rollback
433447
IQuery<IMember> query = SqlContext.Query<IMember>();
434-
if (contentTypeIds != null && contentTypeIds.Count > 0)
448+
if (contentTypeIds.Count > 0)
435449
{
436450
query = query.WhereIn(x => x.ContentTypeId, contentTypeIds); // assume number of ctypes won't blow IN(...)
437451
}

src/Umbraco.PublishedCache.HybridCache/Services/MediaCacheService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public async Task RebuildMemoryCacheByContentTypeAsync(IEnumerable<int> mediaTyp
258258
public void Rebuild(IReadOnlyCollection<int> contentTypeIds)
259259
{
260260
using ICoreScope scope = _scopeProvider.CreateCoreScope();
261-
_databaseCacheRepository.Rebuild(contentTypeIds.ToList());
261+
_databaseCacheRepository.Rebuild(mediaTypeIds: contentTypeIds.ToList());
262262

263263
IEnumerable<Guid> mediaTypeKeys = contentTypeIds.Select(x => _idKeyMap.GetKeyForId(x, UmbracoObjectTypes.MediaType))
264264
.Where(x => x.Success)

0 commit comments

Comments
 (0)