Skip to content

Commit

Permalink
Add @api_view example to caching documentation (#9131)
Browse files Browse the repository at this point in the history
  • Loading branch information
BradWells committed Mar 22, 2024
1 parent 3285916 commit 6df5098
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/api-guide/caching.md
Expand Up @@ -59,6 +59,31 @@ class PostView(APIView):
return Response(content)
```


## Using cache with @api_view decorator

When using @api_view decorator, the Django-provided method-based cache decorators such as [`cache_page`][page],
[`vary_on_cookie`][cookie] and [`vary_on_headers`][headers] can be called directly.

```python
from django.views.decorators.cache import cache_page
from django.views.decorators.vary import vary_on_cookie

from rest_framework.decorators import api_view
from rest_framework.response import Response


@cache_page(60 * 15)
@vary_on_cookie
@api_view(['GET'])
def get_user_list(request):
content = {
'user_feed': request.user.get_user_feed()
}
return Response(content)
```


**NOTE:** The [`cache_page`][page] decorator only caches the
`GET` and `HEAD` responses with status 200.

Expand Down

0 comments on commit 6df5098

Please sign in to comment.