2018-06-29 19:52:40 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Cache;
|
|
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using Umbraco.Core.Persistence.Repositories;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
using Umbraco.Core.Persistence.Repositories.Implement;
|
|
|
|
|
|
using Umbraco.Core.Services;
|
|
|
|
|
|
using Umbraco.Core.Services.Changes;
|
|
|
|
|
|
using Umbraco.Web.Composing;
|
|
|
|
|
|
using Umbraco.Web.PublishedCache;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Cache
|
|
|
|
|
|
{
|
|
|
|
|
|
public sealed class MediaCacheRefresher : PayloadCacheRefresherBase<MediaCacheRefresher, MediaCacheRefresher.JsonPayload>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IPublishedSnapshotService _publishedSnapshotService;
|
|
|
|
|
|
private readonly IdkMap _idkMap;
|
|
|
|
|
|
|
2019-01-17 08:34:29 +01:00
|
|
|
|
public MediaCacheRefresher(AppCaches appCaches, IPublishedSnapshotService publishedSnapshotService, IdkMap idkMap)
|
|
|
|
|
|
: base(appCaches)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
_publishedSnapshotService = publishedSnapshotService;
|
|
|
|
|
|
_idkMap = idkMap;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region Define
|
|
|
|
|
|
|
|
|
|
|
|
protected override MediaCacheRefresher This => this;
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly Guid UniqueId = Guid.Parse("B29286DD-2D40-4DDB-B325-681226589FEC");
|
|
|
|
|
|
|
|
|
|
|
|
public override Guid RefresherUniqueId => UniqueId;
|
|
|
|
|
|
|
|
|
|
|
|
public override string Name => "Media Cache Refresher";
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Refresher
|
|
|
|
|
|
|
|
|
|
|
|
public override void Refresh(JsonPayload[] payloads)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (payloads == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
_publishedSnapshotService.Notify(payloads, out var anythingChanged);
|
|
|
|
|
|
|
|
|
|
|
|
if (anythingChanged)
|
|
|
|
|
|
{
|
2019-01-18 08:14:08 +01:00
|
|
|
|
Current.AppCaches.ClearPartialViewCache();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
var mediaCache = AppCaches.IsolatedCaches.Get<IMedia>();
|
2018-08-28 16:50:56 +10:00
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
foreach (var payload in payloads)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (payload.ChangeTypes == TreeChangeTypes.Remove)
|
|
|
|
|
|
_idkMap.ClearCache(payload.Id);
|
|
|
|
|
|
|
2018-08-28 16:50:56 +10:00
|
|
|
|
if (!mediaCache) continue;
|
|
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
// repository cache
|
|
|
|
|
|
// it *was* done for each pathId but really that does not make sense
|
|
|
|
|
|
// only need to do it for the current media
|
2019-01-17 11:01:23 +01:00
|
|
|
|
mediaCache.Result.Clear(RepositoryCacheKeys.GetKey<IMedia>(payload.Id));
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
// remove those that are in the branch
|
|
|
|
|
|
if (payload.ChangeTypes.HasTypesAny(TreeChangeTypes.RefreshBranch | TreeChangeTypes.Remove))
|
|
|
|
|
|
{
|
|
|
|
|
|
var pathid = "," + payload.Id + ",";
|
2019-01-17 11:01:23 +01:00
|
|
|
|
mediaCache.Result.ClearOfType<IMedia>((_, v) => v.Path.Contains(pathid));
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
base.Refresh(payloads);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// these events should never trigger
|
|
|
|
|
|
// everything should be JSON
|
|
|
|
|
|
|
|
|
|
|
|
public override void RefreshAll()
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Refresh(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Refresh(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Remove(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Json
|
|
|
|
|
|
|
|
|
|
|
|
public class JsonPayload
|
|
|
|
|
|
{
|
|
|
|
|
|
public JsonPayload(int id, TreeChangeTypes changeTypes)
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = id;
|
|
|
|
|
|
ChangeTypes = changeTypes;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int Id { get; }
|
|
|
|
|
|
|
|
|
|
|
|
public TreeChangeTypes ChangeTypes { get; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Indirect
|
|
|
|
|
|
|
2019-01-17 08:34:29 +01:00
|
|
|
|
public static void RefreshMediaTypes(AppCaches appCaches)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2019-01-17 11:01:23 +01:00
|
|
|
|
appCaches.IsolatedCaches.ClearCache<IMedia>();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|