Fixes DataTypeCacheRefresher to be a Json cache refresher since it needs to refresh both by GUID and by Int Ids
This commit is contained in:
@@ -203,22 +203,22 @@ namespace Umbraco.Web.Cache
|
||||
#region DataType event handlers
|
||||
static void DataTypeServiceSaved(IDataTypeService sender, Core.Events.SaveEventArgs<IDataTypeDefinition> e)
|
||||
{
|
||||
e.SavedEntities.ForEach(x => DistributedCache.Instance.RefreshDataTypeCache(x.Id));
|
||||
e.SavedEntities.ForEach(x => DistributedCache.Instance.RefreshDataTypeCache(x));
|
||||
}
|
||||
|
||||
static void DataTypeServiceDeleted(IDataTypeService sender, Core.Events.DeleteEventArgs<IDataTypeDefinition> e)
|
||||
{
|
||||
e.DeletedEntities.ForEach(x => DistributedCache.Instance.RemoveDataTypeCache(x.Id));
|
||||
e.DeletedEntities.ForEach(x => DistributedCache.Instance.RemoveDataTypeCache(x));
|
||||
}
|
||||
|
||||
static void DataTypeDefinitionSaving(global::umbraco.cms.businesslogic.datatype.DataTypeDefinition sender, System.EventArgs e)
|
||||
{
|
||||
DistributedCache.Instance.RefreshDataTypeCache(sender.Id);
|
||||
DistributedCache.Instance.RefreshDataTypeCache(sender);
|
||||
}
|
||||
|
||||
static void DataTypeDefinitionDeleting(global::umbraco.cms.businesslogic.datatype.DataTypeDefinition sender, System.EventArgs e)
|
||||
{
|
||||
DistributedCache.Instance.RemoveDataTypeCache(sender.Id);
|
||||
DistributedCache.Instance.RemoveDataTypeCache(sender);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -1,15 +1,100 @@
|
||||
using System;
|
||||
using System.Web.Script.Serialization;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Cache;
|
||||
using System.Linq;
|
||||
using Umbraco.Core.Models;
|
||||
|
||||
namespace Umbraco.Web.Cache
|
||||
{
|
||||
/// <summary>
|
||||
/// A cache refresher to ensure member cache is updated when members change
|
||||
/// </summary>
|
||||
public sealed class DataTypeCacheRefresher : CacheRefresherBase<DataTypeCacheRefresher>
|
||||
public sealed class DataTypeCacheRefresher : JsonCacheRefresherBase<DataTypeCacheRefresher>
|
||||
{
|
||||
|
||||
#region Static helpers
|
||||
|
||||
/// <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="dataTypes"></param>
|
||||
/// <returns></returns>
|
||||
internal static string SerializeToJsonPayload(params global::umbraco.cms.businesslogic.datatype.DataTypeDefinition[] dataTypes)
|
||||
{
|
||||
var serializer = new JavaScriptSerializer();
|
||||
var items = dataTypes.Select(FromDataTypeDefinition).ToArray();
|
||||
var json = serializer.Serialize(items);
|
||||
return json;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the custom Json payload used to refresh cache amongst the servers
|
||||
/// </summary>
|
||||
/// <param name="dataTypes"></param>
|
||||
/// <returns></returns>
|
||||
internal static string SerializeToJsonPayload(params IDataTypeDefinition[] dataTypes)
|
||||
{
|
||||
var serializer = new JavaScriptSerializer();
|
||||
var items = dataTypes.Select(FromDataTypeDefinition).ToArray();
|
||||
var json = serializer.Serialize(items);
|
||||
return json;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a macro to a jsonPayload object
|
||||
/// </summary>
|
||||
/// <param name="dataType"></param>
|
||||
/// <returns></returns>
|
||||
private static JsonPayload FromDataTypeDefinition(global::umbraco.cms.businesslogic.datatype.DataTypeDefinition dataType)
|
||||
{
|
||||
var payload = new JsonPayload
|
||||
{
|
||||
UniqueId = dataType.UniqueId,
|
||||
Id = dataType.Id
|
||||
};
|
||||
return payload;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a macro to a jsonPayload object
|
||||
/// </summary>
|
||||
/// <param name="dataType"></param>
|
||||
/// <returns></returns>
|
||||
private static JsonPayload FromDataTypeDefinition(IDataTypeDefinition dataType)
|
||||
{
|
||||
var payload = new JsonPayload
|
||||
{
|
||||
UniqueId = dataType.Key,
|
||||
Id = dataType.Id
|
||||
};
|
||||
return payload;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sub classes
|
||||
|
||||
private class JsonPayload
|
||||
{
|
||||
public Guid UniqueId { get; set; }
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
protected override DataTypeCacheRefresher Instance
|
||||
{
|
||||
get { return this; }
|
||||
@@ -25,22 +110,20 @@ namespace Umbraco.Web.Cache
|
||||
get { return "Clears data type cache"; }
|
||||
}
|
||||
|
||||
public override void Refresh(int id)
|
||||
public override void Refresh(string jsonPayload)
|
||||
{
|
||||
ClearCache(id);
|
||||
base.Refresh(id);
|
||||
}
|
||||
var payloads = DeserializeFromJsonPayload(jsonPayload);
|
||||
|
||||
public override void Remove(int id)
|
||||
{
|
||||
ClearCache(id);
|
||||
base.Remove(id);
|
||||
}
|
||||
payloads.ForEach(payload =>
|
||||
{
|
||||
//clear both the Id and Unique Id cache since we cache both in the legacy classes :(
|
||||
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(
|
||||
string.Format("{0}{1}", CacheKeys.DataTypeCacheKey, payload.Id));
|
||||
ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(
|
||||
string.Format("{0}{1}", CacheKeys.DataTypeCacheKey, payload.UniqueId));
|
||||
});
|
||||
|
||||
private void ClearCache(int id)
|
||||
{
|
||||
ApplicationContext.Current.ApplicationCache.
|
||||
ClearCacheByKeySearch(string.Format("{0}{1}", CacheKeys.DataTypeCacheKey, id));
|
||||
base.Refresh(jsonPayload);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,23 +110,59 @@ namespace Umbraco.Web.Cache
|
||||
|
||||
#region Data type cache
|
||||
/// <summary>
|
||||
/// Refreshes the cache amongst servers for a template
|
||||
/// Refreshes the cache amongst servers for a data type
|
||||
/// </summary>
|
||||
/// <param name="dc"></param>
|
||||
/// <param name="dataTypeId"></param>
|
||||
public static void RefreshDataTypeCache(this DistributedCache dc, int dataTypeId)
|
||||
/// <param name="dataType"></param>
|
||||
public static void RefreshDataTypeCache(this DistributedCache dc, global::umbraco.cms.businesslogic.datatype.DataTypeDefinition dataType)
|
||||
{
|
||||
dc.Refresh(new Guid(DistributedCache.DataTypeCacheRefresherId), dataTypeId);
|
||||
if (dataType != null)
|
||||
{
|
||||
dc.RefreshByJson(new Guid(DistributedCache.DataTypeCacheRefresherId),
|
||||
DataTypeCacheRefresher.SerializeToJsonPayload(dataType));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the cache amongst servers for a template
|
||||
/// Removes the cache amongst servers for a data type
|
||||
/// </summary>
|
||||
/// <param name="dc"></param>
|
||||
/// <param name="dataTypeId"></param>
|
||||
public static void RemoveDataTypeCache(this DistributedCache dc, int dataTypeId)
|
||||
/// <param name="dataType"></param>
|
||||
public static void RemoveDataTypeCache(this DistributedCache dc, global::umbraco.cms.businesslogic.datatype.DataTypeDefinition dataType)
|
||||
{
|
||||
dc.Remove(new Guid(DistributedCache.DataTypeCacheRefresherId), dataTypeId);
|
||||
if (dataType != null)
|
||||
{
|
||||
dc.RefreshByJson(new Guid(DistributedCache.DataTypeCacheRefresherId),
|
||||
DataTypeCacheRefresher.SerializeToJsonPayload(dataType));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refreshes the cache amongst servers for a data type
|
||||
/// </summary>
|
||||
/// <param name="dc"></param>
|
||||
/// <param name="dataType"></param>
|
||||
public static void RefreshDataTypeCache(this DistributedCache dc, IDataTypeDefinition dataType)
|
||||
{
|
||||
if (dataType != null)
|
||||
{
|
||||
dc.RefreshByJson(new Guid(DistributedCache.DataTypeCacheRefresherId),
|
||||
DataTypeCacheRefresher.SerializeToJsonPayload(dataType));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the cache amongst servers for a data type
|
||||
/// </summary>
|
||||
/// <param name="dc"></param>
|
||||
/// <param name="dataType"></param>
|
||||
public static void RemoveDataTypeCache(this DistributedCache dc, IDataTypeDefinition dataType)
|
||||
{
|
||||
if (dataType != null)
|
||||
{
|
||||
dc.RefreshByJson(new Guid(DistributedCache.DataTypeCacheRefresherId),
|
||||
DataTypeCacheRefresher.SerializeToJsonPayload(dataType));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user