2021-03-15 13:39:34 +11:00
|
|
|
using System;
|
2021-03-12 21:48:24 +01:00
|
|
|
using Umbraco.Cms.Core.Events;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.PublishedCache;
|
|
|
|
|
using Umbraco.Cms.Core.Serialization;
|
|
|
|
|
using Umbraco.Cms.Core.Services.Changes;
|
|
|
|
|
using static Umbraco.Cms.Core.Cache.LanguageCacheRefresher.JsonPayload;
|
2018-05-02 14:52:00 +10:00
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Cms.Core.Cache
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2021-03-12 21:48:24 +01:00
|
|
|
public sealed class LanguageCacheRefresher : PayloadCacheRefresherBase<LanguageCacheRefresherNotification, LanguageCacheRefresher.JsonPayload>
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2021-03-12 21:48:24 +01:00
|
|
|
public LanguageCacheRefresher(
|
|
|
|
|
AppCaches appCaches,
|
|
|
|
|
IJsonSerializer serializer,
|
|
|
|
|
IPublishedSnapshotService publishedSnapshotService,
|
2021-03-15 13:39:34 +11:00
|
|
|
IEventAggregator eventAggregator,
|
|
|
|
|
ICacheRefresherNotificationFactory factory)
|
|
|
|
|
: base(appCaches, serializer, eventAggregator, factory)
|
2018-05-02 14:52:00 +10:00
|
|
|
{
|
|
|
|
|
_publishedSnapshotService = publishedSnapshotService;
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Define
|
|
|
|
|
|
2018-05-02 14:52:00 +10:00
|
|
|
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
|
|
|
|
|
|
2019-10-14 15:21:00 +11:00
|
|
|
public override void Refresh(JsonPayload[] payloads)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2019-10-14 15:21:00 +11:00
|
|
|
if (payloads.Length == 0) return;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2019-10-14 15:21:00 +11:00
|
|
|
var clearDictionary = false;
|
|
|
|
|
var clearContent = false;
|
|
|
|
|
|
|
|
|
|
//clear all no matter what type of payload
|
2018-05-02 14:52:00 +10:00
|
|
|
ClearAllIsolatedCacheByEntityType<ILanguage>();
|
2019-10-14 15:21:00 +11:00
|
|
|
|
|
|
|
|
foreach (var payload in payloads)
|
2019-10-29 09:16:52 +00:00
|
|
|
{
|
2019-10-14 15:21:00 +11:00
|
|
|
switch (payload.ChangeType)
|
|
|
|
|
{
|
2019-10-14 22:02:42 +11:00
|
|
|
case LanguageChangeType.Update:
|
2019-10-14 15:21:00 +11:00
|
|
|
clearDictionary = true;
|
|
|
|
|
break;
|
2019-10-14 22:02:42 +11:00
|
|
|
case LanguageChangeType.Remove:
|
|
|
|
|
case LanguageChangeType.ChangeCulture:
|
2019-10-14 15:21:00 +11:00
|
|
|
clearDictionary = true;
|
|
|
|
|
clearContent = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (clearDictionary)
|
|
|
|
|
{
|
|
|
|
|
ClearAllIsolatedCacheByEntityType<IDictionaryItem>();
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-14 22:02:42 +11:00
|
|
|
//if this flag is set, we will tell the published snapshot service to refresh ALL content and evict ALL IContent items
|
2019-10-14 15:21:00 +11:00
|
|
|
if (clearContent)
|
|
|
|
|
{
|
2019-10-14 22:09:47 +11:00
|
|
|
//clear all domain caches
|
|
|
|
|
RefreshDomains();
|
2019-10-14 22:02:42 +11:00
|
|
|
ContentCacheRefresher.RefreshContentTypes(AppCaches); // we need to evict all IContent items
|
|
|
|
|
//now refresh all nucache
|
2019-10-14 15:21:00 +11:00
|
|
|
var clearContentPayload = new[] { new ContentCacheRefresher.JsonPayload(0, null, TreeChangeTypes.RefreshAll) };
|
2019-10-29 09:16:52 +00:00
|
|
|
ContentCacheRefresher.NotifyPublishedSnapshotService(_publishedSnapshotService, AppCaches, clearContentPayload);
|
2019-10-14 15:21:00 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// then trigger event
|
|
|
|
|
base.Refresh(payloads);
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-14 15:21:00 +11: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();
|
|
|
|
|
|
2018-05-02 14:52:00 +10:00
|
|
|
#endregion
|
|
|
|
|
|
2019-10-14 22:02:42 +11:00
|
|
|
/// <summary>
|
|
|
|
|
/// Clears all domain caches
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void RefreshDomains()
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2019-10-14 22:02:42 +11:00
|
|
|
ClearAllIsolatedCacheByEntityType<IDomain>();
|
2018-05-02 14:52:00 +10:00
|
|
|
|
2019-10-14 22:02:42 +11:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
2019-10-14 15:21:00 +11:00
|
|
|
#region Json
|
2018-05-02 14:52:00 +10:00
|
|
|
|
2019-10-14 15:21:00 +11:00
|
|
|
public class JsonPayload
|
2018-05-02 14:52:00 +10:00
|
|
|
{
|
2019-10-14 15:21:00 +11:00
|
|
|
public JsonPayload(int id, string isoCode, LanguageChangeType changeType)
|
|
|
|
|
{
|
|
|
|
|
Id = id;
|
|
|
|
|
IsoCode = isoCode;
|
|
|
|
|
ChangeType = changeType;
|
|
|
|
|
}
|
2018-05-02 14:52:00 +10:00
|
|
|
|
2019-10-14 15:21:00 +11:00
|
|
|
public int Id { get; }
|
|
|
|
|
public string IsoCode { get; }
|
|
|
|
|
public LanguageChangeType ChangeType { get; }
|
|
|
|
|
|
|
|
|
|
public enum LanguageChangeType
|
2018-05-02 14:52:00 +10:00
|
|
|
{
|
2019-10-14 15:21:00 +11:00
|
|
|
/// <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-05-02 14:52:00 +10:00
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
2019-10-14 15:21:00 +11:00
|
|
|
|
|
|
|
|
#endregion
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
}
|