Copied to clipboard

Flag this post as spam?

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


  • Arvind 4 posts 94 karma points
    Apr 08, 2022 @ 08:01
    Arvind
    0

    How to hook into PageCacheRefresher.CacheUpdated event?

    Hello, I have a situation where my cache doesn't seem to be cleared when using Umbraco Cloud (on the standard plan).

    In some parts of my code I have been querying the content tree and processing the content, then adding the processed data into AppCache.RuntimeCache.

    I implemented a custom class with INotificationHandler to trigger on ContentPublishedNotification, ContentDeletedNotification and ContentSavedNotification from the backoffice save/publish/delete buttons and clearing the objects from AppCaches.RuntimeCache

    I used the documentation available for accessing the cache and updating it.

    This works fine locally but seems to be erratic on Umbraco Cloud and thought maybe this could be linked to load-balanced scenarios, since INotificationHandler will trigger only on the master server where the backoffice resides.

    After much digging I came across this documentation about ICacheRefresher which mentions distributed cache management but apparently, from this forum post here, this is used when you have custom implementation in the backoffice and use that to trigger an event on all nodes.

    The same post mentions the usage of PageCacheRefresher.CacheUpdated instead and digging some more, I found another forum post showing how to use it.

    Given my scenario, I believe I could just listen to that CacheUpdated event and trigger my cache clearing for my type. I inspired myself from the code in this GitHub issue and tried the following:

    public class NewsCacheRefreshed : IComponent
    {
        public void Initialize()
        {
            CacheRefresherBase<ContentCacheRefresher>.CacheUpdated += CacheUpdated;
        }
    
        private void CacheUpdated(ContentCacheRefresher sender, CacheRefresherEventArgs e)
        {
          // clear my cache here
        }
    
        public void Terminate()
        {            
        }
    }
    

    However, this doesn't work because it seems in V9 there is no CacheUpdated property in the CacheRefresherBase class and ContentCacheRefresher needs to be Notification type class.

    I'm at a loss on how to proceed here since I could not find the CacheUpdated property anywhere, how do I susbcribe to those events? The documentation is completely lacking in this regard, having searched through and through for a couple of days now.

    Any help regarding this would be much appreciated, thank you.

  • Arvind 4 posts 94 karma points
    Apr 08, 2022 @ 13:43
    Arvind
    100

    Ok, I might be stupid here and could have had something staring at me all the time: INotificationHandler. Will this work in a distributed workload with ContentCacheRefresherNotification set up as follows, thereby replacing CacheUpdated completely ?

    public class NewsContentNotification : INotificationHandler<ContentCacheRefresherNotification>
    {
        public void Handle(ContentCacheRefresherNotification notification)
        {
            // Do my cache clearing here
        }
    }
    

    This works on my local system, I can see the Handle function being called but need to know if that would trigger on a distributed system?

  • Arvind 4 posts 94 karma points
    Apr 14, 2022 @ 09:06
    Arvind
    0

    Well, nevermind, seems the documentation here does mention that Events has been moved to Notifications in Umbraco 9.

    https://our.umbraco.com/Documentation/Reference/Notifications/

  • JacobS 7 posts 107 karma points c-trib
    Nov 01, 2022 @ 12:46
    JacobS
    0

    Did you manage to work out how to clear the cache once inside the handle method? Could you share that code?

Please Sign in or register to post replies

Write your reply to:

Draft