Description
Cache Varies¶
Cache objects are stored and retrieved using a Cache Key. An optional component of the cache key is the “vary string.” It’s this vary string that allows you to save multiple cache objects from the same URL.
This is useful in a number of situations including those where the desktop and mobile views of a page are different, or where the currency displayed on a page varies by the visitor’s geographical location.
Vary Types¶
There are two types of cache varies, which may be used together to make up the vary string component of the cache key: vary cookies and a vary environment value.
Vary Cookies¶
When you instruct the the LiteSpeed Cache Engine to vary on a cookie, you are not actually setting the cookie. You are telling the Cache Engine to look for the cookie name, and then vary on the value of that cookie. Vary strings can contain multiple vary cookies.
Example
Suppose the server is instructed to vary on the cookie my_cookie, and the following four requests come in:
- Request A has NO cookies, so the vary string is blank.
- Request B has
my_cookiewith a value ofAlabama, so the vary string ismy_cookie = Alabama. - Request C has
my_cookiewith a value ofCalifornia, so the vary string ismy_cookie = California. - Request D has
my_cookiewith a value ofAlabama, so the vary string ismy_cookie = Alabama.
As a result of these four requests, three versions of the URL have been cached: one with a blank vary string (A), one with a my_cookie = Alabama vary string (B and D), and one with a my_cookie = California vary string (C).
Tip
By default, LiteSpeed servers will recognize any cookie that starts with _lscache_vary as a vary cookie. This cookie is always added as part of the cache key, unless the no-vary cache-control is set in the response header with X-litespeed-cache-control: no-vary.
Vary Environment Value¶
Unlike vary cookies, a vary value is not made up of a key/value pair. It is simply an environment value, and it may only be used once. If multiple vary values are set, only the last one is used. A vary value is often used to indicate that the request is coming from a mobile device, or which country the request is coming from, among other things.
Example
- Request A has no vary value, so the vary string is blank.
- Request B sets a vary value of
ismobile, so the vary string isismobile. - Request C sets vary value
US, so the vary string isUS. - Request D sets vary value
US, so the vary string isUS.
As a result of these four requests, three versions of the URL have been cached: one with a blank vary string (A), one with a ismobile vary string (B), and one with a US vary string (C and D).
Mixed Varies¶
Multiple vary cookies and a vary value may all be set and used together to build the vary string.
Example
Suppose the server is instructed to vary on the cookies my_cookie and my_cookie2.
- Request A has no cookies and no vary value is set, so the vary string is blank.
- Request B has the cookie
my_cookie=Alabamaand no vary value is set, so the vary string ismy_cookie=Alabama. - Request C has the cookie
my_cookie=Alabamaand vary valueismobileis set, so the vary string ismy_cookie=Alabama + ismobile. - Request D has the cookies
my_cookie=Alabamaandmy_cookie2=Appleand vary valueismobileis set, so the vary string ismy_cookie=Alabama&my_cookie2=Apple + ismobile - Request E has no cookies, but vary value
ismobileis set, so the vary string isismobile.
The five requests will all be served a different cache entry, because their final VARY results are different.
Implementing Varies¶
In general, response headers are preferred for vary cookies, and rewrite rules are not required. Once a vary cookie is set by the browser, it will continue to be sent with each request for that URL until that cookie expires or is unset. Therefore, a rewrite rule is not required for retrieval of the cache object.
It’s different with a vary value, as it is not passed along by the browser. You need to use both rewrite rules and response headers together to implement a cache vary based on a vary value. The response header instructs the cache engine to store the vary, and a companion rewrite rule uses the vary value for retrieval of the cache object.




Reviews
There are no reviews yet.