HTTP - Cache (Cache-Control Header, Bursting, …)

About

This article is about Cache management of response in HTTP and is part of the web cache serie.

Control

HTTP 1.1 - Cache-Control Header

Cache-Control is a request headers that controls the:

Example of headers:

Cache-Control: public
Cache-Control: public, max-age=[seconds] 
Cache-Control: s-maxage=[seconds]
Cache-Control: private
Cache-Control: no-cache
Cache-Control: no-store

The value description is:

Value Description
public Any cache can store a copy of the content
public, max-age=[seconds] Caches can store this content for n seconds
s-maxage=[seconds] Same as max-age but applies specifically to proxy caches (gateway and forward).
private Don't store, this is for a single user
no-cache Re-validate before serving this content
no-store Don't ever store this content

Only the max-age, s-maxage, and private Cache-Control headers will influence proxy caching

HTTP 1.0 - Pragma Header

Pragma is a request headers that allows backwards compatibility with HTTP/1.0 caches. If the Cache-Control header field is present and understood, Pragma is ignored.

Example:

Pragma: no-cache

same effect than

Cache-Control: no-cache

Expires

expires gives the date/time after which the response is considered stale.

Bursting

Cache bursting is a technique that integrates in the URL of the resource a versioning element of the resource such as:

Because the cache is based on the URL, the new URL will be different and the cache will not be used.

If the new version used the same URL, because of the cache, the page may used the old one.

For more information, see the specific article: Web HTTP - Cache Bursting (Content fingerprinting)

Property

Status Code

The HTTP redirection status code 304 shows that response coming from the browser cache

A cache hit is seen by http as a redirection to a cached resource.

Key

The primary cache key consists of:

However, since HTTP caches in common use today are typically limited to caching responses to GET, many caches simply decline other methods and use only the URI as the primary cache key.

How

Apache htaccess configuration

If the mod_header is present, Apache can send the caching header.

Example in the root htaccess, max age is in second

# Cache File
<IfModule mod_headers.c>
    # WEEK
    <FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
        Header set Cache-Control "max-age=604800, public"
    </FilesMatch>

    # WEEK
    <FilesMatch "\.(js|css|swf)$">
        Header set Cache-Control "max-age=604800"
    </FilesMatch>
</IfModule>

Specification

  • Hypertext Transfer Protocol (HTTP/1.1): Caching (rfc7234), R. Fielding, M. Nottingham, J. Reschke. IETF.

Documentation / Reference


Powered by ComboStrap