← Back to Django Course | Chapter 15: Security, Caching & Deployment | Lesson 1 of 9

Django Caching का Overview

Caching हर बार भूख लगने पर kitchen तक चलने के बजाय अपनी pocket में एक snack रखने जैसा है, ताकि आपकी site दूसरी बार तेज़ी से जवाब दे सके।
Syntax
markup
from django.core.cache import cache

cache.set('key', value, timeout_seconds)
value = cache.get('key')
cache.delete('key')

Caching क्यों मायने रखती है

एक Django view को हर request में database queries, template rendering, और Python logic involve हो सकती है। अगर एक page बनाने में expensive है लेकिन ज़्यादा बार नहीं बदलता, तो इसे हर single request पर rebuild करना समय waste करता है।

Caching finished result store करती है ताकि next request वह सारा काम दोहराने के बजाय memory से instantly serve हो सके।

Note: Caching उन pages के लिए सबसे useful है जो बार-बार पढ़े जाते हैं लेकिन कम ही लिखे जाते हैं।

उदाहरण: Why Caching Matters

Every request to a Django view can involve database queries, template rendering, and Python logic. If a page is expensive to build but doesn't change often, rebuilding it on every single request wastes time. Caching stores the finished result so the next request can be served instantly from memory instead of redoing all that work.

markup
# settings.py -- configure a cache backend
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'unique-snowflake',
    }
}
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Low-Level Cache API

Django get, set, और delete methods के साथ एक simple cache object expose करता है। आप एक cache key चुनते हैं, एक optional expiry time के साथ एक value store करते हैं, और इसे recompute करने के बजाय बाद में वापस पढ़ते हैं।

उदाहरण: Low-Level Cache API

Django exposes a simple cache object with get, set, and delete methods. You choose a cache key, store a value with an optional expiry time, and read it back later instead of recomputing it.

markup
from django.core.cache import cache

# Store a value for 300 seconds
cache.set('homepage_stats', {'users': 120}, 300)

# Read it back -- returns None if expired or missing
stats = cache.get('homepage_stats')
if stats is None:
    stats = {'users': 120}  # recompute
    cache.set('homepage_stats', stats, 300)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

क्या Cache नहीं करना चाहिए चुनना

हर चीज़ cache नहीं की जानी चाहिए। किसी specific logged-in user से जुड़ा data, real-time counters, या कुछ भी जो हमेशा accurate होना चाहिए (जैसे एक bank balance) हर बार fresh fetch किया जाना चाहिए।

Warning: Sensitive या तेज़ी से बदलने वाला per-user data cache करना users के बीच stale या wrong information leak कर सकता है।

उदाहरण: Choosing What Not to Cache

Not everything should be cached. Data tied to a specific logged-in user, real-time counters, or anything that must always be accurate (like a bank balance) should be fetched fresh every time.

markup
from django.core.cache import cache

# Safe to cache: same for every visitor
cache.set('site_announcement', 'Welcome!', 3600)

# NOT cached: differs per logged-in user
def get_user_balance(user):
    return user.account.balance  # always fetched live
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# 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. वह data cache करना जो हर second बदलता है, इसलिए users को stale results दिखते हैं।
  2. Underlying data update करने के बाद cache को कभी invalidate न करना।
  3. यह मान लेना कि caching default रूप से on है — Django का default cache backend कुछ नहीं करता जब तक आप एक real backend configure न करें।
चैप्टर सारांश
  • Django का cache framework expensive results store करता है ताकि उन्हें हर request पर recompute न करना पड़े।
  • आप settings.py में CACHES setting में एक cache backend configure करते हैं।
  • Caching freshness को speed के लिए trade करती है, इसलिए यह उस data के लिए सबसे अच्छी है जो ज़्यादा नहीं बदलता।
  • Django कई levels पर caching support करता है: per-site, per-view, और low-level manual caching।

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.