Skip to content

Commit

Permalink
Fix Nucache rebuilding more type caches than necessary (#12785)
Browse files Browse the repository at this point in the history
  • Loading branch information
nzdev authored Sep 8, 2022
1 parent 5f42cf0 commit fdc1b02
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
16 changes: 16 additions & 0 deletions src/Umbraco.Core/PublishedCache/IPublishedSnapshotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ void Rebuild(
IReadOnlyCollection<int>? mediaTypeIds = null,
IReadOnlyCollection<int>? memberTypeIds = null);


/// <summary>
/// Rebuilds all internal database caches (but does not reload).
/// </summary>
/// <remarks>
/// <para>
/// Forces the snapshot service to rebuild its internal database caches. For instance, some caches
/// may rely on a database table to store pre-serialized version of documents.
/// </para>
/// <para>
/// This does *not* reload the caches. Caches need to be reloaded, for instance via
/// <see cref="DistributedCache" /> RefreshAllPublishedSnapshot method.
/// </para>
/// </remarks>
void RebuildAll() => Rebuild(Array.Empty<int>(), Array.Empty<int>(), Array.Empty<int>());

/* An IPublishedCachesService implementation can rely on transaction-level events to update
* its internal, database-level data, as these events are purely internal. However, it cannot
* rely on cache refreshers CacheUpdated events to update itself, as these events are external
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public PublishedSnapshotRebuilder(
/// <inheritdoc />
public void Rebuild()
{
_publishedSnapshotService.Rebuild();
_publishedSnapshotService.RebuildAll();
_distributedCache.RefreshAllPublishedSnapshot();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public void RefreshMember(IMember member)
OnRepositoryRefreshed(serializer, member, false);
}

/// <inheritdoc/>
public void Rebuild(
IReadOnlyCollection<int>? contentTypeIds = null,
IReadOnlyCollection<int>? mediaTypeIds = null,
Expand All @@ -107,9 +108,20 @@ public void Rebuild(
| ContentCacheDataSerializerEntityType.Media
| ContentCacheDataSerializerEntityType.Member);

RebuildContentDbCache(serializer, _nucacheSettings.Value.SqlPageSize, contentTypeIds);
RebuildMediaDbCache(serializer, _nucacheSettings.Value.SqlPageSize, mediaTypeIds);
RebuildMemberDbCache(serializer, _nucacheSettings.Value.SqlPageSize, memberTypeIds);
if(contentTypeIds != null)
{
RebuildContentDbCache(serializer, _nucacheSettings.Value.SqlPageSize, contentTypeIds);
}

if (mediaTypeIds != null)
{
RebuildMediaDbCache(serializer, _nucacheSettings.Value.SqlPageSize, mediaTypeIds);
}

if (memberTypeIds != null)
{
RebuildMemberDbCache(serializer, _nucacheSettings.Value.SqlPageSize, memberTypeIds);
}
}

// assumes content tree lock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void RebuildDatabaseCacheIfSerializerChanged()
using (_profilingLogger.TraceDuration<NuCacheContentService>(
$"Rebuilding NuCache database with {serializer} serializer"))
{
Rebuild();
RebuildAll();
_keyValueService.SetValue(NuCacheSerializerKey, serializer.ToString());
}
}
Expand Down Expand Up @@ -113,11 +113,17 @@ public void RefreshMedia(IMedia media)
public void RefreshMember(IMember member)
=> _repository.RefreshMember(member);

/// <inheritdoc />
public void RebuildAll()
{
Rebuild(Array.Empty<int>(), Array.Empty<int>(), Array.Empty<int>());
}

/// <inheritdoc />
public void Rebuild(
IReadOnlyCollection<int>? contentTypeIds = null,
IReadOnlyCollection<int>? mediaTypeIds = null,
IReadOnlyCollection<int>? memberTypeIds = null)
IReadOnlyCollection<int>? contentTypeIds = null,
IReadOnlyCollection<int>? mediaTypeIds = null,
IReadOnlyCollection<int>? memberTypeIds = null)
{
using (ICoreScope scope = ScopeProvider.CreateCoreScope(repositoryCacheMode: RepositoryCacheMode.Scoped))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public PublishedSnapshotCacheStatusController(
[HttpPost]
public string RebuildDbCache()
{
_publishedSnapshotService.Rebuild();
//Rebuild All
_publishedSnapshotService.RebuildAll();
return _publishedSnapshotStatus.GetStatus();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.PublishedCache;
Expand Down Expand Up @@ -59,7 +60,7 @@ public void UnpublishedNameChanges()

Assert.AreEqual("hello", segment);

PublishedSnapshotService.Rebuild();
PublishedSnapshotService.RebuildAll();

cachedContent = ContentService.GetById(content.Id);
segment = urlSegmentProvider.GetUrlSegment(cachedContent);
Expand All @@ -76,7 +77,7 @@ public void UnpublishedNameChanges()
// The page has now been published, so we should see the new url segment
Assert.AreEqual("goodbye", segment);

PublishedSnapshotService.Rebuild();
PublishedSnapshotService.RebuildAll();
cachedContent = ContentService.GetById(content.Id);
segment = urlSegmentProvider.GetUrlSegment(cachedContent);

Expand Down

0 comments on commit fdc1b02

Please sign in to comment.