Files
Umbraco-CMS/src/Umbraco.Infrastructure/Cache/DistributedCacheBinder_Handlers.cs

419 lines
14 KiB
C#
Raw Normal View History

2021-03-01 13:10:37 +01:00
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
2017-05-12 14:49:44 +02:00
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.Changes;
using Umbraco.Cms.Core.Services.Implement;
2021-03-25 14:51:00 +01:00
using Umbraco.Cms.Infrastructure.Services.Notifications;
using Umbraco.Extensions;
2016-09-08 18:43:58 +02:00
namespace Umbraco.Cms.Core.Cache
2016-09-08 18:43:58 +02:00
{
/// <summary>
/// Default <see cref="IDistributedCacheBinder"/> implementation.
2016-09-08 18:43:58 +02:00
/// </summary>
2021-03-25 14:51:00 +01:00
public partial class DistributedCacheBinder :
INotificationHandler<DictionaryItemDeletedNotification>,
INotificationHandler<DictionaryItemSavedNotification>,
INotificationHandler<LanguageSavedNotification>,
INotificationHandler<LanguageDeletedNotification>,
2021-03-29 10:42:42 +02:00
INotificationHandler<MemberSavedNotification>,
INotificationHandler<MemberDeletedNotification>,
INotificationHandler<PublicAccessEntrySavedNotification>,
INotificationHandler<PublicAccessEntryDeletedNotification>,
INotificationHandler<UserSavedNotification>,
INotificationHandler<UserDeletedNotification>,
INotificationHandler<UserGroupWithUsersSavedNotification>,
INotificationHandler<UserGroupDeletedNotification>,
INotificationHandler<MemberGroupDeletedNotification>,
2021-03-30 12:14:32 +02:00
INotificationHandler<MemberGroupSavedNotification>,
INotificationHandler<TemplateDeletedNotification>,
INotificationHandler<TemplateSavedNotification>,
INotificationHandler<DataTypeDeletedNotification>,
2021-03-31 11:52:36 +02:00
INotificationHandler<DataTypeSavedNotification>,
INotificationHandler<RelationTypeDeletedNotification>,
INotificationHandler<RelationTypeSavedNotification>,
2021-03-31 13:26:31 +02:00
INotificationHandler<DomainDeletedNotification>,
INotificationHandler<DomainSavedNotification>,
INotificationHandler<MacroSavedNotification>,
INotificationHandler<MacroDeletedNotification>
2016-09-08 18:43:58 +02:00
{
2017-05-12 14:49:44 +02:00
private List<Action> _unbinders;
private void Bind(Action binder, Action unbinder)
{
// bind now
binder();
// and register unbinder for later, if needed
_unbinders?.Add(unbinder);
}
2017-05-12 14:49:44 +02:00
/// <inheritdoc />
public void UnbindEvents()
2016-09-08 18:43:58 +02:00
{
if (_unbinders == null)
throw new NotSupportedException();
foreach (var unbinder in _unbinders)
unbinder();
_unbinders = null;
2016-09-08 18:43:58 +02:00
}
/// <inheritdoc />
public void BindEvents(bool supportUnbinding = false)
2016-09-08 18:43:58 +02:00
{
if (supportUnbinding)
_unbinders = new List<Action>();
2017-05-12 14:49:44 +02:00
2020-09-15 08:45:40 +02:00
_logger.LogInformation("Initializing Umbraco internal event handlers for cache refreshing.");
2017-07-11 19:13:45 +02:00
2016-09-08 18:43:58 +02:00
// bind to content type events
2017-05-12 14:49:44 +02:00
Bind(() => ContentTypeService.Changed += ContentTypeService_Changed,
() => ContentTypeService.Changed -= ContentTypeService_Changed);
2017-06-23 18:54:42 +02:00
Bind(() => MediaTypeService.Changed += MediaTypeService_Changed,
() => MediaTypeService.Changed -= MediaTypeService_Changed);
Bind(() => MemberTypeService.Changed += MemberTypeService_Changed,
() => MemberTypeService.Changed -= MemberTypeService_Changed);
2016-09-08 18:43:58 +02:00
2017-05-12 14:49:44 +02:00
// bind to media events - handles all media changes
Bind(() => MediaService.TreeChanged += MediaService_TreeChanged,
() => MediaService.TreeChanged -= MediaService_TreeChanged);
2016-09-08 18:43:58 +02:00
// bind to content events
Bind(() => ContentService.TreeChanged += ContentService_TreeChanged,// handles all content changes
() => ContentService.TreeChanged -= ContentService_TreeChanged);
2016-09-08 18:43:58 +02:00
2017-09-14 11:41:46 +02:00
// TreeChanged should also deal with this
//Bind(() => ContentService.SavedBlueprint += ContentService_SavedBlueprint,
// () => ContentService.SavedBlueprint -= ContentService_SavedBlueprint);
//Bind(() => ContentService.DeletedBlueprint += ContentService_DeletedBlueprint,
// () => ContentService.DeletedBlueprint -= ContentService_DeletedBlueprint);
2017-05-12 14:49:44 +02:00
}
2016-09-08 18:43:58 +02:00
#region PublicAccessService
2021-03-29 10:42:42 +02:00
public void Handle(PublicAccessEntrySavedNotification notification)
2016-09-08 18:43:58 +02:00
{
2017-07-11 19:13:45 +02:00
_distributedCache.RefreshPublicAccess();
2016-09-08 18:43:58 +02:00
}
2021-03-29 10:42:42 +02:00
public void Handle(PublicAccessEntryDeletedNotification notification)
2016-09-08 18:43:58 +02:00
{
2017-07-11 19:13:45 +02:00
_distributedCache.RefreshPublicAccess();
2021-03-29 10:42:42 +02:00
2016-09-08 18:43:58 +02:00
}
#endregion
#region ContentService
/// <summary>
/// Handles cache refreshing for when content is copied
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks>
/// When an entity is copied new permissions may be assigned to it based on it's parent, if that is the
/// case then we need to clear all user permissions cache.
/// </remarks>
2017-07-11 19:13:45 +02:00
private void ContentService_Copied(IContentService sender, CopyEventArgs<IContent> e)
{
}
2016-09-08 18:43:58 +02:00
private void ContentService_TreeChanged(IContentService sender, TreeChange<IContent>.EventArgs args)
2016-09-08 18:43:58 +02:00
{
2017-07-11 19:13:45 +02:00
_distributedCache.RefreshContentCache(args.Changes.ToArray());
2016-09-08 18:43:58 +02:00
}
2017-09-14 11:41:46 +02:00
//private void ContentService_SavedBlueprint(IContentService sender, SaveEventArgs<IContent> e)
//{
// _distributedCache.RefreshUnpublishedPageCache(e.SavedEntities.ToArray());
//}
//private void ContentService_DeletedBlueprint(IContentService sender, DeleteEventArgs<IContent> e)
//{
// _distributedCache.RemoveUnpublishedPageCache(e.DeletedEntities.ToArray());
//}
2016-09-08 18:43:58 +02:00
#endregion
#region LocalizationService / Dictionary
2021-03-25 14:51:00 +01:00
public void Handle(DictionaryItemSavedNotification notification)
2016-09-08 18:43:58 +02:00
{
2021-03-25 14:51:00 +01:00
foreach (IDictionaryItem entity in notification.SavedEntities)
{
2017-07-11 19:13:45 +02:00
_distributedCache.RefreshDictionaryCache(entity.Id);
2021-03-25 14:51:00 +01:00
}
2016-09-08 18:43:58 +02:00
}
2021-03-25 14:51:00 +01:00
public void Handle(DictionaryItemDeletedNotification notification)
2016-09-08 18:43:58 +02:00
{
2021-03-25 14:51:00 +01:00
foreach (IDictionaryItem entity in notification.DeletedEntities)
{
2017-07-11 19:13:45 +02:00
_distributedCache.RemoveDictionaryCache(entity.Id);
2021-03-25 14:51:00 +01:00
}
2016-09-08 18:43:58 +02:00
}
#endregion
#region DataTypeService
2017-05-12 14:49:44 +02:00
public void Handle(DataTypeSavedNotification notification)
2016-09-08 18:43:58 +02:00
{
foreach (IDataType entity in notification.SavedEntities)
{
2017-07-11 19:13:45 +02:00
_distributedCache.RefreshDataTypeCache(entity);
}
2016-09-08 18:43:58 +02:00
}
public void Handle(DataTypeDeletedNotification notification)
2016-09-08 18:43:58 +02:00
{
foreach (IDataType entity in notification.DeletedEntities)
{
2017-07-11 19:13:45 +02:00
_distributedCache.RemoveDataTypeCache(entity);
}
2016-09-08 18:43:58 +02:00
}
#endregion
#region DomainService
2021-03-31 13:26:31 +02:00
public void Handle(DomainSavedNotification notification)
2016-09-08 18:43:58 +02:00
{
2021-03-31 13:26:31 +02:00
foreach (IDomain entity in notification.SavedEntities)
{
2017-07-11 19:13:45 +02:00
_distributedCache.RefreshDomainCache(entity);
2021-03-31 13:26:31 +02:00
}
2016-09-08 18:43:58 +02:00
}
2021-03-31 13:26:31 +02:00
public void Handle(DomainDeletedNotification notification)
2016-09-08 18:43:58 +02:00
{
2021-03-31 13:26:31 +02:00
foreach (IDomain entity in notification.DeletedEntities)
{
2017-07-11 19:13:45 +02:00
_distributedCache.RemoveDomainCache(entity);
2021-03-31 13:26:31 +02:00
}
2016-09-08 18:43:58 +02:00
}
#endregion
#region LocalizationService / Language
2017-05-12 14:49:44 +02:00
2016-09-08 18:43:58 +02:00
/// <summary>
/// Fires when a language is deleted
2016-09-08 18:43:58 +02:00
/// </summary>
2021-03-25 14:51:00 +01:00
/// <param name="notification"></param>
public void Handle(LanguageDeletedNotification notification)
2016-09-08 18:43:58 +02:00
{
2021-03-25 14:51:00 +01:00
foreach (ILanguage entity in notification.DeletedEntities)
{
2017-07-11 19:13:45 +02:00
_distributedCache.RemoveLanguageCache(entity);
2021-03-25 14:51:00 +01:00
}
2016-09-08 18:43:58 +02:00
}
/// <summary>
/// Fires when a language is saved
2016-09-08 18:43:58 +02:00
/// </summary>
2021-03-25 14:51:00 +01:00
/// <param name="notification"></param>
public void Handle(LanguageSavedNotification notification)
2016-09-08 18:43:58 +02:00
{
2021-03-25 14:51:00 +01:00
foreach (ILanguage entity in notification.SavedEntities)
{
2017-07-11 19:13:45 +02:00
_distributedCache.RefreshLanguageCache(entity);
2021-03-25 14:51:00 +01:00
}
2016-09-08 18:43:58 +02:00
}
#endregion
#region Content|Media|MemberTypeService
2017-07-11 19:13:45 +02:00
private void ContentTypeService_Changed(IContentTypeService sender, ContentTypeChange<IContentType>.EventArgs args)
2016-09-08 18:43:58 +02:00
{
2017-07-11 19:13:45 +02:00
_distributedCache.RefreshContentTypeCache(args.Changes.ToArray());
2016-09-08 18:43:58 +02:00
}
2017-07-11 19:13:45 +02:00
private void MediaTypeService_Changed(IMediaTypeService sender, ContentTypeChange<IMediaType>.EventArgs args)
2016-09-08 18:43:58 +02:00
{
2017-07-11 19:13:45 +02:00
_distributedCache.RefreshContentTypeCache(args.Changes.ToArray());
2016-09-08 18:43:58 +02:00
}
2017-07-11 19:13:45 +02:00
private void MemberTypeService_Changed(IMemberTypeService sender, ContentTypeChange<IMemberType>.EventArgs args)
2016-09-08 18:43:58 +02:00
{
2017-07-11 19:13:45 +02:00
_distributedCache.RefreshContentTypeCache(args.Changes.ToArray());
2016-09-08 18:43:58 +02:00
}
// TODO: our weird events handling wants this for now
2017-07-11 19:13:45 +02:00
private void ContentTypeService_Saved(IContentTypeService sender, SaveEventArgs<IContentType> args) { }
private void MediaTypeService_Saved(IMediaTypeService sender, SaveEventArgs<IMediaType> args) { }
private void MemberTypeService_Saved(IMemberTypeService sender, SaveEventArgs<IMemberType> args) { }
private void ContentTypeService_Deleted(IContentTypeService sender, DeleteEventArgs<IContentType> args) { }
private void MediaTypeService_Deleted(IMediaTypeService sender, DeleteEventArgs<IMediaType> args) { }
private void MemberTypeService_Deleted(IMemberTypeService sender, DeleteEventArgs<IMemberType> args) { }
2017-06-23 18:54:42 +02:00
2016-09-08 18:43:58 +02:00
#endregion
2017-09-14 11:41:46 +02:00
#region UserService
2016-09-08 18:43:58 +02:00
2021-03-29 10:42:42 +02:00
public void Handle(UserSavedNotification notification)
2016-09-08 18:43:58 +02:00
{
2021-03-29 10:42:42 +02:00
foreach (IUser entity in notification.SavedEntities)
{
2017-07-11 19:13:45 +02:00
_distributedCache.RefreshUserCache(entity.Id);
2021-03-29 10:42:42 +02:00
}
2016-09-08 18:43:58 +02:00
}
2021-03-29 10:42:42 +02:00
public void Handle(UserDeletedNotification notification)
2016-09-08 18:43:58 +02:00
{
2021-03-29 10:42:42 +02:00
foreach (IUser entity in notification.DeletedEntities)
{
2017-07-11 19:13:45 +02:00
_distributedCache.RemoveUserCache(entity.Id);
2021-03-29 10:42:42 +02:00
}
2017-09-14 11:41:46 +02:00
}
2021-03-29 10:42:42 +02:00
public void Handle(UserGroupWithUsersSavedNotification notification)
2017-09-14 11:41:46 +02:00
{
2021-03-29 10:42:42 +02:00
foreach (UserGroupWithUsers entity in notification.SavedEntities)
{
2018-03-27 10:04:07 +02:00
_distributedCache.RefreshUserGroupCache(entity.UserGroup.Id);
2021-03-29 10:42:42 +02:00
}
2017-09-14 11:41:46 +02:00
}
2021-03-29 10:42:42 +02:00
public void Handle(UserGroupDeletedNotification notification)
2017-09-14 11:41:46 +02:00
{
2021-03-29 10:42:42 +02:00
foreach (IUserGroup entity in notification.DeletedEntities)
{
2017-09-14 11:41:46 +02:00
_distributedCache.RemoveUserGroupCache(entity.Id);
2021-03-29 10:42:42 +02:00
}
2016-09-08 18:43:58 +02:00
}
#endregion
2017-06-23 18:54:42 +02:00
#region FileService
2016-09-08 18:43:58 +02:00
/// <summary>
/// Removes cache for template
/// </summary>
2021-03-30 12:14:32 +02:00
/// <param name="notification"></param>
public void Handle(TemplateDeletedNotification notification)
2016-09-08 18:43:58 +02:00
{
2021-03-30 12:14:32 +02:00
foreach (ITemplate entity in notification.DeletedEntities)
{
2017-07-11 19:13:45 +02:00
_distributedCache.RemoveTemplateCache(entity.Id);
2021-03-30 12:14:32 +02:00
}
2016-09-08 18:43:58 +02:00
}
/// <summary>
/// Refresh cache for template
/// </summary>
/// <param name="notification"></param>
public void Handle(TemplateSavedNotification notification)
2016-09-08 18:43:58 +02:00
{
foreach (ITemplate entity in notification.SavedEntities)
{
2017-07-11 19:13:45 +02:00
_distributedCache.RefreshTemplateCache(entity.Id);
}
2016-09-08 18:43:58 +02:00
}
#endregion
#region MacroService
public void Handle(MacroDeletedNotification notification)
2016-09-08 18:43:58 +02:00
{
foreach (IMacro entity in notification.DeletedEntities)
{
_distributedCache.RemoveMacroCache(entity);
}
2016-09-08 18:43:58 +02:00
}
public void Handle(MacroSavedNotification notification)
2016-09-08 18:43:58 +02:00
{
foreach (IMacro entity in notification.SavedEntities)
{
_distributedCache.RefreshMacroCache(entity);
}
2016-09-08 18:43:58 +02:00
}
#endregion
#region MediaService
private void MediaService_TreeChanged(IMediaService sender, TreeChange<IMedia>.EventArgs args)
2016-09-08 18:43:58 +02:00
{
2017-07-11 19:13:45 +02:00
_distributedCache.RefreshMediaCache(args.Changes.ToArray());
2016-09-08 18:43:58 +02:00
}
#endregion
#region MemberService
2021-03-29 10:42:42 +02:00
public void Handle(MemberDeletedNotification notification)
2016-09-08 18:43:58 +02:00
{
2021-03-29 10:42:42 +02:00
_distributedCache.RemoveMemberCache(notification.DeletedEntities.ToArray());
2016-09-08 18:43:58 +02:00
}
2021-03-29 10:42:42 +02:00
public void Handle(MemberSavedNotification notification)
2016-09-08 18:43:58 +02:00
{
2021-03-29 10:42:42 +02:00
_distributedCache.RefreshMemberCache(notification.SavedEntities.ToArray());
2016-09-08 18:43:58 +02:00
}
#endregion
#region MemberGroupService
/// <summary>
/// Fires when a member group is deleted
/// </summary>
/// <param name="notification"></param>
public void Handle(MemberGroupDeletedNotification notification)
2016-09-08 18:43:58 +02:00
{
foreach (IMemberGroup entity in notification.DeletedEntities)
2016-09-08 18:43:58 +02:00
{
_distributedCache.RemoveMemberGroupCache(entity.Id);
2016-09-08 18:43:58 +02:00
}
}
/// <summary>
/// Fires when a member group is saved
/// </summary>
/// <param name="notification"></param>
public void Handle(MemberGroupSavedNotification notification)
2016-09-08 18:43:58 +02:00
{
foreach (IMemberGroup entity in notification.SavedEntities)
2016-09-08 18:43:58 +02:00
{
_distributedCache.RemoveMemberGroupCache(entity.Id);
2016-09-08 18:43:58 +02:00
}
}
2016-09-08 18:43:58 +02:00
#endregion
2017-05-12 14:49:44 +02:00
#region RelationType
2021-03-31 11:52:36 +02:00
public void Handle(RelationTypeSavedNotification notification)
2017-05-12 14:49:44 +02:00
{
2021-03-31 11:52:36 +02:00
DistributedCache dc = _distributedCache;
foreach (IRelationType entity in notification.SavedEntities)
{
dc.RefreshRelationTypeCache(entity.Id);
}
2017-05-12 14:49:44 +02:00
}
2021-03-31 11:52:36 +02:00
public void Handle(RelationTypeDeletedNotification notification)
2017-05-12 14:49:44 +02:00
{
2021-03-31 11:52:36 +02:00
DistributedCache dc = _distributedCache;
foreach (IRelationType entity in notification.DeletedEntities)
{
dc.RemoveRelationTypeCache(entity.Id);
}
2017-05-12 14:49:44 +02:00
}
2017-07-20 11:21:28 +02:00
2017-05-12 14:49:44 +02:00
#endregion
2016-09-08 18:43:58 +02:00
}
2017-07-20 11:21:28 +02:00
}