Invalidating cache tag helper programmatically
# help-with-umbraco
c
I was looking for a solution for the
<cache>
tag helper to be invalidated on publish and I found @Warren Buckley and @marcemarc's
<our-cache>
tag helper which i like the look of. https://our.umbraco.com/forum/using-umbraco-and-getting-started/109868-how-to-clear-the-cache-tag-helper-programmatically-in-aspnet-core-using-contentcacherefreshernotification#comment-340478 The only thing I have found is that when I am testing out the cacherefresher notification to log when it fires, in a single site with no load balancing, it doesn't log anything at all, meaning it's not hitting that event at all. Does this mean that this won't work for non load balanced environments or does it mean that event doesn't fire in dev or something else?
w
Heya Paul. Not sure I understand the problem fully here? So using the cache taghelper is not working as you expect or …?!
m
It detects debug mode and doesnt cache if I recall
c
Hi @Warren Buckley, sorry my point was that with this we are relying on the cache refresher notification being sent in order for this to clear the cache, but if I just do a test of hooking into the content cache refresher event in a non load balanced environment and I publish the home page, my test which would write a log to say it fired does not get hit. So I am wondering if that is because my application is not set up for load balancing or because i am in dev or would you expect it to be hit in these circumstances. This is before installing your package. I'm trying to work out if it is relevant in all circumstances
w
Thanks Matt good remembering that it doesn’t do anything when in dev !
Hmm that event should fire AFAIK but would need to go source code diving to be certain
So we use that notification as well and my test site is not load balanced at all
c
Haha hang on, silly me, i may not have done the composer bit, just the notification bit. Sorry for wasting your time
w
No worries mate - happens to the best of us
c
Copy code
cs
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;

namespace MyProject.NotificationHandlers;

public class CacheRefresherNotificationHandler :
    INotificationHandler<ContentCacheRefresherNotification>,
    INotificationHandler<DictionaryCacheRefresherNotification>,
    INotificationHandler<MediaCacheRefresherNotification>
{

    private readonly ILogger<CacheRefresherNotificationHandler> _logger;

    public CacheRefresherNotificationHandler(ILogger<CacheRefresherNotificationHandler> logger)
    {
        _logger = logger;
    }

    public void Handle(ContentCacheRefresherNotification notification)
    {
        _logger.LogInformation("Content Cache Cleared");
    }

    public void Handle(DictionaryCacheRefresherNotification notification)
    {
        _logger.LogInformation("Dictionary Cache Cleared");
    }

    public void Handle(MediaCacheRefresherNotification notification)
    {
        _logger.LogInformation("Media Cache Cleared");
    }
}
Copy code
cs
using MyProject.NotificationHandlers;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Notifications;

namespace MyProject.Composers;

public class RegisterNotificationHandlersComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        builder.AddNotificationHandler<ContentCacheRefresherNotification, CacheRefresherNotificationHandler>();
        builder.AddNotificationHandler<MediaCacheRefresherNotification, CacheRefresherNotificationHandler>();
        builder.AddNotificationHandler<DictionaryCacheRefresherNotification, CacheRefresherNotificationHandler>();
    }
}
Had chance to test out that these notifications do get called in a non load balanced environment. If i save and publish a content item it logs
Content Cache Cleared
If I save a media item it logs
Media Cache Cleared
And if I save a dictionary item it logs
Dictionary Cache Cleared
This gives me confidence to go with this
<our-cache>
tag helper and it has helped me understand umbraco a bit more.
w
No problem, glad you will find it useful & that you learnt about these notification hooks to be notified about cache invalidation stuff inside Umbraco
2 Views