Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "invalidate" portions of ASP.NET MVC output cache?

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?

like image 461
Matthew Belk Avatar asked Aug 17 '09 15:08

Matthew Belk


People also ask

How do I invalidate my output cache?

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.

Where is output cache stored in MVC?

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.

How output cache works in MVC?

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.

What is the difference between output cache and ASP.NET data cache?

ASP.NET supports caching of data as well web pages. Applications Data caching enables caching of data whereas Page Output Caching enables Web pages.


1 Answers

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.

like image 198
sirrocco Avatar answered Sep 19 '22 08:09

sirrocco