Reintroduce and obsolete static events for use in the distributed cache + added some accidentally removed scope completions

This commit is contained in:
Kenn Jacobsen
2021-03-17 12:47:16 +01:00
parent 3018e1ba89
commit 7a5af55cac
7 changed files with 134 additions and 77 deletions

View File

@@ -49,6 +49,16 @@ namespace Umbraco.Cms.Core.Cache
_logger.LogInformation("Initializing Umbraco internal event handlers for cache refreshing.");
// bind to user and user group events
Bind(() => UserService.SavedUserGroup += UserService_SavedUserGroup,
() => UserService.SavedUserGroup -= UserService_SavedUserGroup);
Bind(() => UserService.DeletedUserGroup += UserService_DeletedUserGroup,
() => UserService.DeletedUserGroup -= UserService_DeletedUserGroup);
Bind(() => UserService.SavedUser += UserService_SavedUser,
() => UserService.SavedUser -= UserService_SavedUser);
Bind(() => UserService.DeletedUser += UserService_DeletedUser,
() => UserService.DeletedUser -= UserService_DeletedUser);
// bind to dictionary events
Bind(() => LocalizationService.DeletedDictionaryItem += LocalizationService_DeletedDictionaryItem,
() => LocalizationService.DeletedDictionaryItem -= LocalizationService_DeletedDictionaryItem);
@@ -100,6 +110,10 @@ namespace Umbraco.Cms.Core.Cache
() => MacroService.Deleted -= MacroService_Deleted);
// bind to member events
Bind(() => MemberService.Saved += MemberService_Saved,
() => MemberService.Saved -= MemberService_Saved);
Bind(() => MemberService.Deleted += MemberService_Deleted,
() => MemberService.Deleted -= MemberService_Deleted);
Bind(() => MemberGroupService.Saved += MemberGroupService_Saved,
() => MemberGroupService.Saved -= MemberGroupService_Saved);
Bind(() => MemberGroupService.Deleted += MemberGroupService_Deleted,
@@ -119,6 +133,12 @@ namespace Umbraco.Cms.Core.Cache
//Bind(() => ContentService.DeletedBlueprint += ContentService_DeletedBlueprint,
// () => ContentService.DeletedBlueprint -= ContentService_DeletedBlueprint);
// bind to public access events
Bind(() => PublicAccessService.Saved += PublicAccessService_Saved,
() => PublicAccessService.Saved -= PublicAccessService_Saved);
Bind(() => PublicAccessService.Deleted += PublicAccessService_Deleted,
() => PublicAccessService.Deleted -= PublicAccessService_Deleted);
// bind to relation type events
Bind(() => RelationService.SavedRelationType += RelationService_SavedRelationType,
() => RelationService.SavedRelationType -= RelationService_SavedRelationType);
@@ -126,6 +146,20 @@ namespace Umbraco.Cms.Core.Cache
() => RelationService.DeletedRelationType -= RelationService_DeletedRelationType);
}
#region PublicAccessService
private void PublicAccessService_Saved(IPublicAccessService sender, SaveEventArgs<PublicAccessEntry> e)
{
_distributedCache.RefreshPublicAccess();
}
private void PublicAccessService_Deleted(IPublicAccessService sender, DeleteEventArgs<PublicAccessEntry> e)
{
_distributedCache.RefreshPublicAccess();
}
#endregion
#region ContentService
/// <summary>
@@ -259,6 +293,35 @@ namespace Umbraco.Cms.Core.Cache
#endregion
#region UserService
private void UserService_SavedUser(IUserService sender, SaveEventArgs<IUser> e)
{
foreach (var entity in e.SavedEntities)
_distributedCache.RefreshUserCache(entity.Id);
}
private void UserService_DeletedUser(IUserService sender, DeleteEventArgs<IUser> e)
{
foreach (var entity in e.DeletedEntities)
_distributedCache.RemoveUserCache(entity.Id);
}
private void UserService_SavedUserGroup(IUserService sender, SaveEventArgs<UserGroupWithUsers> e)
{
foreach (var entity in e.SavedEntities)
_distributedCache.RefreshUserGroupCache(entity.UserGroup.Id);
}
private void UserService_DeletedUserGroup(IUserService sender, DeleteEventArgs<IUserGroup> e)
{
foreach (var entity in e.DeletedEntities)
_distributedCache.RemoveUserGroupCache(entity.Id);
}
#endregion
#region FileService
/// <summary>
@@ -314,6 +377,20 @@ namespace Umbraco.Cms.Core.Cache
#endregion
#region MemberService
private void MemberService_Deleted(IMemberService sender, DeleteEventArgs<IMember> e)
{
_distributedCache.RemoveMemberCache(e.DeletedEntities.ToArray());
}
private void MemberService_Saved(IMemberService sender, SaveEventArgs<IMember> e)
{
_distributedCache.RefreshMemberCache(e.SavedEntities.ToArray());
}
#endregion
#region MemberGroupService
private void MemberGroupService_Deleted(IMemberGroupService sender, DeleteEventArgs<IMemberGroup> e)

