File tree 1 file changed +25
-0
lines changed
1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,31 @@ class PostView(APIView):
59
59
return Response(content)
60
60
```
61
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 = {
81
+ ' user_feed' : request.user.get_user_feed()
82
+ }
83
+ return Response(content)
84
+ ```
85
+
86
+
62
87
** NOTE:** The [ ` cache_page ` ] [ page ] decorator only caches the
63
88
` GET ` and ` HEAD ` responses with status 200.
64
89
You can’t perform that action at this time.
0 commit comments