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?
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!
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?
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:
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.
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:
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?
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
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.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?
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
is working on a reply...