View File

@@ -1,62 +0,0 @@
using System.Linq;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Infrastructure.Services.Notifications;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Cache
{
internal class DistributedCacheHandler :
INotificationHandler<MemberSavedNotification>,
INotificationHandler<MemberDeletedNotification>,
INotificationHandler<UserSavedNotification>,
INotificationHandler<UserDeletedNotification>,
INotificationHandler<UserGroupSavedNotification>,
INotificationHandler<UserGroupDeletedNotification>,
INotificationHandler<PublicAccessEntrySavedNotification>,
INotificationHandler<PublicAccessEntryDeletedNotification>
{
private readonly DistributedCache _distributedCache;
public DistributedCacheHandler(DistributedCache distributedCache) => _distributedCache = distributedCache;
public void Handle(MemberSavedNotification notification) => _distributedCache.RefreshMemberCache(notification.SavedEntities.ToArray());
public void Handle(MemberDeletedNotification notification) => _distributedCache.RemoveMemberCache(notification.DeletedEntities.ToArray());
public void Handle(UserSavedNotification notification)
{
foreach (var entity in notification.SavedEntities)
{
_distributedCache.RefreshUserCache(entity.Id);
}
}
public void Handle(UserDeletedNotification notification)
{
foreach (var entity in notification.DeletedEntities)
{
_distributedCache.RemoveUserCache(entity.Id);
}
}
public void Handle(UserGroupSavedNotification notification)
{
foreach (var entity in notification.SavedEntities)
{
_distributedCache.RefreshUserGroupCache(entity.Id);
}
}
public void Handle(UserGroupDeletedNotification notification)
{
foreach (var entity in notification.DeletedEntities)
{
_distributedCache.RemoveUserGroupCache(entity.Id);
}
}
public void Handle(PublicAccessEntrySavedNotification notification) => _distributedCache.RefreshPublicAccess();
public void Handle(PublicAccessEntryDeletedNotification notification) => _distributedCache.RefreshPublicAccess();
}
}

View File

@@ -76,17 +76,6 @@ namespace Umbraco.Cms.Core.Compose
.AddNotificationHandler<UserDeletedNotification, AuditNotificationsHandler>()
.AddNotificationHandler<UserGroupWithUsersSavedNotification, AuditNotificationsHandler>()
.AddNotificationHandler<AssignedUserGroupPermissionsNotification, AuditNotificationsHandler>();
// add notifications handlers for distributed cache
builder
.AddNotificationHandler<MemberSavedNotification, DistributedCacheHandler>()
.AddNotificationHandler<MemberDeletedNotification, DistributedCacheHandler>()
.AddNotificationHandler<UserSavedNotification, DistributedCacheHandler>()
.AddNotificationHandler<UserDeletedNotification, DistributedCacheHandler>()
.AddNotificationHandler<UserGroupSavedNotification, DistributedCacheHandler>()
.AddNotificationHandler<UserGroupDeletedNotification, DistributedCacheHandler>()
.AddNotificationHandler<PublicAccessEntrySavedNotification, DistributedCacheHandler>()
.AddNotificationHandler<PublicAccessEntryDeletedNotification, DistributedCacheHandler>();
}
}
}

View File

