4

I have html fragments in a partial view that uses the output cache like so

 <cache vary-by="@Util.SomeID" expires-after="@TimeSpan.FromHours(1)">
     <div>some content</div>
 </cache>

In the event that I need the content to be updated before the expiration of the cache, how do I go about busting the cache for that particular fragment (or the entire cache, if necessary) ?

I'm using ASP.NET 5 and MVC 6..

Thanks!

Simon Ordo
  • 1,517
  • 2
  • 14
  • 23

2 Answers2

1

The IMemoryCache interface doesn't have a method to clear the cache. Otherwise you could inject IMemoryCache to your controller or where ever you need it and call the method.

I suggest to post a feature request on the aspnet caching repository or use a different cache mechanism.

I'm working on extending CacheManager to be usable in MVC Core as memory or distributed cache which then will have a lot more functionality than the build in, pretty simple, memory cache...

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
MichaC
  • 13,104
  • 2
  • 44
  • 56
0

The ASP.NET team has confirmed that it's not possible to directly clear cache items today. The workaround suggested is to use a "version" variable as part of the vary-by cache parameter.

That will not actually clear out the existing value, but the new value will be rendered. The old value will eventually be cleaned up as it expires.

More details at https://github.com/aspnet/Mvc/issues/4117#issuecomment-187298466

Simon Ordo
  • 1,517
  • 2
  • 14
  • 23