Django Caching का Overview
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 हो सके।
उदाहरण: 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.
# 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.
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 किया जाना चाहिए।
उदाहरण: 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.
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. #}
- वह data cache करना जो हर second बदलता है, इसलिए users को stale results दिखते हैं।
- Underlying data update करने के बाद cache को कभी invalidate न करना।
- यह मान लेना कि caching default रूप से on है — Django का default cache backend कुछ नहीं करता जब तक आप एक real backend configure न करें।
- Django का cache framework expensive results store करता है ताकि उन्हें हर request पर recompute न करना पड़े।
- आप
settings.pyमेंCACHESsetting में एक cache backend configure करते हैं। - Caching freshness को speed के लिए trade करती है, इसलिए यह उस data के लिए सबसे अच्छी है जो ज़्यादा नहीं बदलता।
- Django कई levels पर caching support करता है: per-site, per-view, और low-level manual caching।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: