set_cache_ttl sets the time-to-live (TTL) for cached resources based on path or HTTP status code.
set_cache_ttl
Syntax
set_cache_ttl(type, ttl)Parameters
| Parameter | Type | Description |
|---|---|---|
type | character | Cache type. Valid values: path, code. |
ttl (when type is path) | numeric | TTL for matched resources, in seconds. |
ttl (when type is code) | character | TTL values for specific HTTP status codes, in the format <code>=<seconds>,.... For example, '301=10,302=5'. |
Return value
Returns true on success, false otherwise.
Examples
Set TTL by path
Use type='path' to apply a TTL to resources matching a URI pattern. The ttl value is numeric, in seconds.
if eq(substr($uri, -4, -1), '.mp4') {
set_cache_ttl('path', 5) // match .mp4 files: cache for 5 seconds
}
if match_re($uri, '^/201801/mp4/') {
set_cache_ttl('path', 50) // match /201801/mp4/ prefix: cache for 50 seconds
}
if match_re($uri, '^/201802/flv/') {
set_cache_ttl('path', 10) // match /201802/flv/ prefix: cache for 10 seconds
}Set TTL by HTTP status code
Use type='code' to assign TTL values to specific HTTP status codes. The ttl value is a string in the format <status-code>=<seconds>, with multiple entries separated by commas.
if match_re($uri, '^/image') {
set_cache_ttl('code', '301=10,302=5') // for /image URIs: cache 301 for 10s, 302 for 5s
}Set separate TTL values for file names and URIs.