HTTP Caching Basics
HTTP Caching Overview
- HTTP caches all files types; it has no intrinsic notion of types.
-
Caching uses two mechanisms:
- expiration . eliminates the need to send requests
- validation . eliminates the need to send full responses
- The client should receive exactly the same response from the cache…
- as it would have received from the origin server…
- had the cache not been involved.
General Caching Timeline
The client makes a request to the origin server.
The cache intercepts the request. If the cache contains a cached response, it checks whether the cached response is fresh. If the cached response is fresh, the cache returns it to the client. Otherwise, the cache sends a validation request to the origin server.
The server receives the validation request. If the cached response is valid, the server returns a 304 not modified response. Otherwise, the server returns a first-hand response.
In either case, the cache receives the server’s response. If it’s a 304 not modified, the cache returns its cached response to the client. Otherwise, it caches the first-hand response from the server, then forwards it on to the client.
Caching Techniques
Here are some use cases and caching techniques that will make them happen.
Table: I would like the client cache to…
Purpose | Headers | Notes |
Allow caching,
force revalidation. |
Cache-Control: private, max-age=0 | google.com does this. |
? | Cache-Control: private, no-cache, no-store, must-revalidate | facebook.com does this. |
? | Cache-control: no-cache, no-store | plus.google.com does this. |
? | Cache-Control: private, max-age=0, must-revalidate | github.com does this. |
HowTo: Inspect Headers with Fiddler
- Open Fiddler.
- Open the Composer tab.
- Type a GET address (e.g. http://www.google.com , http://www.bigfont.ca , etc)
- Turn on capture (F12).
- Click Execute.
- The Inspectors tab will open.
- Click Headers.
- This lets you view cache related headers.
Fiddler Shortcuts
- Use F12 to toggle capture.
- Use Ctrl + X to clear captures.
The Cache Related Headers
Common headers / directives.
Headers
In Request | In Response | |
Age | n/a | The sender’s estimate of the time since the origin server generated the response |
Authorization | n/a | special rules for use in shared caches |
Location | n/a | n/a |
Pragma | implementation specific (e.g. IE 9) | same |
Vary | determines whether to use a fresh response without revalidation | indicates by which fields the response varies |
Warning | n/a | warn of lack of semantic transparency or entity transformations |
Date | date/time of message generation | n/a |
Expires | n/a | date/time after which response is stale |
Cache-Control Directives
In Request | In Response | |
public | n/a | okay to store in any cache |
private | n/a | do not store in a public cache |
no-cache | forces revalidation | okay to store but must revalidate |
no-store |
generally
to protect privacy
;
cache MUST NOT store any part of the request nor any response to it |
same |
s-maxage | n/a | overrides max-age & Expires in shared caches |
max-age |
willing to accept ages up to max-age;
a zero value forces revalidation |
sets the age at which entities become stale;
a zero value forces revalidation on each request |
min-fresh | ? | n/a |
max-stale | willing to accept ages beyond stale | n/a |
only-if-cached | only return a cached response | n/a |
must-revalidate | n/a | never use stale cached entries |
proxy-revalidate | n/a | public caches must revalidate; private one do not need to |
no-transform | do not change the media type | same |
Conditional Headers
Use with methods (e.g. PUT) to make the method conditional. “Server, perform the method if and only if…”
In Request | In Response | |
If-Match | an ETag matches | n/a |
If-Modified-Since | the entity has been modified since | n/a |
If-None-Match | no ETags match | n/a |
If-Range | the entity is unchanged, the send me missing parts only | n/a |
If-Unmodified-Since | the entity has not been modified since | n/a |
Demo
aspnet-caching.azurewebsites.net (coming soon, maybe)
Research Questions
- What file types does HTTP cache?
- What is the level of browser & cache support for each HTTP caching mechanism?
- What are the recommended cache control mechanisms?
- What is the default cache duration?
References
- http://www.w3.org/Protocols/rfc2616/rfc2616.html
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec1.html#sec1.3
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
- http://msdn.microsoft.com/en-us/library/xsbfdd8c%28v=vs.90%29.aspx
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html