@@ -796,6 +796,7 @@ namespace Umbraco.Cms.Core.Services.Implement
if (raiseEvents)
{
scope.Notifications.Publish(new MemberSavedNotification(member, evtMsgs).WithStateFrom(savingNotification));
scope.Events.Dispatch(Saved, this, new SaveEventArgs<IMember>(member, false));
}
Audit(AuditType.Save, 0, member.Id);
@@ -834,6 +835,7 @@ namespace Umbraco.Cms.Core.Services.Implement
if (raiseEvents)
{
scope.Notifications.Publish(new MemberSavedNotification(membersA, evtMsgs).WithStateFrom(savingNotification));
scope.Events.Dispatch(Saved, this, new SaveEventArgs<IMember>(membersA, false));
}
Audit(AuditType.Save, 0, -1, "Save multiple Members");
@@ -875,6 +877,7 @@ namespace Umbraco.Cms.Core.Services.Implement
// a member has no descendants
_memberRepository.Delete(member);
scope.Notifications.Publish(new MemberDeletedNotification(member, evtMsgs).WithState(notificationState));
scope.Events.Dispatch(Deleted, this, new DeleteEventArgs<IMember>(member, false));
// media files deleted by QueuingEventDispatcher
}
@@ -1018,6 +1021,7 @@ namespace Umbraco.Cms.Core.Services.Implement
int[] ids = _memberGroupRepository.GetMemberIds(usernames);
_memberGroupRepository.AssignRoles(ids, roleNames);
scope.Notifications.Publish(new AssignedMemberRolesNotification(ids, roleNames));
scope.Complete();
}
}
@@ -1031,6 +1035,7 @@ namespace Umbraco.Cms.Core.Services.Implement
int[] ids = _memberGroupRepository.GetMemberIds(usernames);
_memberGroupRepository.DissociateRoles(ids, roleNames);
scope.Notifications.Publish(new RemovedMemberRolesNotification(ids, roleNames));
scope.Complete();
}
}
@@ -1043,6 +1048,7 @@ namespace Umbraco.Cms.Core.Services.Implement
scope.WriteLock(Constants.Locks.MemberTree);
_memberGroupRepository.AssignRoles(memberIds, roleNames);
scope.Notifications.Publish(new AssignedMemberRolesNotification(memberIds, roleNames));
scope.Complete();
}
}
@@ -1055,6 +1061,7 @@ namespace Umbraco.Cms.Core.Services.Implement
scope.WriteLock(Constants.Locks.MemberTree);
_memberGroupRepository.DissociateRoles(memberIds, roleNames);
scope.Notifications.Publish(new RemovedMemberRolesNotification(memberIds, roleNames));
scope.Complete();
}
}
@@ -1068,10 +1075,11 @@ namespace Umbraco.Cms.Core.Services.Implement
#region Event Handlers
/// <summary>
/// Occurs after members have been exported.
/// </summary>
public static event TypedEventHandler<IMemberService, ExportedMemberEventArgs> Exported;
[Obsolete("Will be removed in an upcoming version. Implement an INotificationHandler for MemberDeletedNotification instead.")]
public static event TypedEventHandler<IMemberService, DeleteEventArgs<IMember>> Deleted;
[Obsolete("Will be removed in an upcoming version. Implement an INotificationHandler for MemberSavedNotification instead.")]
public static event TypedEventHandler<IMemberService, SaveEventArgs<IMember>> Saved;
#endregion

View File

@@ -140,6 +140,7 @@ namespace Umbraco.Cms.Core.Services.Implement
scope.Complete();
scope.Notifications.Publish(new PublicAccessEntrySavedNotification(entry, evtMsgs).WithStateFrom(savingNotifiation));
scope.Events.Dispatch(Saved, this, new SaveEventArgs<PublicAccessEntry>(entry, false));
}
return OperationResult.Attempt.Succeed(evtMsgs, entry);
@@ -176,6 +177,7 @@ namespace Umbraco.Cms.Core.Services.Implement
scope.Complete();
scope.Notifications.Publish(new PublicAccessEntrySavedNotification(entry, evtMsgs).WithStateFrom(savingNotifiation));
scope.Events.Dispatch(Saved, this, new SaveEventArgs<PublicAccessEntry>(entry, false));
}
return OperationResult.Attempt.Succeed(evtMsgs);
@@ -202,6 +204,7 @@ namespace Umbraco.Cms.Core.Services.Implement
scope.Complete();
scope.Notifications.Publish(new PublicAccessEntrySavedNotification(entry, evtMsgs).WithStateFrom(savingNotifiation));
scope.Events.Dispatch(Saved, this, new SaveEventArgs<PublicAccessEntry>(entry, false));
}
return OperationResult.Attempt.Succeed(evtMsgs);
@@ -228,9 +231,16 @@ namespace Umbraco.Cms.Core.Services.Implement
scope.Complete();
scope.Notifications.Publish(new PublicAccessEntryDeletedNotification(entry, evtMsgs).WithStateFrom(deletingNotification));
scope.Events.Dispatch(Deleted, this, new DeleteEventArgs<PublicAccessEntry>(entry, false));
}
return OperationResult.Attempt.Succeed(evtMsgs);
}
[Obsolete("Will be removed in an upcoming version. Implement an INotificationHandler for PublicAccessEntrySavedNotification instead.")]
public static event TypedEventHandler<IPublicAccessService, SaveEventArgs<PublicAccessEntry>> Saved;
[Obsolete("Will be removed in an upcoming version. Implement an INotificationHandler for PublicAccessEntryDeletedNotification instead.")]
public static event TypedEventHandler<IPublicAccessService, DeleteEventArgs<PublicAccessEntry>> Deleted;
}
}

