2

Which one of the following is better for setting up the caching options of the static content (i.e. js,css,Images) for asp.net mvc2 applications:

** - Web.config:**

<?xml version="1.0"?>
<configuration>
  <system.webServer>
    <!--Caching-->
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00"/>      
    </staticContent>
  </system.webServer>
</configuration>

Or

<?xml version="1.0"?>
<configuration>
  <system.webServer>
    <!--Caching-->
    <staticContent>      
      <clientCache cacheControlMode="UseExpires" httpExpires="Tue, 31 Dec 2030 12:00:00 GMT"/>
    </staticContent>
  </system.webServer>
</configuration>

In short I want to know which of the options : UseMaxAge , UseExpires are better. What are the impacts on it if I remove the ETags in the request and response headers by using the HttpModules.

Can anyone help me to know more details about the above issue?

Thanks & Regards, Santosh Kumar Patro

santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143

1 Answers1

3

Neither is better, they're different.

  • UseExpires is for specifying an absolute time in the future for when the content expires.
  • UseMaxAge is for specifying a sliding expiration.

I believe browsers don't cache content for more than a year, it's therefore why you see the example you provide of specifying 365 days.

Given your examples I suspect you're trying to get the client to cache the content indefinitely (or as long as possible) therefore you probably want to use:

<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />

This way the client will cache the content for a year, or until it's removed from the browser cache for other reasons, such as the user clearing the cache or the cache reaching a limit.

James Skimming
  • 4,991
  • 4
  • 26
  • 32
  • i can see stack overflow set both "Expires" and "max-age" for their images and css etc , like: `Cache-Control:public, max-age=60` `Expires:Sun, 13 Mar 2016 08:05:18 GMT ` how do they to that ? when i use both in web.config as done [here](http://stackoverflow.com/q/20826035/2218697) i get 500 error internal server error. – Shaiju T Mar 13 '16 at 09:16
  • 1
    These values can be set pragmatically. Though from looking at a trace of the requests, it looks like stackoverflow uses CloudFlare as a reverse proxy, which could be how both are set. – James Skimming Mar 14 '16 at 19:48
  • Yes i received the answer for [this](http://stackoverflow.com/questions/35969498/iis-8-0-add-both-expires-header-and-cache-control) post, and came to conclusion that in IIS we can's set both `Expires and Cache-Control` in web.config, so can i use Nginx and IIS with asp mvc, if yes, then any reference ? – Shaiju T Mar 16 '16 at 09:10
  • 1
    To be honest, you should ask this as a question, not comments to the answer of a related, but different, question. To point you in the right direction though, A reverse proxy is a very different solution, one you should research. Any web service can be proxied (there's probably exceptions), because it doesn't care about the server architecture. I'm not sure why you want to set both "Expires" and "max-age", but if you do, do it pragmatically. Don't introduce a reverse proxy just for that. – James Skimming Mar 16 '16 at 09:23
  • sure i will research , as shopping site we want increase performance by caching, so if i set only `cacheControlMode="UseMaxAge"` i am missing the expire header in response, and google speed test says add expires for images, css, js. so i tried to use both. – Shaiju T Mar 16 '16 at 09:32