cleared when media was deleted. Created base classes for cache refreshers, we now have a new event - CacheUpdated which can now be used by code to execute on each individual server when any cache refresher is updated. Listening to events normally only fire on the individual server so if people are wanting to refresh their own cache there was previously no way to do that.
170 lines
5.2 KiB
C#
170 lines
5.2 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
|
|
};
|
|
}
|
|
|
|
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)
|
|
{
|
|
Remove(jsonPayload);
|
|
base.Refresh(jsonPayload);
|
|
}
|
|
|
|
public override void Remove(string jsonPayload)
|
|
{
|
|
var payloads = DeserializeFromJsonPayload(jsonPayload);
|
|
|
|
payloads.ForEach(payload =>
|
|
{
|
|
GetCacheKeysForAlias(payload.Alias).ForEach(
|
|
alias =>
|
|
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(alias));
|
|
});
|
|
|
|
base.Remove(jsonPayload);
|
|
}
|
|
}
|
|
} |