Fungsi set_cache_ttl menetapkan waktu hidup (TTL) untuk sumber daya yang di-cache berdasarkan path atau kode status HTTP.
set_cache_ttl
Syntax
set_cache_ttl(type, ttl)Parameters
| Parameter | Type | Description |
|---|---|---|
type | character | Jenis cache. Nilai yang valid: path, code. |
ttl (ketika type bernilai path) | numeric | TTL untuk sumber daya yang sesuai, dalam satuan detik. |
ttl (ketika type bernilai code) | character | Nilai TTL untuk kode status HTTP tertentu, dalam format <code>=<seconds>,.... Contohnya, '301=10,302=5'. |
Return value
Mengembalikan true jika berhasil, atau false jika gagal.
Examples
Set TTL by path
Gunakan type='path' untuk menerapkan TTL pada sumber daya yang sesuai dengan pola URI. Nilai ttl bersifat numerik dan dinyatakan dalam satuan detik.
if eq(substr($uri, -4, -1), '.mp4') {
set_cache_ttl('path', 5) // sesuai file .mp4: cache selama 5 detik
}
if match_re($uri, '^/201801/mp4/') {
set_cache_ttl('path', 50) // sesuai awalan /201801/mp4/: cache selama 50 detik
}
if match_re($uri, '^/201802/flv/') {
set_cache_ttl('path', 10) // sesuai awalan /201802/flv/: cache selama 10 detik
}Set TTL by HTTP status code
Gunakan type='code' untuk menetapkan TTL berdasarkan kode status HTTP tertentu. Nilai ttl berupa string dalam format <status-code>=<seconds>, dengan beberapa entri dipisahkan oleh koma.
if match_re($uri, '^/image') {
set_cache_ttl('code', '301=10,302=5') // untuk URI /image: cache 301 selama 10 detik, 302 selama 5 detik
}Tetapkan nilai TTL terpisah untuk nama file dan URI.