2018-06-29 19:52:40 +02:00
using System ;
2018-05-02 14:52:00 +10:00
using System.Linq ;
2018-06-29 19:52:40 +02:00
using Umbraco.Core.Cache ;
using Umbraco.Core.Models ;
2018-05-02 14:52:00 +10:00
using Umbraco.Core.Services ;
using Umbraco.Core.Services.Changes ;
using Umbraco.Web.PublishedCache ;
2018-06-29 19:52:40 +02:00
namespace Umbraco.Web.Cache
{
public sealed class LanguageCacheRefresher : CacheRefresherBase < LanguageCacheRefresher >
{
2019-01-17 08:34:29 +01:00
public LanguageCacheRefresher ( AppCaches appCaches , IPublishedSnapshotService publishedSnapshotService , IDomainService domainService )
: base ( appCaches )
2018-05-02 14:52:00 +10:00
{
_publishedSnapshotService = publishedSnapshotService ;
_domainService = domainService ;
2018-06-29 19:52:40 +02:00
}
#region Define
protected override LanguageCacheRefresher This = > this ;
2018-05-02 14:52:00 +10:00
public static readonly Guid UniqueId = Guid . Parse ( "3E0F95D8-0BE5-44B8-8394-2B8750B62654" ) ;
private readonly IPublishedSnapshotService _publishedSnapshotService ;
private readonly IDomainService _domainService ;
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 ( int id )
{
2018-05-02 14:52:00 +10:00
ClearAllIsolatedCacheByEntityType < ILanguage > ( ) ;
2018-06-29 19:52:40 +02:00
RefreshDomains ( id ) ;
base . Refresh ( id ) ;
}
public override void Remove ( int id )
{
ClearAllIsolatedCacheByEntityType < ILanguage > ( ) ;
//if a language is removed, then all dictionary cache needs to be removed
2018-05-02 14:52:00 +10:00
ClearAllIsolatedCacheByEntityType < IDictionaryItem > ( ) ;
RefreshDomains ( id ) ;
2018-06-29 19:52:40 +02:00
base . Remove ( id ) ;
}
2018-05-02 14:52:00 +10:00
#endregion
private void RefreshDomains ( int langId )
{
var assignedDomains = _domainService . GetAll ( true ) . Where ( x = > x . LanguageId . HasValue & & x . LanguageId . Value = = langId ) . ToList ( ) ;
if ( assignedDomains . Count > 0 )
{
2019-01-26 10:52:19 -05:00
// TODO: this is duplicating the logic in DomainCacheRefresher BUT we cannot inject that into this because it it not registered explicitly in the container,
2018-05-02 14:52:00 +10:00
// and we cannot inject the CacheRefresherCollection since that would be a circular reference, so what is the best way to call directly in to the
// DomainCacheRefresher?
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...
// notify
_publishedSnapshotService . Notify ( assignedDomains . Select ( x = > new DomainCacheRefresher . JsonPayload ( x . Id , DomainChangeTypes . Remove ) ) . ToArray ( ) ) ;
}
2018-06-29 19:52:40 +02:00
}
}
}