// Copyright (c) Umbraco. // See LICENSE for more details. using Microsoft.Extensions.Logging; using Umbraco.Cms.Core.Events; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Core.Sync; using Umbraco.Cms.Infrastructure.Persistence; namespace Umbraco.Cms.Core.Cache { /// /// Ensures that distributed cache events are setup and the is initialized /// public sealed class DatabaseServerMessengerNotificationHandler : INotificationHandler, INotificationHandler { private readonly IServerMessenger _messenger; private readonly IRuntimeState _runtimeState; private readonly IDistributedCacheBinder _distributedCacheBinder; private readonly ILogger _logger; /// /// Initializes a new instance of the class. /// public DatabaseServerMessengerNotificationHandler( IServerMessenger serverMessenger, IRuntimeState runtimeState, IDistributedCacheBinder distributedCacheBinder, ILogger logger) { _distributedCacheBinder = distributedCacheBinder; _logger = logger; _messenger = serverMessenger; _runtimeState = runtimeState; } /// public void Handle(UmbracoApplicationStarting notification) { if (_runtimeState.Level == RuntimeLevel.Install) { _logger.LogWarning("Disabling distributed calls during install"); } else { _distributedCacheBinder.BindEvents(); // Sync on startup, this will run through the messenger's initialization sequence _messenger?.Sync(); } } /// /// Clear the batch on end request /// public void Handle(UmbracoRequestEnd notification) => _messenger?.SendMessages(); } }