Is there a way to programmatically invalidate portions of the ASP.NET MVC output cache? What I would like to be able to do is, if a user posts data that changes what would be returned from a cached action, be able to invalidate that cached data.
Is this even possible?
Currently there's no way to invalidate the entire cache, you should do it per cached action (not controller) because the RemoveOutputCacheItem method expects the url of the server side script that it cached. Show activity on this post. This will invalidate the client side cache.
There is a property of the OutputCache attribute 'Location' with following possible values: Any (Default): Content is cached in three locations: the web server, any proxy servers, and the web browser. Client: Content is cached on the web browser. Server: Content is cached on the web server.
The output cache enables you to cache the content returned by a controller action. That way, the same content does not need to be generated each and every time the same controller action is invoked. Imagine, for example, that your ASP.NET MVC application displays a list of database records in a view named Index.
ASP.NET supports caching of data as well web pages. Applications Data caching enables caching of data whereas Page Output Caching enables Web pages.
One way is to use the method :
HttpResponse.RemoveOutputCacheItem("/Home/About");
Another way is described here : http://aspalliance.com/668
I think you could implement the second method by using a method level attribute for every action that you want and just add to it the string representing the key. That's if I understood your question.
Edit: Yes the asp.net mvc OutputCache is just a wrapper .
If you're using varyByParam="none"
then you just invalidate "/Statistics"
- that's if <id1>/<id2>
are querystring values. This will invalidate all versions of the page.
I did a quick test and if you add varyByParam="id1"
and then create multiple versions of the page - if you say invalidate invalidate "/Statistics/id1"
it will invalidate just that version. But you should do further tests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With