Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AndyButland/5fcc6fbcaa89f3f879ab16d644014d8e to your computer and use it in GitHub Desktop.
Save AndyButland/5fcc6fbcaa89f3f879ab16d644014d8e to your computer and use it in GitHub Desktop.
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