Copied to clipboard

Flag this post as spam?

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


  • Debasish Gracias 32 posts 163 karma points
    Aug 30, 2022 @ 11:13
    Debasish Gracias
    0

    How to clear the Cache Tag Helper programmatically in ASP.NET Core using ContentCacheRefresherNotification

    In ASP.NET Core, a cache can be made in a Razor template as follows:

    <cache expires-after="@TimeSpan.FromSeconds(3600)">
    </cache>
    

    I want to be able to clear all caches programmatically by adding custom logic to clear cache on ContentCacheRefresherNotification handler, is this possible?

    My issue is I am creating the cache for a website and when a user changes content I want to clear all caches as what needs to show on the front end may change. Is this possible?

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Aug 30, 2022 @ 12:00
    Marc Goodson
    0

    Hi Debasish

    I've been working on an 'Umbraco' friendly Cache Tag Helper with Warren Buckley for his 'Our-Umbraco-TagHelpers' project: https://github.com/umbraco-community/Our-Umbraco-TagHelpers

    (lots of useful tag helpers here!)

    Anyway there is an open PR here: https://github.com/umbraco-community/Our-Umbraco-TagHelpers/pull/36/files

    with what we came up with and Warren intends to release this some time next week when he is back from holidays.

    Anyway key thing is that .net core hashes the key that stores the cached output of the TagHelper in memory, so you can't easily loop through all the TagHelpers Cache Keys when the Umbraco cache is updated...

    All the code is in that PR, but the gist is, that we create our own 'Our-Cache' Tag Helper, by inheriting from the dot net core one - that doesn't cache in Umbraco preview or Umbraco debug mode, and which generates the CacheKey from the base GenerateKey helper and sticks it in a dictionary in memory.

    Then when a publish event occurs we loop through the dictionary of these keys and pull them out of memory!

    Regards

    Marc

  • Debasish Gracias 32 posts 163 karma points
    Aug 31, 2022 @ 07:07
    Debasish Gracias
    0

    Hi Marc,

    Thank you for your reply and valuable information.

    I came across this blog https://carole.dev/blog/using-asp-net-cache-tag-helper-in-umbraco-9/. It shows how we can add the vary-by parameter to expire cache.

    <cache expires-after="@TimeSpan.FromSeconds(3600)" vary-by="@siteNode.Id + @settings.UpdateDate">
    </cache>
    

    We can test this by updating some content and publishing in Umbraco. Now it will be updated in the cached page as the updated date “vary-by” value has changed, so the cache knows it’s no longer valid.

    I was wondering if the above implementation would work in both single server environments and load balanced (multi-server) environments. Any thoughts?

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Aug 31, 2022 @ 08:21
    Marc Goodson
    0

    Hi Debasish

    Yes, so that instance of the cache will expire after 3600 seconds OR if the Settings node is published, So Editors would need to know if they published a certain page that affected content that was being cached that they would also need to republish the 'settings' page to clear that cache.

    It's quite a common pattern on Umbraco sites to have a page in the content tree that if published will be wired up to break a cache.

    It will work in a distributed environment, as when the settings page is published it will be updated on all the load balanced environments and therefore the settings.UpdateDate will change in those environments too.

    The previously cached version won't immediately disappear from memory, it will hang around until the 3600 expires or the memory cache decides it's not been accessed for a while and naturally it slides out of the cache.

    The example in the tag helper, will actually forcibly remove the previous cached version, when the Umbraco cache is updated.

    Finally if it's important in a distributed environments that the instances share the same cached information, and aren't updated at different times, eg think prices updating every hour... then you could use Distributed Cache Helper:

    https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/distributed-cache-tag-helper?view=aspnetcore-6.0

    Here you can configure the output of Cache Tag Helper to be stored in a resource outside of the server eg SQL or Redis cache... so will be consistent across servers.

    regards

    marc

Please Sign in or register to post replies

Write your reply to:

Draft