From a288fc62ebf67c7c9cece304cd5ba87d32280540 Mon Sep 17 00:00:00 2001 From: Sebastiaan Jansssen Date: Wed, 24 Jan 2018 17:26:21 +0100 Subject: [PATCH] Workaround: keep the cache we used to have but clear all cache for membergroups when a member group changes --- .../Repositories/MemberGroupRepository.cs | 23 +++++++++-------- .../Cache/MemberGroupCacheRefresher.cs | 25 +++++++------------ 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/Umbraco.Core/Persistence/Repositories/MemberGroupRepository.cs b/src/Umbraco.Core/Persistence/Repositories/MemberGroupRepository.cs index a477b6fd40..455e2faf38 100644 --- a/src/Umbraco.Core/Persistence/Repositories/MemberGroupRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/MemberGroupRepository.cs @@ -131,17 +131,18 @@ namespace Umbraco.Core.Persistence.Repositories public IMemberGroup GetByName(string name) { - // cannot use name in the cache key, 'cos when clearing the cache for a given - // member group that has been renamed, we clear the new name but not the old - // name - it just cannot work - and we cannot use the dirty props 'cos it won't - // work on distributed cache - and besides, we end up caching both by name and - // by id - // disabling the cache entirely for now - if we want to cache we'd need to - // implement some sort of cross-reference name/id cache - - var qry = new Query().Where(group => group.Name.Equals(name)); - var result = GetByQuery(qry); - return result.FirstOrDefault(); + return IsolatedCache.GetCacheItem( + string.Format("{0}.{1}", typeof(IMemberGroup).FullName, name), + () => + { + var qry = new Query().Where(group => group.Name.Equals(name)); + var result = GetByQuery(qry); + return result.FirstOrDefault(); + }, + //cache for 5 mins since that is the default in the RuntimeCacheProvider + TimeSpan.FromMinutes(5), + //sliding is true + true); } public IMemberGroup CreateIfNotExists(string roleName) diff --git a/src/Umbraco.Web/Cache/MemberGroupCacheRefresher.cs b/src/Umbraco.Web/Cache/MemberGroupCacheRefresher.cs index a27dca47ca..6d3ce6bb57 100644 --- a/src/Umbraco.Web/Cache/MemberGroupCacheRefresher.cs +++ b/src/Umbraco.Web/Cache/MemberGroupCacheRefresher.cs @@ -1,6 +1,6 @@ using System; using System.Linq; -using System.Web.Script.Serialization; +using System.Web; using Newtonsoft.Json; using Umbraco.Core; using Umbraco.Core.Cache; @@ -83,35 +83,28 @@ namespace Umbraco.Web.Cache public override void Refresh(string jsonPayload) { - ClearCache(DeserializeFromJsonPayload(jsonPayload)); + ClearCache(); base.Refresh(jsonPayload); } public override void Refresh(int id) { - ClearCache(FromMemberGroup(ApplicationContext.Current.Services.MemberGroupService.GetById(id))); + ClearCache(); base.Refresh(id); } public override void Remove(int id) { - ClearCache(FromMemberGroup(ApplicationContext.Current.Services.MemberGroupService.GetById(id))); + ClearCache(); base.Remove(id); } - private void ClearCache(params JsonPayload[] payloads) + private void ClearCache() { - if (payloads == null) return; - - var memberGroupCache = ApplicationContext.Current.ApplicationCache.IsolatedRuntimeCache.GetCache(); - payloads.ForEach(payload => - { - if (payload != null && memberGroupCache) - { - memberGroupCache.Result.ClearCacheItem(RepositoryBase.GetCacheIdKey(payload.Id)); - } - }); - + // Since we cache by group name, it could be problematic when renaming to + // previously existing names - see http://issues.umbraco.org/issue/U4-10846. + // To work around this, just clear all the cache items + ApplicationContext.Current.ApplicationCache.IsolatedRuntimeCache.ClearCache(); } } }