प्रत्येक View की Caching
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 करते हुए।
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.
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.
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.
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. #}
@cache_pageउस view पर apply करना जो हर logged-in user के लिए अलग content दिखाता है, जिससे users किसी और का cached page देखते हैं।- Cache timeout को underlying data असल में कितनी बार बदलता है उससे कहीं ज़्यादा लंबा set करना।
- यह भूल जाना कि cached responses में exact URL cache key का हिस्सा होता है, इसलिए query strings separate cache entries बनाती हैं।
@cache_pagedecorator एक पूरे 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 दिखते हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: