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
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.
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?
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 onContentPublishedNotification
,ContentDeletedNotification
andContentSavedNotification
from the backoffice save/publish/delete buttons and clearing the objects from AppCaches.RuntimeCacheI 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:However, this doesn't work because it seems in V9 there is no
CacheUpdated
property in theCacheRefresherBase
class andContentCacheRefresher
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.
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 withContentCacheRefresherNotification
set up as follows, thereby replacingCacheUpdated
completely ?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?
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/
Did you manage to work out how to clear the cache once inside the handle method? Could you share that code?
is working on a reply...