View File

@@ -142,6 +142,7 @@ namespace Umbraco.Cms.Core.Services.Implement
_userRepository.Save(user);
scope.Notifications.Publish(new UserSavedNotification(user, evtMsgs).WithStateFrom(savingNotification));
scope.Events.Dispatch(SavedUser, this, new SaveEventArgs<IUser>(user, false));
scope.Complete();
}
@@ -255,6 +256,7 @@ namespace Umbraco.Cms.Core.Services.Implement
_userRepository.Delete(user);
scope.Notifications.Publish(new UserDeletedNotification(user, evtMsgs).WithStateFrom(deletingNotification));
scope.Events.Dispatch(DeletedUser, this, new DeleteEventArgs<IUser>(user, false));
scope.Complete();
}
}
@@ -298,6 +300,7 @@ namespace Umbraco.Cms.Core.Services.Implement
if (raiseEvents)
{
scope.Notifications.Publish(new UserSavedNotification(entity, evtMsgs).WithStateFrom(savingNotification));
scope.Events.Dispatch(SavedUser, this, new SaveEventArgs<IUser>(entity, false));
}
scope.Complete();
@@ -351,6 +354,7 @@ namespace Umbraco.Cms.Core.Services.Implement
if (raiseEvents)
{
scope.Notifications.Publish(new UserSavedNotification(entitiesA, evtMsgs).WithStateFrom(savingNotification));
scope.Events.Dispatch(SavedUser, this, new SaveEventArgs<IUser>(entitiesA, false));
}
//commit the whole lot in one go
@@ -861,6 +865,7 @@ namespace Umbraco.Cms.Core.Services.Implement
{
scope.Notifications.Publish(new UserGroupSavedNotification(userGroup, evtMsgs).WithStateFrom(savingNotification));
scope.Notifications.Publish(new UserGroupWithUsersSavedNotification(userGroupWithUsers, evtMsgs).WithStateFrom(savingUserGroupWithUsersNotification));
scope.Events.Dispatch(SavedUserGroup, this, new SaveEventArgs<UserGroupWithUsers>(new UserGroupWithUsers(userGroup, addedUsers, removedUsers), false));
}
scope.Complete();
@@ -887,6 +892,7 @@ namespace Umbraco.Cms.Core.Services.Implement
_userGroupRepository.Delete(userGroup);
scope.Notifications.Publish(new UserGroupDeletedNotification(userGroup, evtMsgs).WithStateFrom(deletingNotification));
scope.Events.Dispatch(DeletedUserGroup, this, new DeleteEventArgs<IUserGroup>(userGroup, false));
scope.Complete();
}
@@ -1166,5 +1172,17 @@ namespace Umbraco.Cms.Core.Services.Implement
}
#endregion
[Obsolete("Will be removed in an upcoming version. Implement an INotificationHandler for UserSavedNotification instead.")]
public static event TypedEventHandler<IUserService, SaveEventArgs<IUser>> SavedUser;
[Obsolete("Will be removed in an upcoming version. Implement an INotificationHandler for UserDeletedNotification instead.")]
public static event TypedEventHandler<IUserService, DeleteEventArgs<IUser>> DeletedUser;
[Obsolete("Will be removed in an upcoming version. Implement an INotificationHandler for UserGroupSavedNotification instead.")]
public static event TypedEventHandler<IUserService, SaveEventArgs<UserGroupWithUsers>> SavedUserGroup;
[Obsolete("Will be removed in an upcoming version. Implement an INotificationHandler for UserGroupDeletedNotification instead.")]
public static event TypedEventHandler<IUserService, DeleteEventArgs<IUserGroup>> DeletedUserGroup;
}
}

View File

