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

प्रत्येक View की Caching

Per-view caching एक finished worksheet को photocopy करने जैसा है ताकि teacher हर बार इसे हाथ से फिर से लिखने के बजाय वही copy दे सके।
Syntax
markup
from django.views.decorators.cache import cache_page

@cache_page(seconds)
def view_name(request):
    ...

# Class-based view in urls.py
path('route/', cache_page(seconds)(ViewName.as_view()))

The cache_page Decorator

cache_page एक view को wrap करता है ताकि Django अपने आप इसका rendered response store करे और timeout expire होने तक future requests को वही same response serve करे, view logic को पूरी तरह skip करते हुए।

Note: Timeouts seconds में दिए जाते हैं, इसलिए 60 * 15 का मतलब है 15 minutes।

उदाहरण: The cache_page Decorator

cache_page wraps a view so that Django automatically stores its rendered response and serves that same response to future requests until the timeout expires, skipping the view logic entirely.

markup
from django.views.decorators.cache import cache_page

@cache_page(60 * 15)
def article_list(request):
    articles = Article.objects.all()
    return render(request, 'articles/list.html', {'articles': articles})
{# 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. #}

Class-Based Views को Cache करना

Class-based views के लिए, cache_page directly class के ऊपर apply नहीं हो सकता। इसके बजाय, urls.py में as_view() call को wrap करें, या view के dispatch method पर method_decorator apply करें।

उदाहरण: Caching Class-Based Views

For class-based views, cache_page can't be applied directly above the class. Instead, wrap the as_view() call in urls.py, or apply method_decorator to the view's dispatch method.

markup
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.views.generic import ListView

@method_decorator(cache_page(60 * 15), name='dispatch')
class ArticleListView(ListView):
    model = Article
    template_name = 'articles/list.html'
{# 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. #}

urls.py में Per-View Caching

View को खुद decorate करने के बजाय, आप इसे वहां wrap कर सकते हैं जहां यह urls.py में registered है। यह view function को clean रखता है और caching behavior को उस URL के ठीक पास visible बनाता है जिस पर यह apply होता है।

उदाहरण: Per-View Caching in urls.py

Instead of decorating the view itself, you can wrap it where it's registered in urls.py. This keeps the view function clean and makes the caching behavior visible right next to the URL it applies to.

markup
from django.urls import path
from django.views.decorators.cache import cache_page
from . import views

urlpatterns = [
    path('articles/', cache_page(60 * 15)(views.article_list), name='article-list'),
]
{# 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. @cache_page उस view पर apply करना जो हर logged-in user के लिए अलग content दिखाता है, जिससे users किसी और का cached page देखते हैं।
  2. Cache timeout को underlying data असल में कितनी बार बदलता है उससे कहीं ज़्यादा लंबा set करना।
  3. यह भूल जाना कि cached responses में exact URL cache key का हिस्सा होता है, इसलिए query strings separate cache entries बनाती हैं।
चैप्टर सारांश
  • @cache_page decorator एक पूरे view का response एक set number of seconds के लिए cache करता है।
  • यह एक view function के ऊपर directly apply होता है या एक class-based view के as_view() के around wrap होता है।
  • Per-view caching coarse-grained है -- यह पूरा HTML response cache करता है, सिर्फ इसके कुछ हिस्से नहीं।
  • यह public pages के लिए सबसे अच्छा काम करता है जो हर visitor के लिए same दिखते हैं।

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.