Documentation menu
Docs › Caching
Cache keys
The cache key decides which requests share a cached response. A good key maximises your hit ratio without ever serving the wrong content.
The default key
By default the cache key is made of the scheme, hostname, path and full query string, plus the Accept-Encoding class (Brotli, zstd, gzip or identity). Two requests that differ in any of these are stored separately.
https | www.example.com | /products | ?color=blue&size=m | brQuery strings
Marketing and analytics parameters are the most common cause of a poor hit ratio: every unique utm_ value creates a new cache entry. Remove them from the key, or keep only the parameters your origin actually uses.
[[rules]]
match = "/*"
key = { ignore_query = ["utm_*", "gclid", "fbclid", "ref"] }
[[rules]]
match = "/products/*"
key = { include_query = ["color", "size", "page"], sort_query = true }sort_query treats ?a=1&b=2 and ?b=2&a=1 as the same key. Ignored parameters are still forwarded to your origin on a miss, unless you also set strip_query.
Headers and cookies
If your origin varies its response on a header or cookie, add it to the key explicitly. We honour the origin's Vary header for Accept-Encoding and Accept only; other Vary values are ignored to protect the hit ratio, so declare them in the rule instead.
[[rules]]
match = "/*"
key = { headers = ["accept-language"], cookies = ["currency"] }Device classes
Instead of adding the full User-Agent to the key, which would make almost every request unique, use the device class. Cache.bz classifies each request as desktop, mobile or tablet and sends the result to your origin in x-cachebz-device.
[[rules]]
match = "/*"
key = { device_class = true }Inspecting keys
Send the debug header with a token that has the debug scope to see the exact key for a request:
$ curl -sI https://www.example.com/products?utm_source=mail&color=blue \
-H "x-cachebz-debug: $CACHEBZ_DEBUG_TOKEN" | grep x-cachebz-key
x-cachebz-key: https|www.example.com|/products|color=blue|br|desktopWhen you deploy a different key for a path, existing objects are no longer found under the new key. Expect a short dip in hit ratio and extra origin traffic; deploy outside your peak hours.