Caching लगाना
In this page:
Cache-Control और Expires
Cache-Control: max-age=604800 browser को बताता है कि यह 7 दिनों तक बिना server से दोबारा पूछे एक file reuse कर सकता है। पुराना Expires header duration की बजाय एक absolute date से मिलता-जुलता काम करता है। modern configs ज़्यादातर Cache-Control इस्तेमाल करते हैं, अक्सर बहुत पुराने clients के साथ compatibility के लिए Expires के साथ।
mod_expires
mod_expires हर extension के लिए Header rule हाथ से लिखने की बजाय, file type से यह headers automatically generate करता है। ExpiresByType image/jpeg "access plus 1 month" Apache को हर JPEG response पर automatically caching headers add करने के लिए कहता है, access होने के समय से एक महीने के लिए valid।
क्या Long-Term Cache होना चाहिए (और क्या नहीं)
Static assets जो कम बदलते हैं -- images, fonts, और खासकर app.a3f9c1.js जैसी versioned/hashed filenames -- लंबे समय के लिए cache करना safe है, क्योंकि एक नया deploy बस एक नई filename produce करता है। main HTML document, और बदलते data वाले किसी भी API response को, आमतौर पर लंबे समय के लिए, या बिल्कुल भी, cache नहीं होना चाहिए, क्योंकि एक stale cached copy outdated content दिखाएगी।
Validators: ETag और Last-Modified
एक cached copy expire होने के बाद भी, browser को पूरी file दोबारा download करने की ज़रूरत नहीं होती -- यह If-None-Match/If-Modified-Since headers के साथ एक conditional request भेज सकता है, और Apache एक सस्ता 304 Not Modified reply देता है अगर file (इसके ETag या modification timestamp से identified) actually बदली नहीं है, इसे दोबारा भेजने की bandwidth बचाते हुए।
उदाहरण: Caching static assets with mod_expires
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
</IfModule>
{# Flagged by hand after confirming a runner can't handle this example
(a shell command / go.mod file stored as a TopicExample, a language
feature the configured runner version doesn't support, or output
that blows a runner's sandbox limit) -- see TopicExample.norun.
Never render the run button for these, regardless of language,
since it would just fail at execute_code (or worse, hang the
Judge0 queue on a submission that can never finish cleanly). #}
- main HTML page को लंबे समय के लिए cache करना, ताकि visitors site update होने के बाद भी एक पुराना version देखते रहें।
- किसी non-versioned filename पर एक लंबा cache lifetime set करना जो actually बदलती है, visitors को cache expire होने तक एक stale file पर stuck छोड़ते हुए।
- यह भूल जाना कि ExpiresByType/ExpiresDefault directives का कोई असर होने से पहले mod_expires को enable करना ज़रूरी है।
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: