Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expire Output Cache ASP.Net MVC

I am using the standard outputcache tag in my MVC app which works great but I need to force it to be dumped at certain times. How do I achieve this? The page that gets cached is built from a very simple route {Controller}/{PageName} - so most pages are something like this: /Pages/About-Us

Here is the output cache tag that is at the top of my .aspx view page just to be clear:

<@ OutputCache Duration="100" VaryByParam="None" %>

So in another action on the same controller where content is updated I need to dump this cache, or even all of it - it's a very small app so not a big deal deal to dump all cached items.

like image 845
Slee Avatar asked Dec 18 '08 03:12

Slee


People also ask

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.

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.

What is the default cache duration value in ASP.NET MVC?

By default, this attribute filter cache the data till 60 seconds. After 60 sec, if a call was made to this action then ASP.NET MVC will cache the output again.

What is page output caching in asp net?

Output caching is an optimization that reduces Web server response time. Normally, when a browser requests an ASP.NET page, ASP.NET creates an instance of the page, runs any code in the page, runs database queries (if any), dynamically assembles the page, and then sends the resulting output to the browser.


2 Answers

Be careful about using "None" vs. "".

  • If you send "" then the HttpHeader for Vary is not sent.
  • If you send "None" then the HttpHeader for Vary is sent.

I used Fiddler to verify this behavior.

This seems to have an impact on whether or not the browser goes back to the server to check for latest version (causing a 304). At least in Chrome it does. You want to use Varies="" if you know for sure you aren't going to want to update the file before it has expired.

I'd recommend using Varies="" as I did in this post. For my javascript file I dont want the browser going back and making another Http request until it has expired. 304 is unnecessary.

like image 168
Simon_Weaver Avatar answered Oct 14 '22 06:10

Simon_Weaver


HttpResponse.RemoveOutputCacheItem() is probably the method you want to use. If you can figure out what name the actions are cached under, you can remove just the specific action (try setting a breakpoint or dumping all of the names of cached items to the screen)

Otherwise, I'd iterate through the entire output cache and just clear every item.

like image 36
Zachary Yates Avatar answered Oct 14 '22 07:10

Zachary Yates