Documentation menu

Docs › Caching

Cache rules

Cache rules decide whether a response is stored at the edge, how long it stays fresh and what happens on the way in and out. Rules live in a cachebz.toml file you keep in version control.

How rules are evaluated

Rules are evaluated top to bottom for every request. Each rule that matches contributes its settings, and later rules override earlier ones for the same setting. This lets you start with broad defaults and narrow them down.

cachebz.toml
[site]
hostname = "www.example.com"
origin   = "origin.example.com"

# 1. Defaults for everything
[[rules]]
match = "/*"
ttl   = "respect-origin"

# 2. Fingerprinted build output never changes
[[rules]]
match = "/assets/*"
ttl   = "365d"
browser_ttl = "365d"

# 3. Never cache personalised areas
[[rules]]
match  = ["/account/*", "/checkout/*", "/api/session"]
bypass = true

Match expressions

The match field takes a path pattern or a list of them. A trailing * matches any suffix. For anything more specific, use a when expression, which can test the method, headers, cookies, query parameters and the visitor's country.

TOML
[[rules]]
match = "/search"
when  = 'method == "GET" && !cookie("session")'
ttl   = "60s"

[[rules]]
match = "/*"
when  = 'header("x-preview") == "1"'
bypass = true

Time to live

ttl controls how long the edge considers a response fresh. Accepted values are a duration (30s, 10m, 12h, 365d), respect-origin to follow the origin's Cache-Control and Expires headers, or 0 to store without serving from cache (useful with stale-if-error).

SettingDefaultDescription
ttlrespect-originEdge freshness lifetime.
browser_ttlunchangedRewrites Cache-Control: max-age sent to browsers.
bypassfalseSend the request to origin and never store the response.
cache_status[200, 203, 204, 206, 300, 301, 404, 410]Status codes that may be stored.
negative_ttl10sTTL for cacheable 404 and 410 responses.
tagsnoneCache tags added to matching responses, for purging.

Origin headers that prevent caching

When ttl = "respect-origin", responses are not stored if the origin sends Cache-Control: private, no-store or no-cache, or a Set-Cookie header. An explicit ttl duration overrides Cache-Control but never Set-Cookie: we will not cache a response that sets a cookie unless you also set strip_set_cookie = true.

Be careful with strip_set_cookie

Only use it on paths where the cookie is not user-specific, such as a consent banner preference. Caching a session cookie would hand one visitor's session to everyone.

Deploying changes

Validate and deploy from your machine or CI. Deploys are atomic and reach every city in about four seconds. Every deploy is versioned, so rolling back is one command.

Shell
cachebz config validate
cachebz deploy --message "Cache product API for 30s"
# Deployed version 142 to 52/52 locations in 3.8s

cachebz rollback 141
Was this page helpful? Tell us what was missing at support@cache.bz. Every message is read by the team that writes these docs.