SymfonyWorld Online 2020
100% online
30+ talks + workshops
Live + Replay watch talks later

HTTP Cache Expiration

HTTP Cache Expiration

The expiration model is the most efficient and straightforward of the two caching models and should be used whenever possible. When a response is cached with an expiration, the cache returns it directly without hitting the application until the cached response expires.

The expiration model can be accomplished using one of two, nearly identical, HTTP headers: Expires or Cache-Control.

Expiration with the Cache-Control Header

Most of the time, you will use the Cache-Control header, which is used to specify many different cache directives:

// sets the number of seconds after which the response
// should no longer be considered fresh by shared caches

The Cache-Control header would take on the following format (it may have additional directives):

Cache-Control: public, s-maxage=600

Expiration with the Expires Header

An alternative to the Cache-Control header is Expires. There’s no advantage or disadvantage to either: they’re just different ways to set expiration caching on your response.

According to the HTTP specification, “the Expires header field gives the date/time after which the response is considered stale.” The Expires header can be set with the setExpires() Response method. It takes a DateTime instance as an argument:

$date = new DateTime();
$date->modify('+600 seconds');


The resulting HTTP header will look like this:

Expires: Thu, 01 Mar 2011 16:00:00 GMT


The setExpires() method automatically converts the date to the GMT timezone as required by the specification.

Note that in HTTP versions before 1.1 the origin server wasn’t required to send the Date header. Consequently, the cache (e.g. the browser) might need to rely on the local clock to evaluate the Expires header making the lifetime calculation vulnerable to clock skew. Another limitation of the Expires header is that the specification states that “HTTP/1.1 servers should not send Expires dates more than one year in the future.”


According to RFC 7234 - Caching, the Expires header value is ignored when the s-maxage or max-age directive of the Cache-Control header is defined.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.