| 1 |
# Caching |
| 2 |
|
| 3 |
> A certain woman had a very sharp consciousness but almost no |
| 4 |
> memory ... She remembered enough to work, and she worked hard. |
| 5 |
> - Lydia Davis |
| 6 |
|
| 7 |
Caching in REST Framework works well with the cache utilities |
| 8 |
provided in Django. |
| 9 |
|
| 10 |
--- |
| 11 |
|
| 12 |
## Using cache with apiview and viewsets |
| 13 |
|
| 14 |
Django provides a [`method_decorator`][decorator] to use |
| 15 |
decorators with class based views. This can be used with |
| 16 |
other cache decorators such as [`cache_page`][page], |
| 17 |
[`vary_on_cookie`][cookie] and [`vary_on_headers`][headers]. |
| 18 |
|
| 19 |
```python |
| 20 |
from django.utils.decorators import method_decorator |
| 21 |
from django.views.decorators.cache import cache_page |
| 22 |
from django.views.decorators.vary import vary_on_cookie, vary_on_headers |
| 23 |
|
| 24 |
from rest_framework.response import Response |
| 25 |
from rest_framework.views import APIView |
| 26 |
from rest_framework import viewsets |
| 27 |
|
| 28 |
|
| 29 |
class UserViewSet(viewsets.ViewSet): |
| 30 |
# With cookie: cache requested url for each user for 2 hours |
| 31 |
@method_decorator(cache_page(60 * 60 * 2)) |
| 32 |
@method_decorator(vary_on_cookie) |
| 33 |
def list(self, request, format=None): |
| 34 |
content = { |
| 35 |
"user_feed": request.user.get_user_feed(), |
| 36 |
} |
| 37 |
return Response(content) |
| 38 |
|
| 39 |
|
| 40 |
class ProfileView(APIView): |
| 41 |
# With auth: cache requested url for each user for 2 hours |
| 42 |
@method_decorator(cache_page(60 * 60 * 2)) |
| 43 |
@method_decorator(vary_on_headers("Authorization")) |
| 44 |
def get(self, request, format=None): |
| 45 |
content = { |
| 46 |
"user_feed": request.user.get_user_feed(), |
| 47 |
} |
| 48 |
return Response(content) |
| 49 |
|
| 50 |
|
| 51 |
class PostView(APIView): |
| 52 |
# Cache page for the requested url |
| 53 |
@method_decorator(cache_page(60 * 60 * 2)) |
| 54 |
def get(self, request, format=None): |
| 55 |
content = { |
| 56 |
"title": "Post title", |
| 57 |
"body": "Post content", |
| 58 |
} |
| 59 |
return Response(content) |
| 60 |
``` |
| 61 |
|
| 62 |
|
| 63 |
## Using cache with @api_view decorator |
| 64 |
|
| 65 |
When using @api_view decorator, the Django-provided method-based cache decorators such as [`cache_page`][page], |
| 66 |
[`vary_on_cookie`][cookie] and [`vary_on_headers`][headers] can be called directly. |
| 67 |
|
| 68 |
```python |
| 69 |
from django.views.decorators.cache import cache_page |
| 70 |
from django.views.decorators.vary import vary_on_cookie |
| 71 |
|
| 72 |
from rest_framework.decorators import api_view |
| 73 |
from rest_framework.response import Response |
| 74 |
|
| 75 |
|
| 76 |
@cache_page(60 * 15) |
| 77 |
@vary_on_cookie |
| 78 |
@api_view(["GET"]) |
| 79 |
def get_user_list(request): |
| 80 |
content = {"user_feed": request.user.get_user_feed()} |
| 81 |
return Response(content) |
| 82 |
``` |
| 83 |
|
| 84 |
|
| 85 |
!!! note |
| 86 |
The [`cache_page`][page] decorator only caches the `GET` and `HEAD` responses with status 200. |
| 87 |
|
| 88 |
[page]: https://docs.djangoproject.com/en/stable/topics/cache/#the-per-view-cache |
| 89 |
[cookie]: https://docs.djangoproject.com/en/stable/topics/http/decorators/#django.views.decorators.vary.vary_on_cookie |
| 90 |
[headers]: https://docs.djangoproject.com/en/stable/topics/http/decorators/#django.views.decorators.vary.vary_on_headers |
| 91 |
[decorator]: https://docs.djangoproject.com/en/stable/topics/class-based-views/intro/#decorating-the-class |