HTTP context

Caching servers and proxies usually use a URL as the caching identifier; however, Magento URLs are not unique enough to allow caching by URL only. (We can cache the customer group, selected language, whether the customer logged in or not, and so on).

To make each cached URL totally unique, we use HTTP context variables. Context variables enable the Magento application to serve different content on the same URL based on customer group, selected language, whether customer logged in or not, and so on.

Context variables must not be specific to exactly one user, because variables are used in cache keys for public content. In other words, a context variable per user results in a separate copy of content cached on the server for each user.

Magento combines context variables in a string, generates the cache from that string, and sets it as the value of the X-Magento-Vary cookie. HTTP proxies can be configured to calculate a unique identifier for cache based on the cookie and URL. For example, our sample Varnish 4 configuration uses the following:

sub vcl_hash {
if (req.http.cookie ~ "X-Magento-Vary=") {
hash_data(regsub(req.http.cookie, "^.?X-Magento-Vary=([^;]+);.*$", "\1"));
... more ...

For an example of a context class, see Magento/Framework/App/Http/Context.


Cache invalidation and private content versioning