← Back to Apache Course | Chapter 5: Performance | Lesson 3 of 4

Caching लगाना

Caching browsers (और बीच में किसी proxy) को बताता है कि एक file बदली नहीं है और इसे reuse किया जा सकता है, ताकि अगली visit पर इसे दोबारा download करने की ज़रूरत न पड़े -- bandwidth बचाते हुए और pages को तेज़ महसूस कराते हुए।

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 दिखाएगी।

Note: किसी भी चीज़ के लिए जो actually बदल सकती है, long cache lifetimes को cache-busting filenames (path में एक hash या version number) के साथ pair करें -- इस तरह आप long caching safely पाते हैं, बिना users के किसी पुरानी file पर stuck हुए।

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

apacheconf
<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). #}

⚠️ This example can't run in the browser editor. Try it in your own local environment instead.

{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. main HTML page को लंबे समय के लिए cache करना, ताकि visitors site update होने के बाद भी एक पुराना version देखते रहें।
  2. किसी non-versioned filename पर एक लंबा cache lifetime set करना जो actually बदलती है, visitors को cache expire होने तक एक stale file पर stuck छोड़ते हुए।
  3. यह भूल जाना कि ExpiresByType/ExpiresDefault directives का कोई असर होने से पहले mod_expires को enable करना ज़रूरी है।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.