Streamlines how macro cache is handled

This commit is contained in:
Shannon Deminick
2013-03-12 03:00:42 +04:00
parent 164f22aa50
commit ec46d65bb4
18 changed files with 167 additions and 141 deletions

View File

@@ -87,7 +87,7 @@ namespace Umbraco.Web.Cache
/// <param name="e"></param>
static void MacroAfterDelete(Macro sender, DeleteEventArgs e)
{
DistributedCache.Instance.RemoveMacroCache(sender.Id);
DistributedCache.Instance.RemoveMacroCache(sender);
}
/// <summary>
@@ -97,7 +97,7 @@ namespace Umbraco.Web.Cache
/// <param name="e"></param>
static void MacroAfterSave(Macro sender, SaveEventArgs e)
{
DistributedCache.Instance.RefreshMacroCache(sender.Id);
DistributedCache.Instance.RefreshMacroCache(sender);
}
static void MediaServiceTrashing(IMediaService sender, Core.Events.MoveEventArgs<Core.Models.IMedia> e)

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Models;
using umbraco;
namespace Umbraco.Web.Cache
{
@@ -159,6 +160,19 @@ namespace Umbraco.Web.Cache
dc.Refresh(new Guid(DistributedCache.MacroCacheRefresherId), macroId);
}
/// <summary>
/// Refreshes the cache amongst servers for a macro item
/// </summary>
/// <param name="dc"></param>
/// <param name="macro"></param>
public static void RefreshMacroCache(this DistributedCache dc, global::umbraco.cms.businesslogic.macro.Macro macro)
{
if (macro != null)
{
dc.Refresh(new Guid(DistributedCache.MacroCacheRefresherId), macro1 => macro1.Id, macro);
}
}
/// <summary>
/// Removes the cache amongst servers for a macro item
/// </summary>
@@ -168,5 +182,31 @@ namespace Umbraco.Web.Cache
{
dc.Remove(new Guid(DistributedCache.MacroCacheRefresherId), macroId);
}
/// <summary>
/// Removes the cache amongst servers for a macro item
/// </summary>
/// <param name="dc"></param>
/// <param name="macro"></param>
public static void RemoveMacroCache(this DistributedCache dc, macro macro)
{
if (macro != null && macro.Model != null)
{
dc.Remove(new Guid(DistributedCache.MacroCacheRefresherId), macro1 => macro1.Model.Id, macro);
}
}
/// <summary>
/// Removes the cache amongst servers for a macro item
/// </summary>
/// <param name="dc"></param>
/// <param name="macro"></param>
public static void RemoveMacroCache(this DistributedCache dc, global::umbraco.cms.businesslogic.macro.Macro macro)
{
if (macro != null)
{
dc.Remove(new Guid(DistributedCache.MacroCacheRefresherId), macro1 => macro1.Id, macro);
}
}
}
}

View File

@@ -1,4 +1,6 @@
using System;
using Umbraco.Core;
using Umbraco.Core.Cache;
using umbraco;
using umbraco.interfaces;
@@ -7,8 +9,13 @@ namespace Umbraco.Web.Cache
/// <summary>
/// Used to invalidate/refresh the cache for macros
/// </summary>
public class MacroCacheRefresher : ICacheRefresher
public class MacroCacheRefresher : ICacheRefresher<macro>
{
internal static string[] GetCacheKeys(string alias)
{
return new[] { CacheKeys.MacroRuntimeCacheKey + alias, CacheKeys.UmbracoMacroCacheKey + alias };
}
public string Name
{
get
@@ -35,13 +42,32 @@ namespace Umbraco.Web.Cache
void ICacheRefresher.Refresh(int id)
{
macro.GetMacro(id).removeFromCache();
if (id <= 0) return;
var m = new macro(id);
Remove(m);
}
void ICacheRefresher.Remove(int id)
{
macro.GetMacro(id).removeFromCache();
if (id <= 0) return;
var m = new macro(id);
Remove(m);
}
public void Refresh(macro instance)
{
Remove(instance);
}
public void Remove(macro instance)
{
if (instance != null && instance.Model != null && instance.Model.Id > 0)
{
GetCacheKeys(instance.Model.Alias).ForEach(
alias =>
ApplicationContext.Current.ApplicationCache.ClearCacheItem(alias));
}
}
}
}

View File

@@ -1,13 +1,12 @@
using System;
using Umbraco.Core;
using Umbraco.Core.Cache;
using umbraco.interfaces;
namespace Umbraco.Web.Cache
{
public class MediaCacheRefresher : ICacheRefresher
{
const string getmediaCacheKey = "GetMedia";
public Guid UniqueIdentifier
{
get { return new Guid(DistributedCache.MediaCacheRefresherId); }
@@ -44,12 +43,12 @@ namespace Umbraco.Web.Cache
foreach (var idPart in m.Path.Split(','))
{
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(
string.Format("UL_{0}_{1}_True", getmediaCacheKey, idPart));
string.Format("UL_{0}_{1}_True", CacheKeys.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));
string.Format("UL_{0}_{1}", CacheKeys.GetMediaCacheKey, id));
}
}

View File

@@ -1,5 +1,6 @@
using System;
using Umbraco.Core;
using Umbraco.Core.Cache;
using umbraco.interfaces;
namespace Umbraco.Web.Cache
@@ -38,10 +39,8 @@ namespace Umbraco.Web.Cache
private void ClearCache(int id)
{
const string getmemberCacheKey = "GetMember";
ApplicationContext.Current.ApplicationCache.
ClearCacheByKeySearch(string.Format("UL_{0}_{1}", getmemberCacheKey, id));
ClearCacheByKeySearch(string.Format("UL_{0}_{1}", CacheKeys.GetMemberCacheKey, id));
}
}

View File

@@ -1,4 +1,5 @@
using System;
using Umbraco.Core.Cache;
using Umbraco.Core.Models;
using Umbraco.Core.Sync;
using umbraco;

View File

@@ -1,5 +1,6 @@
using System;
using Umbraco.Core;
using Umbraco.Core.Cache;
using umbraco;
using umbraco.interfaces;
@@ -7,8 +8,7 @@ namespace Umbraco.Web.Cache
{
public class TemplateCacheRefresher : ICacheRefresher
{
private const string TemplateCacheKey = "template";
public string Name
{
get
@@ -46,7 +46,7 @@ namespace Umbraco.Web.Cache
private void RemoveFromCache(int id)
{
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(
string.Format("{0}{1}", TemplateCacheKey, id));
string.Format("{0}{1}", CacheKeys.TemplateCacheKey, id));
}
}

View File

@@ -1,5 +1,6 @@
using System;
using Umbraco.Core;
using Umbraco.Core.Cache;
using umbraco.interfaces;
namespace Umbraco.Web.Cache
@@ -20,7 +21,7 @@ namespace Umbraco.Web.Cache
public void RefreshAll()
{
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch("UmbracoUser");
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(CacheKeys.UserCacheKey);
}
public void Refresh(int id)
@@ -30,7 +31,7 @@ namespace Umbraco.Web.Cache
public void Remove(int id)
{
ApplicationContext.Current.ApplicationCache.ClearCacheItem(string.Format("UmbracoUser{0}", id.ToString()));
ApplicationContext.Current.ApplicationCache.ClearCacheItem(string.Format("{0}{1}", CacheKeys.UserCacheKey, id.ToString()));
}
public void Refresh(Guid id)