2017-10-20 10:10:09 +03:00
|
|
|
# Caching
|
|
|
|
|
2019-06-18 22:58:01 +03:00
|
|
|
> A certain woman had a very sharp consciousness but almost no
|
2017-10-20 10:10:09 +03:00
|
|
|
> memory ... She remembered enough to work, and she worked hard.
|
|
|
|
> - Lydia Davis
|
|
|
|
|
|
|
|
Caching in REST Framework works well with the cache utilities
|
|
|
|
provided in Django.
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
## Using cache with apiview and viewsets
|
|
|
|
|
|
|
|
Django provides a [`method_decorator`][decorator] to use
|
|
|
|
decorators with class based views. This can be used with
|
2021-03-09 14:49:19 +03:00
|
|
|
other cache decorators such as [`cache_page`][page],
|
|
|
|
[`vary_on_cookie`][cookie] and [`vary_on_headers`][headers].
|
2017-10-20 10:10:09 +03:00
|
|
|
|
|
|
|
```python
|
2019-11-01 20:56:59 +03:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views.decorators.cache import cache_page
|
2021-03-09 14:49:19 +03:00
|
|
|
from django.views.decorators.vary import vary_on_cookie, vary_on_headers
|
2019-11-01 20:56:59 +03:00
|
|
|
|
2017-10-20 10:10:09 +03:00
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
from rest_framework import viewsets
|
|
|
|
|
2019-11-01 20:56:59 +03:00
|
|
|
|
|
|
|
class UserViewSet(viewsets.ViewSet):
|
2021-03-09 14:49:19 +03:00
|
|
|
# With cookie: cache requested url for each user for 2 hours
|
2023-10-13 14:44:45 +03:00
|
|
|
@method_decorator(cache_page(60 * 60 * 2))
|
2017-10-20 10:10:09 +03:00
|
|
|
@method_decorator(vary_on_cookie)
|
|
|
|
def list(self, request, format=None):
|
|
|
|
content = {
|
2023-10-13 14:44:45 +03:00
|
|
|
"user_feed": request.user.get_user_feed(),
|
2017-10-20 10:10:09 +03:00
|
|
|
}
|
|
|
|
return Response(content)
|
|
|
|
|
2019-11-01 20:56:59 +03:00
|
|
|
|
2021-03-09 14:49:19 +03:00
|
|
|
class ProfileView(APIView):
|
|
|
|
# With auth: cache requested url for each user for 2 hours
|
2023-10-13 14:44:45 +03:00
|
|
|
@method_decorator(cache_page(60 * 60 * 2))
|
|
|
|
@method_decorator(vary_on_headers("Authorization"))
|
2021-03-09 14:49:19 +03:00
|
|
|
def get(self, request, format=None):
|
|
|
|
content = {
|
2023-10-13 14:44:45 +03:00
|
|
|
"user_feed": request.user.get_user_feed(),
|
2021-03-09 14:49:19 +03:00
|
|
|
}
|
|
|
|
return Response(content)
|
2017-10-20 10:10:09 +03:00
|
|
|
|
2021-03-09 14:49:19 +03:00
|
|
|
|
|
|
|
class PostView(APIView):
|
2017-10-20 10:10:09 +03:00
|
|
|
# Cache page for the requested url
|
2023-10-13 14:44:45 +03:00
|
|
|
@method_decorator(cache_page(60 * 60 * 2))
|
2017-10-20 10:10:09 +03:00
|
|
|
def get(self, request, format=None):
|
|
|
|
content = {
|
2023-10-13 14:44:45 +03:00
|
|
|
"title": "Post title",
|
|
|
|
"body": "Post content",
|
2017-10-20 10:10:09 +03:00
|
|
|
}
|
|
|
|
return Response(content)
|
|
|
|
```
|
|
|
|
|
|
|
|
**NOTE:** The [`cache_page`][page] decorator only caches the
|
|
|
|
`GET` and `HEAD` responses with status 200.
|
|
|
|
|
|
|
|
[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
|
2021-03-09 14:49:19 +03:00
|
|
|
[headers]: https://docs.djangoproject.com/en/dev/topics/http/decorators/#django.views.decorators.vary.vary_on_headers
|
2017-10-20 10:10:09 +03:00
|
|
|
[decorator]: https://docs.djangoproject.com/en/dev/topics/class-based-views/intro/#decorating-the-class
|