Skip to content

Commit 380f3f7

Browse files
Clear elements cache instead of refreshing it (#17708)
1 parent 437fcba commit 380f3f7

File tree

1 file changed

+11
-48
lines changed

1 file changed

+11
-48
lines changed

src/Umbraco.PublishedCache.HybridCache/NotificationHandlers/CacheRefreshingNotificationHandler.cs

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public CacheRefreshingNotificationHandler(
4545

4646
public async Task HandleAsync(ContentRefreshNotification notification, CancellationToken cancellationToken)
4747
{
48-
await RefreshElementsCacheAsync(notification.Entity);
48+
ClearElementsCache();
4949

5050
await _documentCacheService.RefreshContentAsync(notification.Entity);
5151
}
@@ -54,73 +54,36 @@ public async Task HandleAsync(ContentDeletedNotification notification, Cancellat
5454
{
5555
foreach (IContent deletedEntity in notification.DeletedEntities)
5656
{
57-
RemoveFromElementsCache(deletedEntity);
57+
ClearElementsCache();
5858
await _documentCacheService.DeleteItemAsync(deletedEntity);
5959
}
6060
}
6161

6262
public async Task HandleAsync(MediaRefreshNotification notification, CancellationToken cancellationToken)
6363
{
64-
await RefreshElementsCacheAsync(notification.Entity);
64+
ClearElementsCache();
6565
await _mediaCacheService.RefreshMediaAsync(notification.Entity);
6666
}
6767

6868
public async Task HandleAsync(MediaDeletedNotification notification, CancellationToken cancellationToken)
6969
{
7070
foreach (IMedia deletedEntity in notification.DeletedEntities)
7171
{
72-
RemoveFromElementsCache(deletedEntity);
72+
ClearElementsCache();
7373
await _mediaCacheService.DeleteItemAsync(deletedEntity);
7474
}
7575
}
7676

77-
private async Task RefreshElementsCacheAsync(IUmbracoEntity content)
77+
private void ClearElementsCache()
7878
{
79-
IEnumerable<IRelation> parentRelations = _relationService.GetByParent(content)!;
80-
IEnumerable<IRelation> childRelations = _relationService.GetByChild(content);
81-
82-
var ids = parentRelations.Select(x => x.ChildId).Concat(childRelations.Select(x => x.ParentId)).ToHashSet();
83-
// We need to add ourselves to the list of ids to clear
84-
ids.Add(content.Id);
85-
foreach (var id in ids)
86-
{
87-
if (await _documentCacheService.HasContentByIdAsync(id) is false)
88-
{
89-
continue;
90-
}
91-
92-
IPublishedContent? publishedContent = await _documentCacheService.GetByIdAsync(id);
93-
if (publishedContent is null)
94-
{
95-
continue;
96-
}
97-
98-
foreach (IPublishedProperty publishedProperty in publishedContent.Properties)
99-
{
100-
var property = (PublishedProperty) publishedProperty;
101-
if (property.ReferenceCacheLevel is PropertyCacheLevel.Elements
102-
|| property.PropertyType.DeliveryApiCacheLevel is PropertyCacheLevel.Elements
103-
|| property.PropertyType.DeliveryApiCacheLevelForExpansion is PropertyCacheLevel.Elements)
104-
{
105-
_elementsCache.ClearByKey(property.ValuesCacheKey);
106-
}
107-
}
108-
}
79+
// Ideally we'd like to not have to clear the entire cache here. However, this was the existing behavior in NuCache.
80+
// The reason for this is that we have no way to know which elements are affected by the changes. or what their keys are.
81+
// This is because currently published elements lives exclusively in a JSON blob in the umbracoPropertyData table.
82+
// This means that the only way to resolve these keys are to actually parse this data with a specific value converter, and for all cultures, which is not feasible.
83+
// If published elements become their own entities with relations, instead of just property data, we can revisit this,
84+
_elementsCache.Clear();
10985
}
11086

111-
private void RemoveFromElementsCache(IUmbracoEntity content)
112-
{
113-
// ClearByKey clears by "startsWith" so we'll clear by the cachekey prefix + contentKey
114-
// This will clear any and all properties for this content item, this is important because
115-
// we cannot resolve the PublishedContent for this entity since it and its content type is deleted.
116-
_elementsCache.ClearByKey(GetContentWideCacheKey(content.Key, true));
117-
_elementsCache.ClearByKey(GetContentWideCacheKey(content.Key, false));
118-
}
119-
120-
private string GetContentWideCacheKey(Guid contentKey, bool isPreviewing) => isPreviewing
121-
? CacheKeys.PreviewPropertyCacheKeyPrefix + contentKey
122-
: CacheKeys.PropertyCacheKeyPrefix + contentKey;
123-
12487
public Task HandleAsync(ContentTypeRefreshedNotification notification, CancellationToken cancellationToken)
12588
{
12689
const ContentTypeChangeTypes types // only for those that have been refreshed

0 commit comments

Comments
 (0)