using System; using System.Web.Script.Serialization; using umbraco; using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Models; using System.Linq; using Umbraco.Core.Persistence.Repositories; using Umbraco.Core.Sync; namespace Umbraco.Web.Cache { /// /// A cache refresher used for non-published content, this is primarily to notify Examine indexes to update and to refresh the RuntimeCacheRefresher /// public sealed class UnpublishedPageCacheRefresher : TypedCacheRefresherBase, IJsonCacheRefresher { protected override UnpublishedPageCacheRefresher Instance { get { return this; } } public override Guid UniqueIdentifier { get { return new Guid(DistributedCache.UnpublishedPageCacheRefresherId); } } public override string Name { get { return "Unpublished Page Refresher"; } } #region Static helpers /// /// Converts the json to a JsonPayload object /// /// /// internal static JsonPayload[] DeserializeFromJsonPayload(string json) { var serializer = new JavaScriptSerializer(); var jsonObject = serializer.Deserialize(json); return jsonObject; } internal static string SerializeToJsonPayloadForPermanentDeletion(params int[] contentIds) { var serializer = new JavaScriptSerializer(); var items = contentIds.Select(x => new JsonPayload { Id = x, Operation = OperationType.Deleted }).ToArray(); var json = serializer.Serialize(items); return json; } #endregion #region Sub classes internal enum OperationType { Deleted } internal class JsonPayload { public int Id { get; set; } public OperationType Operation { get; set; } } #endregion public override void RefreshAll() { ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheObjectTypes(); ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheObjectTypes(); base.RefreshAll(); } public override void Refresh(int id) { ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(RepositoryBase.GetCacheIdKey(id)); ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheObjectTypes(); content.Instance.UpdateSortOrder(id); base.Refresh(id); } public override void Remove(int id) { ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(RepositoryBase.GetCacheIdKey(id)); ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheObjectTypes(); base.Remove(id); } public override void Refresh(IContent instance) { ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(RepositoryBase.GetCacheIdKey(instance.Id)); ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheObjectTypes(); content.Instance.UpdateSortOrder(instance); base.Refresh(instance); } public override void Remove(IContent instance) { ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(RepositoryBase.GetCacheIdKey(instance.Id)); ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheObjectTypes(); base.Remove(instance); } /// /// Implement the IJsonCacheRefresher so that we can bulk delete the cache based on multiple IDs for when the recycle bin is emptied /// /// public void Refresh(string jsonPayload) { ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheObjectTypes(); foreach (var payload in DeserializeFromJsonPayload(jsonPayload)) { ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(RepositoryBase.GetCacheIdKey(payload.Id)); content.Instance.UpdateSortOrder(payload.Id); } OnCacheUpdated(Instance, new CacheRefresherEventArgs(jsonPayload, MessageType.RefreshByJson)); } } }