@@ -5,6 +5,7 @@ using NUnit.Framework;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
@@ -17,6 +18,7 @@ namespace Umbraco.Cms.Tests.Integration.Cache
[UmbracoTest(Boot = true)]
public class DistributedCacheBinderTests : UmbracoIntegrationTest
{
private IUserService UserService => GetRequiredService<IUserService>();
private ILocalizationService LocalizationService => GetRequiredService<ILocalizationService>();
private IDataTypeService DataTypeService => GetRequiredService<IDataTypeService>();
private IFileService FileService => GetRequiredService<IFileService>();
@@ -25,7 +27,11 @@ namespace Umbraco.Cms.Tests.Integration.Cache
private IDomainService DomainService => GetRequiredService<IDomainService>();
private IMemberTypeService MemberTypeService => GetRequiredService<IMemberTypeService>();
private IMacroService MacroService => GetRequiredService<IMacroService>();
private IMemberService MemberService => GetRequiredService<IMemberService>();
private IMemberGroupService MemberGroupService => GetRequiredService<IMemberGroupService>();
private IMediaService MediaService => GetRequiredService<IMediaService>();
private IContentService ContentService => GetRequiredService<IContentService>();
private IPublicAccessService PublicAccessService => GetRequiredService<IPublicAccessService>();
private IRelationService RelationService => GetRequiredService<IRelationService>();
private UriUtility UriUtility => GetRequiredService<UriUtility>();
private IUmbracoContextFactory UmbracoContextFactory => GetRequiredService<IUmbracoContextFactory>();
@@ -36,6 +42,11 @@ namespace Umbraco.Cms.Tests.Integration.Cache
var definitions = new IEventDefinition[]
{
new EventDefinition<IUserService, SaveEventArgs<IUser>>(null, UserService, new SaveEventArgs<IUser>(Enumerable.Empty<IUser>())),
new EventDefinition<IUserService, DeleteEventArgs<IUser>>(null, UserService, new DeleteEventArgs<IUser>(Enumerable.Empty<IUser>())),
new EventDefinition<IUserService, SaveEventArgs<UserGroupWithUsers>>(null, UserService, new SaveEventArgs<UserGroupWithUsers>(Enumerable.Empty<UserGroupWithUsers>())),
new EventDefinition<IUserService, DeleteEventArgs<IUserGroup>>(null, UserService, new DeleteEventArgs<IUserGroup>(Enumerable.Empty<IUserGroup>())),
new EventDefinition<ILocalizationService, SaveEventArgs<IDictionaryItem>>(null, LocalizationService, new SaveEventArgs<IDictionaryItem>(Enumerable.Empty<IDictionaryItem>())),
new EventDefinition<ILocalizationService, DeleteEventArgs<IDictionaryItem>>(null, LocalizationService, new DeleteEventArgs<IDictionaryItem>(Enumerable.Empty<IDictionaryItem>())),
@@ -65,6 +76,9 @@ namespace Umbraco.Cms.Tests.Integration.Cache
new EventDefinition<IMacroService, SaveEventArgs<IMacro>>(null, MacroService, new SaveEventArgs<IMacro>(Enumerable.Empty<IMacro>())),
new EventDefinition<IMacroService, DeleteEventArgs<IMacro>>(null, MacroService, new DeleteEventArgs<IMacro>(Enumerable.Empty<IMacro>())),
new EventDefinition<IMemberService, SaveEventArgs<IMember>>(null, MemberService, new SaveEventArgs<IMember>(Enumerable.Empty<IMember>())),
new EventDefinition<IMemberService, DeleteEventArgs<IMember>>(null, MemberService, new DeleteEventArgs<IMember>(Enumerable.Empty<IMember>())),
new EventDefinition<IMemberGroupService, SaveEventArgs<IMemberGroup>>(null, MemberGroupService, new SaveEventArgs<IMemberGroup>(Enumerable.Empty<IMemberGroup>())),
new EventDefinition<IMemberGroupService, DeleteEventArgs<IMemberGroup>>(null, MemberGroupService, new DeleteEventArgs<IMemberGroup>(Enumerable.Empty<IMemberGroup>())),
@@ -72,6 +86,9 @@ namespace Umbraco.Cms.Tests.Integration.Cache
//new EventDefinition<IContentService, SaveEventArgs<IContent>>(null, ContentService, new SaveEventArgs<IContent>(Enumerable.Empty<IContent>()), "SavedBlueprint"),
//new EventDefinition<IContentService, DeleteEventArgs<IContent>>(null, ContentService, new DeleteEventArgs<IContent>(Enumerable.Empty<IContent>()), "DeletedBlueprint"),
new EventDefinition<IPublicAccessService, SaveEventArgs<PublicAccessEntry>>(null, PublicAccessService, new SaveEventArgs<PublicAccessEntry>(Enumerable.Empty<PublicAccessEntry>())),
new EventDefinition<IPublicAccessService, DeleteEventArgs<PublicAccessEntry>>(null, PublicAccessService, new DeleteEventArgs<PublicAccessEntry>(Enumerable.Empty<PublicAccessEntry>())),
new EventDefinition<IRelationService, SaveEventArgs<IRelationType>>(null, RelationService, new SaveEventArgs<IRelationType>(Enumerable.Empty<IRelationType>())),
new EventDefinition<IRelationService, DeleteEventArgs<IRelationType>>(null, RelationService, new DeleteEventArgs<IRelationType>(Enumerable.Empty<IRelationType>())),