ASP.NET Response.Cache.SetNoStore() vs. Response.Cache.SetNoServerCaching()
Asked Answered
G

2

7

Can anyone break down what these two methods do at a HTTP level.

We are dealing with Akamai edge-caching and have been told that SetNoStore() will cause can exclusion so that (for example) form pages will always post back to the origin server. According to {guy} this sets the HTTP header:

Cache-Control: "no-cache, no-store"

As I was implementing this change to our forms I found SetNoServerCaching(). Well that seems to make a bit more sense semantically, and the documentation says "Explicitly denies caching of the document on the origin-server."

So I went down to the sea sea sea to see what I could see see see. I tried both of these methods and reviewed the headers in Firebug and Fiddler.

And from what I can tell, both these method set the exact same Http Header.

Can anyone explain if there are actual differences between these methods and if so, where are hiding in the http response?!

Gregarine answered 24/11, 2011 at 23:41 Comment(0)
P
7

Theres a few differences,

SetNoStore, essentially stops the browser (and any network resource such as a CDN) from saving any part of the response or request, that includes saving to temp files. This will set the NO-STORE HTTP 1.1 header

SetNoServerCaching, will essentially stop the server from saving files, in ASP.NET There are several levels of caching that can happen, Data only, Partial Requests, Full Pages, and SQL Data. This call should stop the HTTP (Full and Partial) requests being saved on the server. This method should not set the cache-control headers or no-store or no cache.

There is also

Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetMaxAge(new TimeSpan(1, 0, 0));

as a possible way of setting cache, this will set the content-expires header.

For a CDN you probably want to set the content-expires header so that he CDN knows when to fetch new content, it if it gets a HIT. You probably don't want no-cache or no-store as this would cause a refetch on every HIT so essentially you are nullifying any benefit the CDN brings to you except they may have a faster backbone connection to the end user than your current ISP but that would be marginal.

Palpebrate answered 25/11, 2011 at 22:56 Comment(3)
Thanks, this is a good explanation of what they "mean" but I'm looking for an explanation of what they "do" - i.e. why do both methods set the Cache-Control header to the exact same value: "no-cache, no-store"? Semantically (as .NET Framework abstractions), SetNoStore seems to be the correct option for stopping CDN/edge-caching (as a middleware "client" of sorts), while SetNoServerCaching refers only to origin server (IIS/ASP.NET) caching.Gregarine
I checked it on my setup and it didn't set no-cache, no-store for the SetNoSeo urverCaching only for the SetNoStore. What version of .NET are you using?Palpebrate
Interesting, it is possible that this header is set somewhere else in the App (.NET 4.0, Sitecore 6.1). I might do some further investigation.Gregarine
S
6

Differnce between the two is

HttpCachePolicy.SetNoStore() or Response.Cache.SetNoStore: Prevents the browser from caching the ASPX page.

HttpCachePolicy.SetNoServerCaching or Response.Cache.SetNoServerCaching: Stops all origin-server caching for the current response. Explicitly denies caching of the document on the origin-server. Once set, all requests for the document are fully processed.

When these methods are invoked, caching cannot be reenabled for the current response.

Shirtmaker answered 25/11, 2011 at 4:44 Comment(1)
You can revert SetNoStore via reflection, and similarly SetNoServerCaching in extreme cases where you don't have simple control over the request lifecycle.Policeman

© 2022 - 2024 — McMap. All rights reserved.