From c0b708b1f4ed5993ca7710b47320be701b3837bd Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Thu, 7 Feb 2013 05:53:59 +0600 Subject: [PATCH] Streamlines how Media cache gets invalidated - ensures Save() is called on media where it was needed, cache now gets invalidated on events. --- .../Cache/CacheRefresherEventHandler.cs | 10 ++-- .../Cache/DistributedCacheExtensions.cs | 20 +++++++ src/Umbraco.Web/Cache/MediaCacheRefresher.cs | 58 +++++++++++++++++++ .../Cache/MediaLibraryRefreshers.cs | 38 ------------ src/Umbraco.Web/Cache/MemberCacheRefresher.cs | 23 +++++--- src/Umbraco.Web/CacheHelperExtensions.cs | 39 +------------ src/Umbraco.Web/Umbraco.Web.csproj | 2 +- .../umbraco.presentation/library.cs | 4 +- .../umbraco/cache/LegacyClasses.cs | 2 +- .../umbraco/dialogs/moveOrCopy.aspx.cs | 2 +- 10 files changed, 104 insertions(+), 94 deletions(-) create mode 100644 src/Umbraco.Web/Cache/MediaCacheRefresher.cs delete mode 100644 src/Umbraco.Web/Cache/MediaLibraryRefreshers.cs diff --git a/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs b/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs index 4e86392fd7..f5f4d35b14 100644 --- a/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs +++ b/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs @@ -86,19 +86,19 @@ namespace Umbraco.Web.Cache static void MediaServiceTrashing(IMediaService sender, Core.Events.MoveEventArgs e) { - ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(e.Entity.Id); + DistributedCache.Instance.RemoveMediaCache(e.Entity.Id); } static void MediaServiceMoving(IMediaService sender, Core.Events.MoveEventArgs e) { - ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(e.Entity.Id); + DistributedCache.Instance.RefreshMediaCache(e.Entity.Id); } static void MediaServiceDeleting(IMediaService sender, Core.Events.DeleteEventArgs e) { foreach (var item in e.DeletedEntities) { - ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(item.Id); + DistributedCache.Instance.RemoveMediaCache(item.Id); } } @@ -106,13 +106,13 @@ namespace Umbraco.Web.Cache { foreach (var item in e.SavedEntities) { - ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(item.Id); + DistributedCache.Instance.RefreshMediaCache(item.Id); } } static void MemberBeforeDelete(Member sender, DeleteEventArgs e) { - DistributedCache.Instance.RefreshMemberCache(sender.Id); + DistributedCache.Instance.RemoveMemberCache(sender.Id); } static void MemberAfterSave(Member sender, SaveEventArgs e) diff --git a/src/Umbraco.Web/Cache/DistributedCacheExtensions.cs b/src/Umbraco.Web/Cache/DistributedCacheExtensions.cs index b9be4500e9..7b6f1c8dae 100644 --- a/src/Umbraco.Web/Cache/DistributedCacheExtensions.cs +++ b/src/Umbraco.Web/Cache/DistributedCacheExtensions.cs @@ -66,6 +66,16 @@ namespace Umbraco.Web.Cache dc.Refresh(new Guid(DistributedCache.MemberCacheRefresherId), memberId); } + /// + /// Removes the cache amongst servers for a member + /// + /// + /// + public static void RemoveMemberCache(this DistributedCache dc, int memberId) + { + dc.Remove(new Guid(DistributedCache.MemberCacheRefresherId), memberId); + } + /// /// Refreshes the cache amongst servers for a media item /// @@ -76,6 +86,16 @@ namespace Umbraco.Web.Cache dc.Refresh(new Guid(DistributedCache.MediaCacheRefresherId), mediaId); } + /// + /// Removes the cache amongst servers for a media item + /// + /// + /// + public static void RemoveMediaCache(this DistributedCache dc, int mediaId) + { + dc.Remove(new Guid(DistributedCache.MediaCacheRefresherId), mediaId); + } + /// /// Refreshes the cache amongst servers for a macro item /// diff --git a/src/Umbraco.Web/Cache/MediaCacheRefresher.cs b/src/Umbraco.Web/Cache/MediaCacheRefresher.cs new file mode 100644 index 0000000000..b5efa092fc --- /dev/null +++ b/src/Umbraco.Web/Cache/MediaCacheRefresher.cs @@ -0,0 +1,58 @@ +using System; +using Umbraco.Core; +using umbraco.interfaces; + +namespace Umbraco.Web.Cache +{ + public class MediaCacheRefresher : ICacheRefresher + { + const string getmediaCacheKey = "GetMedia"; + + public Guid UniqueIdentifier + { + get { return new Guid(DistributedCache.MediaCacheRefresherId); } + } + + public string Name + { + get { return "Clears Media Cache from umbraco.library"; } + } + + public void RefreshAll() + { + } + + public void Refresh(int id) + { + ClearCache(id); + } + + public void Remove(int id) + { + ClearCache(id); + } + + public void Refresh(Guid id) + { + } + + private static void ClearCache(int id) + { + var m = ApplicationContext.Current.Services.MediaService.GetById(id); + if (m == null) return; + + foreach (var idPart in m.Path.Split(',')) + { + ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch( + string.Format("UL_{0}_{1}_True", getmediaCacheKey, idPart)); + + // Also clear calls that only query this specific item! + if (idPart == m.Id.ToString()) + ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch( + string.Format("UL_{0}_{1}", getmediaCacheKey, id)); + + } + } + + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Cache/MediaLibraryRefreshers.cs b/src/Umbraco.Web/Cache/MediaLibraryRefreshers.cs deleted file mode 100644 index aba1565aeb..0000000000 --- a/src/Umbraco.Web/Cache/MediaLibraryRefreshers.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using Umbraco.Core; -using umbraco.interfaces; - -namespace Umbraco.Web.Cache -{ - public class MediaLibraryRefreshers : ICacheRefresher - { - - public Guid UniqueIdentifier - { - get { return new Guid(DistributedCache.MediaCacheRefresherId); } - } - - public string Name - { - get { return "Clears Media Cache from umbraco.library"; } - } - - public void RefreshAll() - { - } - - public void Refresh(int id) - { - ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(id, false); - } - - public void Remove(int id) - { - } - - public void Refresh(Guid id) - { - } - - } -} \ No newline at end of file diff --git a/src/Umbraco.Web/Cache/MemberCacheRefresher.cs b/src/Umbraco.Web/Cache/MemberCacheRefresher.cs index 18bb9f3e3e..ce044ac6a9 100644 --- a/src/Umbraco.Web/Cache/MemberCacheRefresher.cs +++ b/src/Umbraco.Web/Cache/MemberCacheRefresher.cs @@ -22,6 +22,21 @@ namespace Umbraco.Web.Cache } public void Refresh(int id) + { + ClearCache(id); + } + + public void Remove(int id) + { + ClearCache(id); + } + + public void Refresh(Guid id) + { + + } + + private void ClearCache(int id) { const string getmemberCacheKey = "GetMember"; @@ -29,13 +44,5 @@ namespace Umbraco.Web.Cache ClearCacheByKeySearch(string.Format("UL_{0}_{1}", getmemberCacheKey, id)); } - public void Remove(int id) - { - } - - public void Refresh(Guid id) - { - } - } } \ No newline at end of file diff --git a/src/Umbraco.Web/CacheHelperExtensions.cs b/src/Umbraco.Web/CacheHelperExtensions.cs index e2bb75716f..780f841190 100644 --- a/src/Umbraco.Web/CacheHelperExtensions.cs +++ b/src/Umbraco.Web/CacheHelperExtensions.cs @@ -46,44 +46,7 @@ namespace Umbraco.Web } public const string PartialViewCacheKey = "Umbraco.Web.PartialViewCacheKey"; - - /// - /// Clears the library cache for media - /// - /// - /// - /// - /// If set to false, this will only clear the library cache for the current server, not all servers registered in the - /// server farm. In most cases if you are clearing cache you would probably clear it on all servers. - /// - public static void ClearLibraryCacheForMedia(this CacheHelper cacheHelper, int mediaId, bool allServers = true) - { - const string getmediaCacheKey = "GetMedia"; - - if (allServers && UmbracoSettings.UseDistributedCalls) - { - DistributedCache.Instance.RefreshMediaCache(mediaId); - } - else - { - var m = new global::umbraco.cms.businesslogic.media.Media(mediaId); - if (m.nodeObjectType == global::umbraco.cms.businesslogic.media.Media._objectType) - { - foreach (string id in m.Path.Split(',')) - { - cacheHelper.ClearCacheByKeySearch( - string.Format("UL_{0}_{1}_True", getmediaCacheKey, id)); - - // Also clear calls that only query this specific item! - if (id == m.Id.ToString()) - cacheHelper.ClearCacheByKeySearch( - string.Format("UL_{0}_{1}", getmediaCacheKey, id)); - - } - } - } - } - + /// /// Outputs and caches a partial view in MVC /// diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 8388ed97e9..de75f975b0 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -249,7 +249,7 @@ - + diff --git a/src/Umbraco.Web/umbraco.presentation/library.cs b/src/Umbraco.Web/umbraco.presentation/library.cs index fe2bb93688..7ea043ece2 100644 --- a/src/Umbraco.Web/umbraco.presentation/library.cs +++ b/src/Umbraco.Web/umbraco.presentation/library.cs @@ -1852,13 +1852,13 @@ namespace umbraco [Obsolete("Use ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia instead")] public static void ClearLibraryCacheForMedia(int mediaId) { - ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(mediaId); + DistributedCache.Instance.RemoveMediaCache(mediaId); } [Obsolete("Use ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia with the allServers flag set to false instead")] public static void ClearLibraryCacheForMediaDo(int mediaId) { - ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(mediaId, false); + DistributedCache.Instance.RemoveMediaCache(mediaId); } [Obsolete("Use ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMember instead")] diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/cache/LegacyClasses.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/cache/LegacyClasses.cs index e05488bb7f..10f0f9d874 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/cache/LegacyClasses.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/cache/LegacyClasses.cs @@ -14,7 +14,7 @@ namespace umbraco.presentation.cache } [Obsolete("This class is no longer used, use Umbraco.Web.Cache.MediaLibraryRefreshers instead")] - public class MediaLibraryRefreshers : Umbraco.Web.Cache.MediaLibraryRefreshers + public class MediaLibraryRefreshers : Umbraco.Web.Cache.MediaCacheRefresher { } diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/dialogs/moveOrCopy.aspx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/dialogs/moveOrCopy.aspx.cs index aeab148ef7..e3a78f7b08 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/dialogs/moveOrCopy.aspx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/dialogs/moveOrCopy.aspx.cs @@ -269,7 +269,7 @@ namespace umbraco.dialogs media.Move(int.Parse(UmbracoContext.Current.Request["copyTo"])); media = new Media(int.Parse(UmbracoContext.Current.Request["id"])); media.XmlGenerate(new XmlDocument()); - library.ClearLibraryCacheForMedia(media.Id); + media.Save(); } feedback.Text = ui.Text("moveOrCopy", "moveDone", nodes, base.getUser()) + "

" + ui.Text("closeThisWindow") + "";