Streamlines how Media cache gets invalidated - ensures Save() is called on media where it was needed,

cache now gets invalidated on events.
This commit is contained in:
Shannon Deminick
2013-02-07 05:53:59 +06:00
parent d93ca26181
commit c0b708b1f4
10 changed files with 104 additions and 94 deletions

View File

@@ -86,19 +86,19 @@ namespace Umbraco.Web.Cache
static void MediaServiceTrashing(IMediaService sender, Core.Events.MoveEventArgs<Core.Models.IMedia> e)
{
ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(e.Entity.Id);
DistributedCache.Instance.RemoveMediaCache(e.Entity.Id);
}
static void MediaServiceMoving(IMediaService sender, Core.Events.MoveEventArgs<Core.Models.IMedia> e)
{
ApplicationContext.Current.ApplicationCache.ClearLibraryCacheForMedia(e.Entity.Id);
DistributedCache.Instance.RefreshMediaCache(e.Entity.Id);
}
static void MediaServiceDeleting(IMediaService sender, Core.Events.DeleteEventArgs<Core.Models.IMedia> 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)

View File

@@ -66,6 +66,16 @@ namespace Umbraco.Web.Cache
dc.Refresh(new Guid(DistributedCache.MemberCacheRefresherId), memberId);
}
/// <summary>
/// Removes the cache amongst servers for a member
/// </summary>
/// <param name="dc"></param>
/// <param name="memberId"></param>
public static void RemoveMemberCache(this DistributedCache dc, int memberId)
{
dc.Remove(new Guid(DistributedCache.MemberCacheRefresherId), memberId);
}
/// <summary>
/// Refreshes the cache amongst servers for a media item
/// </summary>
@@ -76,6 +86,16 @@ namespace Umbraco.Web.Cache
dc.Refresh(new Guid(DistributedCache.MediaCacheRefresherId), mediaId);
}
/// <summary>
/// Removes the cache amongst servers for a media item
/// </summary>
/// <param name="dc"></param>
/// <param name="mediaId"></param>
public static void RemoveMediaCache(this DistributedCache dc, int mediaId)
{
dc.Remove(new Guid(DistributedCache.MediaCacheRefresherId), mediaId);
}
/// <summary>
/// Refreshes the cache amongst servers for a macro item
/// </summary>

View File

@@ -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));
}
}
}
}

View File

@@ -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)
{
}
}
}

View File

@@ -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)
{
}
}
}

View File

@@ -46,44 +46,7 @@ namespace Umbraco.Web
}
public const string PartialViewCacheKey = "Umbraco.Web.PartialViewCacheKey";
/// <summary>
/// Clears the library cache for media
/// </summary>
/// <param name="cacheHelper"></param>
/// <param name="mediaId"></param>
/// <param name="allServers">
/// 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.
/// </param>
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));
}
}
}
}
/// <summary>
/// Outputs and caches a partial view in MVC
/// </summary>

View File

@@ -249,7 +249,7 @@
<Compile Include="Cache\DistributedCacheExtensions.cs" />
<Compile Include="Cache\CacheRefresherEventHandler.cs" />
<Compile Include="Cache\MacroCacheRefresher.cs" />
<Compile Include="Cache\MediaLibraryRefreshers.cs" />
<Compile Include="Cache\MediaCacheRefresher.cs" />
<Compile Include="Cache\MemberCacheRefresher.cs" />
<Compile Include="Cache\PageCacheRefresher.cs" />
<Compile Include="Cache\TemplateCacheRefresher.cs" />

View File

@@ -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")]

View File

@@ -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
{
}

View File

@@ -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()) + "</p><p><a href='#' onclick='" + ClientTools.Scripts.CloseModalWindow() + "'>" + ui.Text("closeThisWindow") + "</a>";