From 15d7443c6117fcb4437b0f7eb52b372a36478728 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 28 Jan 2014 12:13:27 +1100 Subject: [PATCH] Streamlines the content type cache refreshing, leaving some of the legacy cache clearing but that shouldn't really have an effect, with these new changes it should be guaranteed that the distributed cache is executed always for Doc, media and member types. --- .../Repositories/ContentTypeBaseRepository.cs | 16 +--------- .../Cache/CacheRefresherEventHandler.cs | 31 +++++++++---------- .../Cache/ContentTypeCacheRefresher.cs | 23 ++++++++++++-- src/umbraco.cms/businesslogic/ContentType.cs | 2 +- .../businesslogic/media/MediaType.cs | 4 --- .../propertytype/propertytype.cs | 1 + .../businesslogic/web/DocumentType.cs | 10 +----- 7 files changed, 38 insertions(+), 49 deletions(-) diff --git a/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs index e3dd5d1301..ad27b6e757 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs @@ -54,21 +54,7 @@ namespace Umbraco.Core.Persistence.Repositories yield return dto.ContentTypeNodeId; } } - - /// - /// We need to override this method to ensure that any content cache is cleared - /// - /// - /// - /// see: http://issues.umbraco.org/issue/U4-1963 - /// - public override void PersistUpdatedItem(IEntity entity) - { - InMemoryCacheProvider.Current.Clear(typeof(IContent)); - RuntimeCacheProvider.Current.Clear(typeof(IContent)); - base.PersistUpdatedItem(entity); - } - + protected void PersistNewBaseContentType(ContentTypeDto dto, IContentTypeComposition entity) { //Logic for setting Path, Level and SortOrder diff --git a/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs b/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs index 3b76060b9a..ca2861ab5c 100644 --- a/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs +++ b/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs @@ -33,9 +33,11 @@ namespace Umbraco.Web.Cache Application.Deleted += ApplicationDeleted; Application.New += ApplicationNew; - //bind to user type events + //bind to user / user type events UserService.SavedUserType += UserServiceSavedUserType; UserService.DeletedUserType += UserServiceDeletedUserType; + UserService.SavedUser += UserServiceSavedUser; + UserService.DeletedUser += UserServiceDeletedUser; //Bind to dictionary events //NOTE: we need to bind to legacy and new API events currently: http://issues.umbraco.org/issue/U4-1979 @@ -88,11 +90,6 @@ namespace Umbraco.Web.Cache MemberTypeService.Saved += MemberTypeServiceSaved; MemberTypeService.Deleted += MemberTypeServiceDeleted; - //Bind to user events - - User.Saving += UserSaving; - User.Deleting += UserDeleting; - //Bind to permission events Permission.New += PermissionNew; @@ -130,7 +127,7 @@ namespace Umbraco.Web.Cache ContentService.Created += ContentServiceCreated; ContentService.Copied += ContentServiceCopied; } - + #region Content service event handlers /// @@ -440,17 +437,17 @@ namespace Umbraco.Web.Cache { InvalidateCacheForPermissionsChange(sender); } + + void UserServiceSavedUser(IUserService sender, Core.Events.SaveEventArgs e) + { + e.SavedEntities.ForEach(x => DistributedCache.Instance.RefreshUserCache(x.Id)); + } + + void UserServiceDeletedUser(IUserService sender, Core.Events.DeleteEventArgs e) + { + e.DeletedEntities.ForEach(x => DistributedCache.Instance.RemoveUserCache(x.Id)); + } - static void UserDeleting(User sender, System.EventArgs e) - { - DistributedCache.Instance.RemoveUserCache(sender.Id); - } - - static void UserSaving(User sender, System.EventArgs e) - { - DistributedCache.Instance.RefreshUserCache(sender.Id); - } - private static void InvalidateCacheForPermissionsChange(UserPermission sender) { if (sender.User != null) diff --git a/src/Umbraco.Web/Cache/ContentTypeCacheRefresher.cs b/src/Umbraco.Web/Cache/ContentTypeCacheRefresher.cs index 983529fd01..f07bc02b1d 100644 --- a/src/Umbraco.Web/Cache/ContentTypeCacheRefresher.cs +++ b/src/Umbraco.Web/Cache/ContentTypeCacheRefresher.cs @@ -122,6 +122,9 @@ namespace Umbraco.Web.Cache public override void RefreshAll() { + RuntimeCacheProvider.Current.Clear(typeof(IContent)); + RuntimeCacheProvider.Current.Clear(typeof(IContentType)); + //all property type cache ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(CacheKeys.PropertyTypeCacheKey); //all content type property cache @@ -173,7 +176,7 @@ namespace Umbraco.Web.Cache /// - RuntimeCacheProvider.Current.Clear(); /// - RoutesCache.Clear(); /// - private static void ClearContentTypeCache(IEnumerable payloads) + private static void ClearContentTypeCache(JsonPayload[] payloads) { var needsContentRefresh = false; @@ -203,8 +206,22 @@ namespace Umbraco.Web.Cache //clear the cache providers if there were any content types to clear if (payloads.Any()) { - InMemoryCacheProvider.Current.Clear(); - RuntimeCacheProvider.Current.Clear(); + if (payloads.Any(x => x.Type == typeof (IContentType).Name)) + { + RuntimeCacheProvider.Current.Clear(typeof(IContent)); + RuntimeCacheProvider.Current.Clear(typeof(IContentType)); + } + if (payloads.Any(x => x.Type == typeof(IMediaType).Name)) + { + RuntimeCacheProvider.Current.Clear(typeof(IMedia)); + RuntimeCacheProvider.Current.Clear(typeof(IMediaType)); + } + if (payloads.Any(x => x.Type == typeof(IMemberType).Name)) + { + RuntimeCacheProvider.Current.Clear(typeof(IMember)); + RuntimeCacheProvider.Current.Clear(typeof(IMemberType)); + } + //we only need to do this for IContentType NOT for IMediaType, we don't want to refresh the whole routes //cache if only a media type has changed. diff --git a/src/umbraco.cms/businesslogic/ContentType.cs b/src/umbraco.cms/businesslogic/ContentType.cs index b31be8a804..e10ac4aa25 100644 --- a/src/umbraco.cms/businesslogic/ContentType.cs +++ b/src/umbraco.cms/businesslogic/ContentType.cs @@ -30,7 +30,7 @@ namespace umbraco.cms.businesslogic /// Besides data definition, the ContentType also defines the sorting and grouping (in tabs) of Properties/Datafields /// on the Content and which Content (by ContentType) can be created as child to the Content of the ContentType. /// - [Obsolete("Obsolete, Use Umbraco.Core.Models.ContentType or Umbraco.Core.Models.MediaType", false)] + [Obsolete("Obsolete, Use Umbraco.Core.Models.ContentType or Umbraco.Core.Models.MediaType or or Umbraco.Core.Models.MemberType", false)] public class ContentType : CMSNode { #region Constructors diff --git a/src/umbraco.cms/businesslogic/media/MediaType.cs b/src/umbraco.cms/businesslogic/media/MediaType.cs index 12059a7a20..c597774472 100644 --- a/src/umbraco.cms/businesslogic/media/MediaType.cs +++ b/src/umbraco.cms/businesslogic/media/MediaType.cs @@ -133,10 +133,6 @@ namespace umbraco.cms.businesslogic.media int userId = current == null ? 0 : current.Id; ApplicationContext.Current.Services.ContentTypeService.Save(_mediaType, userId); - //Ensure that MediaTypes are reloaded from db by clearing cache - InMemoryCacheProvider.Current.Clear(); - RuntimeCacheProvider.Current.Clear(); - base.Save(); FireAfterSave(e); diff --git a/src/umbraco.cms/businesslogic/propertytype/propertytype.cs b/src/umbraco.cms/businesslogic/propertytype/propertytype.cs index 70ed1771c1..7a5804dc1f 100644 --- a/src/umbraco.cms/businesslogic/propertytype/propertytype.cs +++ b/src/umbraco.cms/businesslogic/propertytype/propertytype.cs @@ -21,6 +21,7 @@ namespace umbraco.cms.businesslogic.propertytype /// /// Summary description for propertytype. /// + [Obsolete("Use the ContentTypeService instead")] public class PropertyType { #region Declarations diff --git a/src/umbraco.cms/businesslogic/web/DocumentType.cs b/src/umbraco.cms/businesslogic/web/DocumentType.cs index 154e374c04..4f8c6dab87 100644 --- a/src/umbraco.cms/businesslogic/web/DocumentType.cs +++ b/src/umbraco.cms/businesslogic/web/DocumentType.cs @@ -452,10 +452,7 @@ namespace umbraco.cms.businesslogic.web { throw new ArgumentException("Can't delete a Document Type used as a Master Content Type. Please remove all references first!"); } - - // Remove from cache - FlushFromCache(Id); - + ApplicationContext.Current.Services.ContentTypeService.Delete(_contentType); clearTemplates(); @@ -592,11 +589,6 @@ namespace umbraco.cms.businesslogic.web int userId = current == null ? 0 : current.Id; ApplicationContext.Current.Services.ContentTypeService.Save(_contentType, userId); - //Ensure that DocumentTypes are reloaded from db by clearing cache. - //NOTE Would be nice if we could clear cache by type instead of emptying the entire cache. - InMemoryCacheProvider.Current.Clear(); - RuntimeCacheProvider.Current.Clear();//Runtime cache is used for Content, so we clear that as well - base.Save(); FireAfterSave(e); }