Skip to content

Commit 6df5098

Browse files
authored
Add @api_view example to caching documentation (#9131)
1 parent 3285916 commit 6df5098

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/api-guide/caching.md

+25
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ class PostView(APIView):
5959
return Response(content)
6060
```
6161

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 = {
81+
'user_feed': request.user.get_user_feed()
82+
}
83+
return Response(content)
84+
```
85+
86+
6287
**NOTE:** The [`cache_page`][page] decorator only caches the
6388
`GET` and `HEAD` responses with status 200.
6489

0 commit comments

Comments
 (0)