Created
November 30, 2022 08:38
-
-
Save AndyButland/5fcc6fbcaa89f3f879ab16d644014d8e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Umbraco.Cms.Core.Events; | |
using Umbraco.Cms.Core.Models; | |
using Umbraco.Cms.Core.Notifications; | |
using Umbraco.Cms.Core.Services; | |
using Umbraco.Cms.Core.Sync; | |
namespace Umbraco.Deploy.OnPrem.TestSite.Business | |
{ | |
public class DictionaryCacheRefresherNotificationHandler : INotificationHandler<DictionaryCacheRefresherNotification> | |
{ | |
private readonly ILogger<DictionaryCacheRefresherNotificationHandler> _logger; | |
private readonly IServerRoleAccessor _serverRoleAccessor; | |
private readonly ILocalizationService _localizationService; | |
public DictionaryCacheRefresherNotificationHandler( | |
ILogger<DictionaryCacheRefresherNotificationHandler> logger, | |
IServerRoleAccessor serverRoleAccessor, | |
ILocalizationService localizationService) | |
{ | |
_logger = logger; | |
_serverRoleAccessor = serverRoleAccessor; | |
_localizationService = localizationService; | |
} | |
public void Handle(DictionaryCacheRefresherNotification notification) | |
{ | |
ServerRole serverRole = _serverRoleAccessor.CurrentServerRole; | |
if (!(serverRole == ServerRole.Single || serverRole == ServerRole.SchedulingPublisher)) | |
{ | |
return; | |
} | |
if (notification.MessageType != MessageType.RefreshById) | |
{ | |
return; | |
} | |
var dictionaryItemId = (int)notification.MessageObject; | |
IDictionaryItem? dictionaryItem = _localizationService.GetDictionaryItemById(dictionaryItemId); | |
if (dictionaryItem == null) | |
{ | |
_logger.LogWarning( | |
"DictionaryCacheRefresherNotification handled for type {MessageType} but dictionary item with Id {Id} could not be found.", | |
notification.MessageType, | |
dictionaryItemId); | |
return; | |
} | |
// Do something with the dictionary item. | |
// Here we'll just log some details. | |
_logger.LogInformation( | |
"DictionaryCacheRefresherNotification handled for type {MessageType} and id {Id}. " + | |
"Key: {Key}, Default Value: {DefaultValue}", | |
notification.MessageType, | |
dictionaryItemId, | |
dictionaryItem.ItemKey, | |
dictionaryItem.GetDefaultValue()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment