From 75e747a2d20e6067b245da1f1b489534bc26e8c6 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Thu, 7 Feb 2013 04:26:48 +0600 Subject: [PATCH] Simplifies clearing cache for macro's all is done via the DistributedCache.Instance - hopefully all cache will be invalidated via this method so that all cache types are simply invalidated with ICacheRefreshers and ApplicationEventHandlers. Currently we have calls to clear cache in zillions of places and its near impossible to tell where/when it currently happens. --- src/Umbraco.Web/Cache/DistributedCache.cs | 128 +++++++----------- .../Cache/DistributedCacheExtensions.cs | 79 +++++++++++ src/Umbraco.Web/Umbraco.Web.csproj | 1 + .../developer/Macros/editMacro.aspx.cs | 9 +- 4 files changed, 133 insertions(+), 84 deletions(-) create mode 100644 src/Umbraco.Web/Cache/DistributedCacheExtensions.cs diff --git a/src/Umbraco.Web/Cache/DistributedCache.cs b/src/Umbraco.Web/Cache/DistributedCache.cs index 664622c429..9a44b115a0 100644 --- a/src/Umbraco.Web/Cache/DistributedCache.cs +++ b/src/Umbraco.Web/Cache/DistributedCache.cs @@ -10,81 +10,10 @@ using Umbraco.Core.Configuration; using Umbraco.Core.IO; using Umbraco.Core.Logging; using umbraco.BusinessLogic; +using umbraco.interfaces; namespace Umbraco.Web.Cache { - public static class DistributedCacheExtensions - { - /// - /// Refreshes the cache amongst servers for a template - /// - /// - /// - public static void RefreshTemplateCache(this DistributedCache dc, int templateId) - { - dc.Refresh(new Guid(DistributedCache.TemplateRefresherId), templateId); - } - - /// - /// Refreshes the cache amongst servers for all pages - /// - /// - public static void RefreshAllPageCache(this DistributedCache dc) - { - dc.RefreshAll(new Guid(DistributedCache.PageCacheRefresherId)); - } - - /// - /// Refreshes the cache amongst servers for a page - /// - /// - /// - public static void RefreshPageCache(this DistributedCache dc, int pageId) - { - dc.Refresh(new Guid(DistributedCache.PageCacheRefresherId), pageId); - } - - /// - /// Removes the cache amongst servers for a page - /// - /// - /// - public static void RemovePageCache(this DistributedCache dc, int pageId) - { - dc.Remove(new Guid(DistributedCache.PageCacheRefresherId), pageId); - } - - /// - /// Refreshes the cache amongst servers for a member - /// - /// - /// - public static void RefreshMemberCache(this DistributedCache dc, int memberId) - { - dc.Refresh(new Guid(DistributedCache.MemberCacheRefresherId), memberId); - } - - /// - /// Refreshes the cache amongst servers for a media item - /// - /// - /// - public static void RefreshMediaCache(this DistributedCache dc, int mediaId) - { - dc.Refresh(new Guid(DistributedCache.MediaCacheRefresherId), mediaId); - } - - /// - /// Refreshes the cache amongst servers for a macro item - /// - /// - /// - public static void RefreshMacroCache(this DistributedCache dc, int macroId) - { - dc.Refresh(new Guid(DistributedCache.MacroCacheRefresherId), macroId); - } - } - /// /// DistrubutedCacheDispatcher is used to handle Umbraco's load balancing. /// @@ -181,6 +110,33 @@ namespace Umbraco.Web.Cache InvokeDispatchMethod(DispatchType.RemoveById, factoryGuid, id, Guid.Empty); } + /// + /// Used to invoke the method on an ICacheRefresher instance if we are not currently using distributed calls. + /// + /// + /// + /// + /// + private void InvokeMethodOnRefresherInstance(ICacheRefresher refresher, DispatchType dispatchType, int numericId, Guid guidId) + { + //if we are not, then just invoke the call on the cache refresher + switch (dispatchType) + { + case DispatchType.RefreshAll: + refresher.RefreshAll(); + break; + case DispatchType.RefreshByNumericId: + refresher.Refresh(numericId); + break; + case DispatchType.RefreshByGuid: + refresher.Refresh(guidId); + break; + case DispatchType.RemoveById: + refresher.Remove(numericId); + break; + } + } + /// /// Invokes the relevant dispatch method. /// @@ -190,9 +146,25 @@ namespace Umbraco.Web.Cache /// The GUID id. private void InvokeDispatchMethod(DispatchType dispatchType, Guid factoryGuid, int numericId, Guid guidId) { - //TODO: THIS IS NOT USED, WHY IS IT HERE?? - var name = GetFactoryObjectName(factoryGuid); + //get the refresher, it must be found or else we cannot continue + var refresher = GetRefresherById(factoryGuid); + if (refresher == null) + { + var ex = new InvalidOperationException( + "Could not find an " + typeof(ICacheRefresher).Name + " with the Id " + guidId); + LogHelper.Error("Could not continue with DistributedCache call", ex); + return; + } + + //Now, check if we are using Distrubuted calls + if (!UmbracoSettings.UseDistributedCalls) + { + //if we are not, then just invoke the call on the cache refresher + InvokeMethodOnRefresherInstance(refresher, dispatchType, numericId, guidId); + return; + } + //We are using distributed calls, so lets make them... try { using (var cacheRefresher = new CacheRefresherClient()) @@ -320,11 +292,9 @@ namespace Umbraco.Web.Cache cr.Url = string.Format("{0}://{1}{2}/cacheRefresher.asmx", protocol, domain, _webServicesUrl); } - private string GetFactoryObjectName(Guid uniqueIdentifier) - { - var cacheRefresher = CacheRefreshersResolver.Current.GetById(uniqueIdentifier); - - return cacheRefresher != null ? cacheRefresher.Name : ""; + private static ICacheRefresher GetRefresherById(Guid uniqueIdentifier) + { + return CacheRefreshersResolver.Current.GetById(uniqueIdentifier); } private void LogStartDispatch() diff --git a/src/Umbraco.Web/Cache/DistributedCacheExtensions.cs b/src/Umbraco.Web/Cache/DistributedCacheExtensions.cs new file mode 100644 index 0000000000..4c1e67ba75 --- /dev/null +++ b/src/Umbraco.Web/Cache/DistributedCacheExtensions.cs @@ -0,0 +1,79 @@ +using System; + +namespace Umbraco.Web.Cache +{ + /// + /// Extension methods for DistrubutedCache + /// + public static class DistributedCacheExtensions + { + /// + /// Refreshes the cache amongst servers for a template + /// + /// + /// + public static void RefreshTemplateCache(this DistributedCache dc, int templateId) + { + dc.Refresh(new Guid(DistributedCache.TemplateRefresherId), templateId); + } + + /// + /// Refreshes the cache amongst servers for all pages + /// + /// + public static void RefreshAllPageCache(this DistributedCache dc) + { + dc.RefreshAll(new Guid(DistributedCache.PageCacheRefresherId)); + } + + /// + /// Refreshes the cache amongst servers for a page + /// + /// + /// + public static void RefreshPageCache(this DistributedCache dc, int pageId) + { + dc.Refresh(new Guid(DistributedCache.PageCacheRefresherId), pageId); + } + + /// + /// Removes the cache amongst servers for a page + /// + /// + /// + public static void RemovePageCache(this DistributedCache dc, int pageId) + { + dc.Remove(new Guid(DistributedCache.PageCacheRefresherId), pageId); + } + + /// + /// Refreshes the cache amongst servers for a member + /// + /// + /// + public static void RefreshMemberCache(this DistributedCache dc, int memberId) + { + dc.Refresh(new Guid(DistributedCache.MemberCacheRefresherId), memberId); + } + + /// + /// Refreshes the cache amongst servers for a media item + /// + /// + /// + public static void RefreshMediaCache(this DistributedCache dc, int mediaId) + { + dc.Refresh(new Guid(DistributedCache.MediaCacheRefresherId), mediaId); + } + + /// + /// Refreshes the cache amongst servers for a macro item + /// + /// + /// + public static void RefreshMacroCache(this DistributedCache dc, int macroId) + { + dc.Refresh(new Guid(DistributedCache.MacroCacheRefresherId), macroId); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 0abc1792f3..baf582add3 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -246,6 +246,7 @@ + diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Macros/editMacro.aspx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Macros/editMacro.aspx.cs index ddbe7cb184..44685e9c0b 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Macros/editMacro.aspx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/developer/Macros/editMacro.aspx.cs @@ -108,11 +108,10 @@ namespace umbraco.cms.presentation.developer mp.Save(); } - // Flush macro from cache! - if (UmbracoSettings.UseDistributedCalls) - DistributedCache.Instance.RefreshMacroCache(macroID); - else - macro.GetMacro(macroID).removeFromCache(); + + // Flush macro from cache! + DistributedCache.Instance.RefreshMacroCache(macroID); + ClientTools.ShowSpeechBubble(speechBubbleIcon.save, "Macro saved", "");