Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions src/Umbraco.Core/Cache/DistributedCacheExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Services.Changes;

namespace Umbraco.Extensions;
Expand Down Expand Up @@ -152,11 +153,49 @@ public static void RefreshContentCache(this DistributedCache dc, IEnumerable<Tre

#region MemberCacheRefresher

[Obsolete("Please use the overload taking all parameters. Scheduled for removal in Umbraco 18.")]
public static void RefreshMemberCache(this DistributedCache dc, IEnumerable<IMember> members)
=> dc.RefreshByPayload(MemberCacheRefresher.UniqueId, members.DistinctBy(x => (x.Id, x.Username)).Select(x => new MemberCacheRefresher.JsonPayload(x.Id, x.Username, false)));
=> dc.RefreshMemberCache(members, new Dictionary<string, object?>());

public static void RefreshMemberCache(this DistributedCache dc, IEnumerable<IMember> members, IDictionary<string, object?> state)
=> dc.RefreshByPayload(
MemberCacheRefresher.UniqueId,
GetPayloads(members, state, false));

[Obsolete("Please use the overload taking all parameters. Scheduled for removal in Umbraco 18.")]
public static void RemoveMemberCache(this DistributedCache dc, IEnumerable<IMember> members)
=> dc.RefreshByPayload(MemberCacheRefresher.UniqueId, members.DistinctBy(x => (x.Id, x.Username)).Select(x => new MemberCacheRefresher.JsonPayload(x.Id, x.Username, true)));
=> dc.RemoveMemberCache(members, new Dictionary<string, object?>());

public static void RemoveMemberCache(this DistributedCache dc, IEnumerable<IMember> members, IDictionary<string, object?> state)
=> dc.RefreshByPayload(
MemberCacheRefresher.UniqueId,
GetPayloads(members, state, true));

// Internal for unit test.
internal static IEnumerable<MemberCacheRefresher.JsonPayload> GetPayloads(IEnumerable<IMember> members, IDictionary<string, object?> state, bool removed)
=> members
.DistinctBy(x => (x.Id, x.Username))
.Select(x => new MemberCacheRefresher.JsonPayload(x.Id, x.Username, removed)
{
PreviousUsername = GetPreviousUsername(x, state)
});

private static string? GetPreviousUsername(IMember x, IDictionary<string, object?> state)
{
if (state.TryGetValue(MemberSavedNotification.PreviousUsernameStateKey, out object? previousUserNames) is false)
{
return null;
}

if (previousUserNames is not IDictionary<Guid, string> previousUserNamesDictionary)
{
return null;
}

return previousUserNamesDictionary.TryGetValue(x.Key, out string? previousUsername)
? previousUsername
: null;
}

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ public abstract class DistributedCacheNotificationHandlerBase<TEntity, TNotifica
{
/// <inheritdoc />
public void Handle(TNotification notification)
=> Handle(GetEntities(notification));
=> Handle(
GetEntities(notification),
notification is StatefulNotification statefulNotification
? statefulNotification.State
: new Dictionary<string, object?>());

/// <inheritdoc />
public void Handle(IEnumerable<TNotification> notifications)
=> Handle(notifications.SelectMany(GetEntities));
{
foreach (TNotification notification in notifications)
{
Handle(notification);
}
}

/// <summary>
/// Gets the entities from the specified notification.
Expand All @@ -25,9 +34,23 @@ public void Handle(IEnumerable<TNotification> notifications)
/// </returns>
protected abstract IEnumerable<TEntity> GetEntities(TNotification notification);

// TODO (V18): When removing the obsolete method, make the remaining Handle method abstract
// rather than virtual. It couldn't be made abstract when introduced as that would be a breaking change.

/// <summary>
/// Handles the specified entities.
/// </summary>
/// <param name="entities">The entities.</param>
[Obsolete("Please use the overload taking all parameters. Scheduled for removal in Umbraco 18.")]
protected abstract void Handle(IEnumerable<TEntity> entities);

/// <summary>
/// Handles the specified entities.
/// </summary>
/// <param name="entities">The entities.</param>
/// <param name="state">The notification state.</param>
protected virtual void Handle(IEnumerable<TEntity> entities, IDictionary<string, object?> state)
#pragma warning disable CS0618 // Type or member is obsolete
=> Handle(entities);
#pragma warning restore CS0618 // Type or member is obsolete
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public ContentTreeChangeDistributedCacheNotificationHandler(DistributedCache dis
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<TreeChange<IContent>> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<TreeChange<IContent>> entities, IDictionary<string, object?> state)
=> _distributedCache.RefreshContentCache(entities);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public ContentTypeChangedDistributedCacheNotificationHandler(DistributedCache di
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<ContentTypeChange<IContentType>> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<ContentTypeChange<IContentType>> entities, IDictionary<string, object?> state)
=> _distributedCache.RefreshContentTypeCache(entities);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Services.Changes;
using Umbraco.Extensions;

namespace Umbraco.Cms.Core.Cache;
Expand All @@ -17,7 +18,12 @@ public DataTypeDeletedDistributedCacheNotificationHandler(DistributedCache distr
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<IDataType> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<IDataType> entities, IDictionary<string, object?> state)
{
_distributedCache.RemoveDataTypeCache(entities);
_distributedCache.RefreshValueEditorCache(entities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ public DataTypeSavedDistributedCacheNotificationHandler(DistributedCache distrib
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<IDataType> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<IDataType> entities, IDictionary<string, object?> state)
{
_distributedCache.RefreshDataTypeCache(entities);
_distributedCache.RefreshValueEditorCache(entities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public DictionaryItemDeletedDistributedCacheNotificationHandler(DistributedCache
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<IDictionaryItem> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<IDictionaryItem> entities, IDictionary<string, object?> state)
=> _distributedCache.RemoveDictionaryCache(entities);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public DictionaryItemSavedDistributedCacheNotificationHandler(DistributedCache d
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<IDictionaryItem> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<IDictionaryItem> entities, IDictionary<string, object?> state)
=> _distributedCache.RefreshDictionaryCache(entities);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public DomainDeletedDistributedCacheNotificationHandler(DistributedCache distrib
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<IDomain> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<IDomain> entities, IDictionary<string, object?> state)
=> _distributedCache.RemoveDomainCache(entities);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public DomainSavedDistributedCacheNotificationHandler(DistributedCache distribut
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<IDomain> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<IDomain> entities, IDictionary<string, object?> state)
=> _distributedCache.RefreshDomainCache(entities);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public LanguageDeletedDistributedCacheNotificationHandler(DistributedCache distr
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<ILanguage> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<ILanguage> entities, IDictionary<string, object?> state)
=> _distributedCache.RemoveLanguageCache(entities);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public LanguageSavedDistributedCacheNotificationHandler(DistributedCache distrib
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<ILanguage> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<ILanguage> entities, IDictionary<string, object?> state)
=> _distributedCache.RefreshLanguageCache(entities);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public MediaTreeChangeDistributedCacheNotificationHandler(DistributedCache distr
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<TreeChange<IMedia>> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<TreeChange<IMedia>> entities, IDictionary<string, object?> state)
=> _distributedCache.RefreshMediaCache(entities);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public MediaTypeChangedDistributedCacheNotificationHandler(DistributedCache dist
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<ContentTypeChange<IMediaType>> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<ContentTypeChange<IMediaType>> entities, IDictionary<string, object?> state)
=> _distributedCache.RefreshContentTypeCache(entities);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Services.Changes;
using Umbraco.Extensions;

namespace Umbraco.Cms.Core.Cache;
Expand All @@ -17,6 +18,11 @@ public MemberDeletedDistributedCacheNotificationHandler(DistributedCache distrib
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<IMember> entities)
=> _distributedCache.RemoveMemberCache(entities);
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<IMember> entities, IDictionary<string, object?> state)
=> _distributedCache.RemoveMemberCache(entities, state);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public MemberGroupDeletedDistributedCacheNotificationHandler(DistributedCache di
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<IMemberGroup> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<IMemberGroup> entities, IDictionary<string, object?> state)
=> _distributedCache.RemoveMemberGroupCache(entities);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Extensions;

Expand All @@ -17,6 +17,11 @@ public MemberGroupSavedDistributedCacheNotificationHandler(DistributedCache dist
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<IMemberGroup> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<IMemberGroup> entities, IDictionary<string, object?> state)
=> _distributedCache.RefreshMemberGroupCache(entities);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public MemberSavedDistributedCacheNotificationHandler(DistributedCache distribut
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<IMember> entities)
=> _distributedCache.RefreshMemberCache(entities);
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<IMember> entities, IDictionary<string, object?> state)
=> _distributedCache.RefreshMemberCache(entities, state);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public MemberTypeChangedDistributedCacheNotificationHandler(DistributedCache dis
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<ContentTypeChange<IMemberType>> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<ContentTypeChange<IMemberType>> entities, IDictionary<string, object?> state)
=> _distributedCache.RefreshContentTypeCache(entities);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Services.Changes;
using Umbraco.Extensions;

namespace Umbraco.Cms.Core.Cache;
Expand All @@ -17,6 +18,11 @@ public PublicAccessEntryDeletedDistributedCacheNotificationHandler(DistributedCa
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<PublicAccessEntry> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<PublicAccessEntry> entities, IDictionary<string, object?> state)
=> _distributedCache.RefreshPublicAccess();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public PublicAccessEntrySavedDistributedCacheNotificationHandler(DistributedCach
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<PublicAccessEntry> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<PublicAccessEntry> entities, IDictionary<string, object?> state)
=> _distributedCache.RefreshPublicAccess();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public RelationTypeDeletedDistributedCacheNotificationHandler(DistributedCache d
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<IRelationType> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<IRelationType> entities, IDictionary<string, object?> state)
=> _distributedCache.RemoveRelationTypeCache(entities);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public RelationTypeSavedDistributedCacheNotificationHandler(DistributedCache dis
=> _distributedCache = distributedCache;

/// <inheritdoc />
[Obsolete("Scheduled for removal in Umbraco 18.")]
protected override void Handle(IEnumerable<IRelationType> entities)
=> Handle(entities, new Dictionary<string, object?>());

/// <inheritdoc />
protected override void Handle(IEnumerable<IRelationType> entities, IDictionary<string, object?> state)
=> _distributedCache.RefreshRelationTypeCache(entities);
}
Loading