2021-06-24 13:35:57 -06:00
using System ;
2020-12-09 22:43:49 +11:00
using System.Collections.Generic ;
using Microsoft.Extensions.Logging ;
2021-06-24 13:35:57 -06:00
using Microsoft.Extensions.Options ;
2021-02-23 12:24:51 +01:00
using Umbraco.Cms.Core ;
2021-06-24 13:35:57 -06:00
using Umbraco.Cms.Core.Configuration.Models ;
2021-02-09 10:22:42 +01:00
using Umbraco.Cms.Core.Events ;
2021-06-24 13:35:57 -06:00
using Umbraco.Cms.Core.Logging ;
2021-02-09 10:22:42 +01:00
using Umbraco.Cms.Core.Models ;
using Umbraco.Cms.Core.Scoping ;
2021-06-24 13:35:57 -06:00
using Umbraco.Cms.Core.Services ;
2021-02-23 12:24:51 +01:00
using Umbraco.Cms.Core.Services.Implement ;
2020-12-09 22:43:49 +11:00
2021-02-10 10:46:52 +01:00
namespace Umbraco.Cms.Infrastructure.PublishedCache.Persistence
2020-12-09 22:43:49 +11:00
{
public class NuCacheContentService : RepositoryService , INuCacheContentService
{
private readonly INuCacheContentRepository _repository ;
2021-06-24 13:35:57 -06:00
private readonly IKeyValueService _keyValueService ;
private readonly IProfilingLogger _profilingLogger ;
private readonly IOptions < NuCacheSettings > _nucacheSettings ;
private readonly ILogger < NuCacheContentService > _logger ;
private const string NuCacheSerializerKey = "Umbraco.Web.PublishedCache.NuCache.Serializer" ;
2020-12-09 22:43:49 +11:00
2021-06-24 09:43:57 -06:00
public NuCacheContentService (
INuCacheContentRepository repository ,
2021-06-24 13:35:57 -06:00
IKeyValueService keyValueService ,
2021-06-24 09:43:57 -06:00
IScopeProvider provider ,
ILoggerFactory loggerFactory ,
2021-06-24 13:35:57 -06:00
IProfilingLogger profilingLogger ,
IEventMessagesFactory eventMessagesFactory ,
IOptions < NuCacheSettings > nucacheSettings )
2020-12-09 22:43:49 +11:00
: base ( provider , loggerFactory , eventMessagesFactory )
{
_repository = repository ;
2021-06-24 13:35:57 -06:00
_keyValueService = keyValueService ;
_profilingLogger = profilingLogger ;
_nucacheSettings = nucacheSettings ;
_logger = loggerFactory . CreateLogger < NuCacheContentService > ( ) ;
}
public void RebuildDatabaseCacheIfSerializerChanged ( )
{
NuCacheSerializerType serializer = _nucacheSettings . Value . NuCacheSerializerType ;
var currentSerializerValue = _keyValueService . GetValue ( NuCacheSerializerKey ) ;
if ( ! Enum . TryParse ( currentSerializerValue , out NuCacheSerializerType currentSerializer )
| | serializer ! = currentSerializer )
{
_logger . LogWarning ( "Database NuCache was serialized using {CurrentSerializer}. Currently configured NuCache serializer {Serializer}. Rebuilding Nucache" , currentSerializer , serializer ) ;
using ( _profilingLogger . TraceDuration < NuCacheContentService > ( $"Rebuilding NuCache database with {serializer} serializer" ) )
{
Rebuild ( ) ;
_keyValueService . SetValue ( NuCacheSerializerKey , serializer . ToString ( ) ) ;
}
}
2020-12-09 22:43:49 +11:00
}
/// <inheritdoc/>
public IEnumerable < ContentNodeKit > GetAllContentSources ( )
= > _repository . GetAllContentSources ( ) ;
/// <inheritdoc/>
public IEnumerable < ContentNodeKit > GetAllMediaSources ( )
= > _repository . GetAllMediaSources ( ) ;
/// <inheritdoc/>
public IEnumerable < ContentNodeKit > GetBranchContentSources ( int id )
= > _repository . GetBranchContentSources ( id ) ;
/// <inheritdoc/>
public IEnumerable < ContentNodeKit > GetBranchMediaSources ( int id )
= > _repository . GetBranchMediaSources ( id ) ;
/// <inheritdoc/>
public ContentNodeKit GetContentSource ( int id )
= > _repository . GetContentSource ( id ) ;
/// <inheritdoc/>
public ContentNodeKit GetMediaSource ( int id )
= > _repository . GetMediaSource ( id ) ;
/// <inheritdoc/>
public IEnumerable < ContentNodeKit > GetTypeContentSources ( IEnumerable < int > ids )
= > _repository . GetTypeContentSources ( ids ) ;
/// <inheritdoc/>
public IEnumerable < ContentNodeKit > GetTypeMediaSources ( IEnumerable < int > ids )
= > _repository . GetTypeContentSources ( ids ) ;
/// <inheritdoc/>
public void DeleteContentItem ( IContentBase item )
= > _repository . DeleteContentItem ( item ) ;
2021-04-20 12:17:11 +02:00
public void DeleteContentItems ( IEnumerable < IContentBase > items )
{
foreach ( IContentBase item in items )
{
_repository . DeleteContentItem ( item ) ;
}
}
2020-12-09 22:43:49 +11:00
/// <inheritdoc/>
public void RefreshContent ( IContent content )
= > _repository . RefreshContent ( content ) ;
/// <inheritdoc/>
2021-06-24 09:43:57 -06:00
public void RefreshMedia ( IMedia media )
= > _repository . RefreshMedia ( media ) ;
/// <inheritdoc/>
public void RefreshMember ( IMember member )
= > _repository . RefreshMember ( member ) ;
2020-12-09 22:43:49 +11:00
/// <inheritdoc/>
public void Rebuild (
IReadOnlyCollection < int > contentTypeIds = null ,
IReadOnlyCollection < int > mediaTypeIds = null ,
IReadOnlyCollection < int > memberTypeIds = null )
{
using ( IScope scope = ScopeProvider . CreateScope ( repositoryCacheMode : RepositoryCacheMode . Scoped ) )
{
2021-06-23 11:31:08 +02:00
scope . ReadLock ( Constants . Locks . ContentTree ) ;
scope . ReadLock ( Constants . Locks . MediaTree ) ;
scope . ReadLock ( Constants . Locks . MemberTree ) ;
2020-12-09 22:43:49 +11:00
2021-06-24 09:43:57 -06:00
_repository . Rebuild ( contentTypeIds , mediaTypeIds , memberTypeIds ) ;
2021-06-24 13:35:57 -06:00
// Save a key/value of the serialized type. This is used during startup to see
// if the serialized type changed and if so it will rebuild with the correct type.
_keyValueService . SetValue ( NuCacheSerializerKey , _nucacheSettings . Value . NuCacheSerializerType . ToString ( ) ) ;
2020-12-09 22:43:49 +11:00
scope . Complete ( ) ;
}
}
/// <inheritdoc/>
public bool VerifyContentDbCache ( )
2020-12-21 17:41:12 +11:00
{
using IScope scope = ScopeProvider . CreateScope ( autoComplete : true ) ;
scope . ReadLock ( Constants . Locks . ContentTree ) ;
return _repository . VerifyContentDbCache ( ) ;
}
2020-12-09 22:43:49 +11:00
/// <inheritdoc/>
public bool VerifyMediaDbCache ( )
2020-12-21 17:41:12 +11:00
{
using IScope scope = ScopeProvider . CreateScope ( autoComplete : true ) ;
scope . ReadLock ( Constants . Locks . MediaTree ) ;
return _repository . VerifyMediaDbCache ( ) ;
}
2020-12-09 22:43:49 +11:00
/// <inheritdoc/>
public bool VerifyMemberDbCache ( )
2020-12-21 17:41:12 +11:00
{
using IScope scope = ScopeProvider . CreateScope ( autoComplete : true ) ;
scope . ReadLock ( Constants . Locks . MemberTree ) ;
return _repository . VerifyMemberDbCache ( ) ;
}
2020-12-09 22:43:49 +11:00
}
}