mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-13 05:06:53 +03:00
docs: add example for caching (#7118)
This commit is contained in:
parent
95ae92ef23
commit
82b8a64a02
|
@ -13,13 +13,13 @@ provided in Django.
|
||||||
|
|
||||||
Django provides a [`method_decorator`][decorator] to use
|
Django provides a [`method_decorator`][decorator] to use
|
||||||
decorators with class based views. This can be used with
|
decorators with class based views. This can be used with
|
||||||
other cache decorators such as [`cache_page`][page] and
|
other cache decorators such as [`cache_page`][page],
|
||||||
[`vary_on_cookie`][cookie].
|
[`vary_on_cookie`][cookie] and [`vary_on_headers`][headers].
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
from django.views.decorators.cache import cache_page
|
from django.views.decorators.cache import cache_page
|
||||||
from django.views.decorators.vary import vary_on_cookie
|
from django.views.decorators.vary import vary_on_cookie, vary_on_headers
|
||||||
|
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
@ -27,8 +27,7 @@ from rest_framework import viewsets
|
||||||
|
|
||||||
|
|
||||||
class UserViewSet(viewsets.ViewSet):
|
class UserViewSet(viewsets.ViewSet):
|
||||||
|
# With cookie: cache requested url for each user for 2 hours
|
||||||
# Cache requested url for each user for 2 hours
|
|
||||||
@method_decorator(cache_page(60*60*2))
|
@method_decorator(cache_page(60*60*2))
|
||||||
@method_decorator(vary_on_cookie)
|
@method_decorator(vary_on_cookie)
|
||||||
def list(self, request, format=None):
|
def list(self, request, format=None):
|
||||||
|
@ -38,8 +37,18 @@ class UserViewSet(viewsets.ViewSet):
|
||||||
return Response(content)
|
return Response(content)
|
||||||
|
|
||||||
|
|
||||||
class PostView(APIView):
|
class ProfileView(APIView):
|
||||||
|
# With auth: cache requested url for each user for 2 hours
|
||||||
|
@method_decorator(cache_page(60*60*2))
|
||||||
|
@method_decorator(vary_on_headers("Authorization",))
|
||||||
|
def get(self, request, format=None):
|
||||||
|
content = {
|
||||||
|
'user_feed': request.user.get_user_feed()
|
||||||
|
}
|
||||||
|
return Response(content)
|
||||||
|
|
||||||
|
|
||||||
|
class PostView(APIView):
|
||||||
# Cache page for the requested url
|
# Cache page for the requested url
|
||||||
@method_decorator(cache_page(60*60*2))
|
@method_decorator(cache_page(60*60*2))
|
||||||
def get(self, request, format=None):
|
def get(self, request, format=None):
|
||||||
|
@ -55,4 +64,5 @@ class PostView(APIView):
|
||||||
|
|
||||||
[page]: https://docs.djangoproject.com/en/dev/topics/cache/#the-per-view-cache
|
[page]: https://docs.djangoproject.com/en/dev/topics/cache/#the-per-view-cache
|
||||||
[cookie]: https://docs.djangoproject.com/en/dev/topics/http/decorators/#django.views.decorators.vary.vary_on_cookie
|
[cookie]: https://docs.djangoproject.com/en/dev/topics/http/decorators/#django.views.decorators.vary.vary_on_cookie
|
||||||
|
[headers]: https://docs.djangoproject.com/en/dev/topics/http/decorators/#django.views.decorators.vary.vary_on_headers
|
||||||
[decorator]: https://docs.djangoproject.com/en/dev/topics/class-based-views/intro/#decorating-the-class
|
[decorator]: https://docs.djangoproject.com/en/dev/topics/class-based-views/intro/#decorating-the-class
|
||||||
|
|
Loading…
Reference in New Issue
Block a user