Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lars Danielsen 5 posts 75 karma points
    Jul 21, 2023 @ 08:41
    Lars Danielsen
    0

    CacheRefresher / Distributed events

    Hi all

    I am upgrading my solution from v7. I have to react to umbraco items being published/unpublished, across several servers. I have been using the v7 CacheRefresher technique.

    In umbraco 10, I have looked at the new CacheRefresher, but it seems to not work in the same way. It seems to me, that it invalidates caches created using the built-in cache types, which is not what I need to do. I need to perform more advanced logic, triggered by the publishing of items.

    My old code looks like this, can anybody show me how to recreate it in v10?:

        public class MyDistEventHandler : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                CacheRefresherBase<PageCacheRefresher>.CacheUpdated += MyDistEventHandler_CacheUpdated;
            }
            private void MyDistEventHandler_CacheUpdated(PageCacheRefresher sender, CacheRefresherEventArgs e)
            {
                var content = MyGetContent(e.MessageType, e.MessageObject);
                var react = false;
                switch (e.MessageType)
                {
                    case MessageType.RefreshByInstance:
                    case MessageType.RemoveByInstance:
                    case MessageType.RefreshById:
                    case MessageType.RemoveById:
                    case MessageType.RefreshAll:
                        react = true;
                        break;
                    case MessageType.RefreshByJson:
                    case MessageType.RefreshByPayload:
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
    
                if (react)
                {
                    MyDoCleverStuff(content);
                }
            }
        }
    
        public static IContent MyGetContent(MessageType messageType, object messageObject)
        {
            switch (messageType)
            {
                case MessageType.RefreshByInstance:
                case MessageType.RemoveByInstance:
                    return (Content)messageObject;
                case MessageType.RefreshById:
                case MessageType.RemoveById:
                    var cs = UmbracoContext.Current.Application.Services.ContentService;
                    return cs.GetById((int)messageObject);
                case MessageType.RefreshByJson:
                case MessageType.RefreshByPayload:
                case MessageType.RefreshAll:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
    
            return null;
        }
    

    I appreciate any help and suggestions, but please consider, that I try to avoid reimplementing existing logic, even though there might be better ways of doing things today. :)

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    Jul 21, 2023 @ 10:45
    Paul Seal
    0

    Hi, If you are looking to use the cache refresher events there is documentation here:

    https://docs.umbraco.com/umbraco-cms/reference/notifications/cacherefresher-notifications

    If you want to know how to hook into content being published then you can do that like this:

    1 - Create a notification handler

    using Umbraco.Cms.Core.Events;
    using Umbraco.Cms.Core.Notifications;
    
    namespace My.Site.ExamineIndexes;
    
    public class MyPublishContentNotificationHandler : INotificationHandler<ContentPublishedNotification>
    {
        public void Handle(ContentPublishedNotification notification)
        {
            foreach (var item in notification.PublishedEntities)
            {
                //custom logic in here
            }
        }
    }
    

    2 - Create a composer to add the notification hander to the publish notification

    using My.Site.ExamineIndexes;
    using My.Site.NotificationHandlers;
    
    using Umbraco.Cms.Core.Composing;
    using Umbraco.Cms.Core.Notifications;
    
    namespace My.Site.Composers;
    
    public class RegisterNotificationsComposer : IComposer
    {
        public void Compose(IUmbracoBuilder builder)
        {
            builder.AddNotificationHandler<ContentPublishedNotification, MyPublishContentNotificationHandler>();
        }
    }
    

    I hope that helps.

    Kind regards

    Paul

  • Lars Danielsen 5 posts 75 karma points
    Jul 29, 2023 @ 09:11
    Lars Danielsen
    0

    Thank you

    That was indeed my first attempt, but it seem that it does not distribute the events to the other servers. Am I wrong about that?

    I absolutely works for the site on which I do the edit, but nothing happens on the other site.

    The events are supposed to be distributed to the other servers through the JSON in the umbracoCacheInstruction table (afaik), meaning that the events should even be raised if the other server is down, and then starts later. But nothing seems to happen.

    I must admit that I, for now, just test it using two IIS Express sites, with urls like; localhost:12345 and localhost:23456. Is that why It does not work? That does not really make sense.

  • Lars Danielsen 5 posts 75 karma points
    Aug 24, 2023 @ 07:40
    Lars Danielsen
    0

    Good news; it is solved.

    It turns out that it works as expected when I run the site(s) in IIS instead of IIS Express

    Unexpected, and I think it should be mentioned in the documentation (or fixed)

  • Nijaz Hameed 38 posts 173 karma points
    Jan 23, 2024 @ 16:10
    Nijaz Hameed
    0

    Did you use ICachereferesher to refresh the cache or it is already their in Umbraco 9+? I just updated the content in the backend and it seems updated on the other site which is in a different port and connected to the same database.

Please Sign in or register to post replies

Write your reply to:

Draft