38

I am working on an asp.net MVC web application and I need to know if there are any differences when defining the OutputCache for my action methods as follow:-

[OutputCache(Duration = 0, Location = OutputCacheLocation.Client, VaryByParam = "*")]

VS

[OutputCache(NoStore = true, Duration = 0, Location="None", VaryByParam = "*")]

VS

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

Will all the above three setting prevent caching the data , or each on have different meaning ?

Second question what is the main difference between defining duration=0 & NoStore=true ? will both of them prevent caching ? Thanks

John John
  • 1
  • 72
  • 238
  • 501

1 Answers1

77

The NoStore property is used to inform proxy servers and browser that they should not store a permanent copy of the cached content by setting Cache-Control: no-store within the request header.

Duration simply specifies how long the content of the controller action should be cached, e.g. 10seconds. This will set the Cache-Control: max-age to >= 0. And also sets the Expires header to a valid timestamp.

To your initial question, no, the three variations do not have the same meaning.

[OutputCache(Duration = 0, Location = OutputCacheLocation.Client, VaryByParam = "*")]

create a cache-header like this

Cache-Control: private, max-age=0
Expires: Fri, 03 Jan 2014 12:32:15 GMT

[OutputCache(NoStore = true, Duration = 0, Location="None", VaryByParam = "*")]

creates the following cache-header:

Cache-Control: no-cache, no-store
Pragma: no-cache
Expires: -1

This is basically what you want to see if you want to prevent caching by all means. VaryByParam is optional (at least in MVC5) and the default is "*" anyways, so you can simply use [OutputCache(NoStore = true, Location = OutputCacheLocation.None)] instead.


[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

even creates a public cache control...

Cache-Control: public, no-store, max-age=0
Expires: Fri, 03 Jan 2014 12:36:38 GMT

There is a good post on SO which discusses the difference between max-age=0 and no-cache etc..

At the end all three might prevent caching your data but still have different meanings.

Community
  • 1
  • 1
MichaC
  • 13,104
  • 2
  • 44
  • 56
  • Removing the OutputCache will force some browsers for example IE to cache partial views automatically. that why i have defined this . so if i define NoStore=true , then there is not any meaning for the duration, is that right ?? – John John Jan 03 '14 at 10:16
  • and you mentioned that the three syntax for the Outputcache will not prevent caching ? I think they should prevent caching ? – John John Jan 03 '14 at 10:28
  • 1
    Sorry for confusion, hope my update now answers your question. And you are right, not defining any cache setting for the action will let MVC create a default "private" cache header which lets the browser eventually cache it... – MichaC Jan 03 '14 at 13:04
  • 3
    It's stated correctly later, but the right syntax for the 3rd example is: Location=OutputCacheLocation.None -- you can't just use "None" – RickNZ May 08 '14 at 03:54