4

I have used <cache> tag helper in my Asp.Net Core 3.1 website to cache some parts of the page. However, in some situations I want to clear these cache entries and make the website re-create those. How can I achieve that? I tried the code mentioned in this answer but couldn't get it to work. When I register the service the application seems to ignore it and no entries get cached in my custom service.

Alireza Noori
  • 14,961
  • 30
  • 95
  • 179
  • What do you mean by " clear these cache entries and make the website re-create those" ?Do the [Cache Tag Helper Attributes](https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/cache-tag-helper?view=aspnetcore-3.1) help you complete what you want ? – Xueli Chen Dec 17 '19 at 10:05
  • @XueliChen no the attributes are for when I know beforehand when I want the cache to expire. Usually I don't want these cached sections to expire for at least several days but sometimes I make a change in the data and want to invalidate the cache and ask the website to render that section again. – Alireza Noori Dec 19 '19 at 08:53
  • @AlirezaNoori, do you get any solution for that. I assume it is possible to get the collection by reflection – sina_Islam Dec 07 '21 at 12:18
  • @sina_Islam Unfortunately no. Please let me know if you find anything. – Alireza Noori Dec 21 '21 at 16:16
  • @AlirezaNoori, I get a workaround for the above scenario. You can check https://stackoverflow.com/questions/70272433/get-value-of-vary-by-attribute-of-cache-tag-helper-from-c-sharp/70446789#70446789 – sina_Islam Dec 22 '21 at 09:24

1 Answers1

1

Here is a way to do it:

    private readonly CacheTagHelperMemoryCacheFactory factory;

    public MyClass(CacheTagHelperMemoryCacheFactory factory)
    {
        this.factory = factory;
    }

and later

    (this.factory.Cache as MemoryCache).Compact(1.0);
PavelY
  • 31
  • 3
  • What is this class? Is it a tag helper? If so, how can I access the code on the bottom? Make it a function? How would I get the current cache helper? – Alireza Noori Dec 21 '21 at 16:16
  • This may be any class created using asp.net core dependency injection system. For example it may be a model class. The idea is that you accept CacheTagHelperMemoryCacheFactory in constructor and use it later anywhere in the class. – PavelY Dec 22 '21 at 17:37
  • But if that's the case, how can I use it in razor like I mentioned in my question. I'm looking for a way to use a tag handler on razor and invalidate it if necessary. – Alireza Noori Dec 25 '21 at 22:36
  • If your tag helper is used inside tag, your tag helper is called only when cache is empty. Once content was generated it's stored in the cache. Next time page is rendered your tag does not get called. The content is taken from the cache as string. One way to make your tag helper to re-render is to clear the cache. I'm showing how to do it above. But you can't do it inside of your tag helper logic if it is called inside of the region because once cached your tag helper will not get called. Ideally you should drop cache where the data gets changed. – PavelY Dec 27 '21 at 09:53
  • Ah, this solved my problem of trying to clear all the pages cached with CacheTagHelper. – Garrett Banuk Aug 24 '23 at 19:12