Files
Umbraco-CMS/src/Umbraco.Core/Cache/LanguageCacheRefresher.cs

151 lines
5.0 KiB
C#
Raw Normal View History

2018-06-29 19:52:40 +02:00
using System;
using System.Linq;
2018-06-29 19:52:40 +02:00
using Umbraco.Core.Cache;
using Umbraco.Core.Models;
using Umbraco.Core.Serialization;
using Umbraco.Core.Services;
using Umbraco.Core.Services.Changes;
using Umbraco.Web.PublishedCache;
using static Umbraco.Web.Cache.LanguageCacheRefresher.JsonPayload;
2018-06-29 19:52:40 +02:00
namespace Umbraco.Web.Cache
{
public sealed class LanguageCacheRefresher : PayloadCacheRefresherBase<LanguageCacheRefresher, LanguageCacheRefresher.JsonPayload>
2018-06-29 19:52:40 +02:00
{
public LanguageCacheRefresher(AppCaches appCaches, IJsonSerializer serializer, IPublishedSnapshotService publishedSnapshotService)
: base(appCaches, serializer)
{
_publishedSnapshotService = publishedSnapshotService;
2018-06-29 19:52:40 +02:00
}
#region Define
protected override LanguageCacheRefresher This => this;
public static readonly Guid UniqueId = Guid.Parse("3E0F95D8-0BE5-44B8-8394-2B8750B62654");
private readonly IPublishedSnapshotService _publishedSnapshotService;
2018-06-29 19:52:40 +02:00
public override Guid RefresherUniqueId => UniqueId;
public override string Name => "Language Cache Refresher";
#endregion
#region Refresher
public override void Refresh(JsonPayload[] payloads)
2018-06-29 19:52:40 +02:00
{
if (payloads.Length == 0) return;
2018-06-29 19:52:40 +02:00
var clearDictionary = false;
var clearContent = false;
//clear all no matter what type of payload
ClearAllIsolatedCacheByEntityType<ILanguage>();
foreach (var payload in payloads)
{
switch (payload.ChangeType)
{
case LanguageChangeType.Update:
clearDictionary = true;
break;
case LanguageChangeType.Remove:
case LanguageChangeType.ChangeCulture:
clearDictionary = true;
clearContent = true;
break;
}
}
if (clearDictionary)
{
ClearAllIsolatedCacheByEntityType<IDictionaryItem>();
}
//if this flag is set, we will tell the published snapshot service to refresh ALL content and evict ALL IContent items
if (clearContent)
{
2019-10-14 22:09:47 +11:00
//clear all domain caches
RefreshDomains();
ContentCacheRefresher.RefreshContentTypes(AppCaches); // we need to evict all IContent items
//now refresh all nucache
var clearContentPayload = new[] { new ContentCacheRefresher.JsonPayload(0, null, TreeChangeTypes.RefreshAll) };
ContentCacheRefresher.NotifyPublishedSnapshotService(_publishedSnapshotService, AppCaches, clearContentPayload);
}
// then trigger event
base.Refresh(payloads);
2018-06-29 19:52:40 +02:00
}
// these events should never trigger
// everything should be PAYLOAD/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
/// <summary>
/// Clears all domain caches
/// </summary>
private void RefreshDomains()
2018-06-29 19:52:40 +02:00
{
ClearAllIsolatedCacheByEntityType<IDomain>();
// note: must do what's above FIRST else the repositories still have the old cached
// content and when the PublishedCachesService is notified of changes it does not see
// the new content...
var payloads = new[] { new DomainCacheRefresher.JsonPayload(0, DomainChangeTypes.RefreshAll) };
_publishedSnapshotService.Notify(payloads);
2018-06-29 19:52:40 +02:00
}
#region Json
public class JsonPayload
{
public JsonPayload(int id, string isoCode, LanguageChangeType changeType)
{
Id = id;
IsoCode = isoCode;
ChangeType = changeType;
}
public int Id { get; }
public string IsoCode { get; }
public LanguageChangeType ChangeType { get; }
public enum LanguageChangeType
{
/// <summary>
/// A new languages has been added
/// </summary>
Add = 0,
/// <summary>
/// A language has been deleted
/// </summary>
Remove = 1,
/// <summary>
/// A language has been updated - but it's culture remains the same
/// </summary>
Update = 2,
/// <summary>
/// A language has been updated - it's culture has changed
/// </summary>
ChangeCulture = 3
}
2018-06-29 19:52:40 +02:00
}
#endregion
2018-06-29 19:52:40 +02:00
}
}