Files
Umbraco-CMS/src/Umbraco.Web/Cache/MacroCacheRefresher.cs
Shannon Deminick 2a7e493c74 Changes all instances of the legacy Cache class to be using ApplicationContext.Current.ApplicationCache.
Fixes: #U4-2039 - ensures macro xslt cache is invalidated in LB scenarios.
2013-04-03 23:39:51 +06:00

166 lines
5.1 KiB
C#

using System;
using System.Web.Script.Serialization;
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 : JsonCacheRefresherBase<MacroCacheRefresher>
{
#region Static helpers
internal static string[] GetAllMacroCacheKeys()
{
return new[]
{
CacheKeys.MacroCacheKey,
CacheKeys.MacroControlCacheKey,
CacheKeys.MacroHtmlCacheKey,
CacheKeys.MacroHtmlDateAddedCacheKey,
CacheKeys.MacroControlDateAddedCacheKey,
CacheKeys.MacroXsltCacheKey,
};
}
internal static string[] GetCacheKeysForAlias(string alias)
{
return GetAllMacroCacheKeys().Select(x => x + alias).ToArray();
}
/// <summary>
/// Converts the json to a JsonPayload object
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
private static JsonPayload[] DeserializeFromJsonPayload(string json)
{
var serializer = new JavaScriptSerializer();
var jsonObject = serializer.Deserialize<JsonPayload[]>(json);
return jsonObject;
}
/// <summary>
/// Creates the custom Json payload used to refresh cache amongst the servers
/// </summary>
/// <param name="macros"></param>
/// <returns></returns>
internal static string SerializeToJsonPayload(params Macro[] macros)
{
var serializer = new JavaScriptSerializer();
var items = macros.Select(FromMacro).ToArray();
var json = serializer.Serialize(items);
return json;
}
/// <summary>
/// Creates the custom Json payload used to refresh cache amongst the servers
/// </summary>
/// <param name="macros"></param>
/// <returns></returns>
internal static string SerializeToJsonPayload(params macro[] macros)
{
var serializer = new JavaScriptSerializer();
var items = macros.Select(FromMacro).ToArray();
var json = serializer.Serialize(items);
return json;
}
/// <summary>
/// Converts a macro to a jsonPayload object
/// </summary>
/// <param name="macro"></param>
/// <returns></returns>
private static JsonPayload FromMacro(Macro macro)
{
var payload = new JsonPayload
{
Alias = macro.Alias,
Id = macro.Id
};
return payload;
}
/// <summary>
/// Converts a macro to a jsonPayload object
/// </summary>
/// <param name="macro"></param>
/// <returns></returns>
private static JsonPayload FromMacro(macro macro)
{
var payload = new JsonPayload
{
Alias = macro.Alias,
Id = macro.Model.Id
};
return payload;
}
#endregion
#region Sub classes
private class JsonPayload
{
public string Alias { get; set; }
public int Id { get; set; }
}
#endregion
protected override MacroCacheRefresher Instance
{
get { return this; }
}
public override string Name
{
get
{
return "Macro cache refresher";
}
}
public override Guid UniqueIdentifier
{
get
{
return new Guid(DistributedCache.MacroCacheRefresherId);
}
}
public override void RefreshAll()
{
ApplicationContext.Current.ApplicationCache.ClearCacheObjectTypes<MacroCacheContent>();
GetAllMacroCacheKeys().ForEach(
prefix =>
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(prefix));
base.RefreshAll();
}
public override void Refresh(string jsonPayload)
{
var payloads = DeserializeFromJsonPayload(jsonPayload);
payloads.ForEach(payload =>
{
GetCacheKeysForAlias(payload.Alias).ForEach(
alias =>
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(alias));
});
base.Refresh(jsonPayload);
}
}
}