beresp.cacheable

BOOL, can be read and set, but not unset.

Available infetch

Whether or not the object is cacheable. Setting to true will cause otherwise uncacheable responses to be cached. Setting to false will cause otherwise cacheable responses to be passed.

The default value of this variable is determined by the HTTP status code of the response. The following response statuses will cause beresp.cacheable to be set to true at the start of the vcl_fetch subroutine. Any other response status will result in beresp.cacheable being set to false.

StatusDescription
200OK
203Non-Authoritative Information
300Multiple Choices
301Moved Permanently
302Moved Temporarily
404Not Found
410Gone

The behavior for a response with the HTTP status code 206 (Partial Content) depends on whether Segmented Caching is enabled. To enable Segmented Caching, set req.enable_segmented_caching to true in vcl_recv.

If Segmented Caching is enabled, then a backend response with status code 206 will cause beresp.cacheable to be set to true at the start of the vcl_fetch subroutine. If it is not enabled, then a backend response with status code 206 will cause beresp.cacheable to be set to false at the start of the vcl_fetch subroutine.

A common pattern is to override this behavior to allow all 2XX responses not included in the table above to be cacheable:

vcl_fetch { ... }
Fastly VCL
if (beresp.status >= 200 && beresp.status < 300) {
set beresp.cacheable = true;
}

Effects on requst collapsing

If beresp.cacheable is false, the object will not be saved to cache, even if execution of the subroutine ends in return(deliver). A hit-for-pass object will also not be created. Any secondary requests waiting on this response as a result of request collapsing will therefore be dequeued but are liable to form another queue immediately, and be sent to origin consecutively.

Try it out

beresp.cacheable is used in the following code examples. Examples apply VCL to real-world use cases and can be deployed as they are, or adapted for your own service. See the full list of code examples for more inspiration.

Click RUN on a sample below to provision a Fastly service, execute the code on Fastly, and see how the function behaves.

Cache '429' rate-limiter responses per IP

If a backend returns a 429, cache it for the requesting IP but continue to allow other clients to use origin.

Failover to a secondary backend

If primary backend fails, retry with a different backend without caching the failure or reducing cache efficiency.