Files
Umbraco-CMS/src/Umbraco.Web/Cache/MacroCacheRefresher.cs

108 lines
3.0 KiB
C#
Raw Normal View History

using System;
2013-03-12 03:00:42 +04:00
using Umbraco.Core;
using Umbraco.Core.Cache;
using umbraco;
using umbraco.cms.businesslogic.macro;
using umbraco.interfaces;
using System.Linq;
namespace Umbraco.Web.Cache
{
/// <summary>
/// A cache refresher to ensure macro cache is updated when members change
/// </summary>
/// <remarks>
/// This is not intended to be used directly in your code and it should be sealed but due to legacy code we cannot seal it.
/// </remarks>
public class MacroCacheRefresher : ICacheRefresher<Macro>, ICacheRefresher<macro>
{
internal static string[] GetAllMacroCacheKeys()
2013-03-12 03:00:42 +04:00
{
return new[]
{
CacheKeys.MacroCacheKey,
CacheKeys.MacroControlCacheKey,
CacheKeys.MacroHtmlCacheKey,
CacheKeys.MacroHtmlDateAddedCacheKey,
CacheKeys.MacroControlDateAddedCacheKey
};
}
internal static string[] GetCacheKeysForAlias(string alias)
{
return GetAllMacroCacheKeys().Select(x => x + alias).ToArray();
2013-03-12 03:00:42 +04:00
}
public string Name
{
get
{
return "Macro cache refresher";
}
}
public Guid UniqueIdentifier
{
get
{
return new Guid(DistributedCache.MacroCacheRefresherId);
}
}
public void RefreshAll()
{
ApplicationContext.Current.ApplicationCache.ClearCacheObjectTypes<MacroCacheContent>();
GetAllMacroCacheKeys().ForEach(
prefix =>
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(prefix));
}
public void Refresh(Guid id)
{
}
public void Refresh(int id)
{
2013-03-12 03:00:42 +04:00
if (id <= 0) return;
var m = new Macro(id);
2013-03-12 03:00:42 +04:00
Remove(m);
}
public void Remove(int id)
{
2013-03-12 03:00:42 +04:00
if (id <= 0) return;
var m = new Macro(id);
2013-03-12 03:00:42 +04:00
Remove(m);
}
public void Refresh(Macro instance)
2013-03-12 03:00:42 +04:00
{
Remove(instance);
}
public void Remove(Macro instance)
2013-03-12 03:00:42 +04:00
{
if (instance != null && instance.Id > 0)
2013-03-12 03:00:42 +04:00
{
GetCacheKeysForAlias(instance.Alias).ForEach(
2013-03-12 03:00:42 +04:00
alias =>
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(alias));
2013-03-12 03:00:42 +04:00
}
}
public void Refresh(macro instance)
{
Remove(instance);
}
public void Remove(macro instance)
{
if (instance == null || instance.Model == null) return;
var m = instance.Model;
GetCacheKeysForAlias(m.Alias).ForEach(
alias =>
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(alias));
}
}
}