При использовании кэша в DRF после использования POST-запроса новые данные не отображаются на стороне клиента
Я реализовал кэш для моих представлений статей и установил кэш страницы. В настоящее время проблема, с которой я сталкиваюсь, заключается в том, что когда я пытаюсь выполнить POST запрос данных для создания новой статьи, страница остается прежней.
Что я хочу сделать, так это иметь возможность POST статьи, пока я все еще нахожусь в кэш-странице, и отображать новые данные, находясь в кэш-странице.
далее следует код страницы кэша ...
просмотров статей
class ArticleViewSet(viewsets.ModelViewSet):
serializer_class=ArticleSerializer
permission_classes=[permissions.IsAuthenticated]
authentication_classes = [authentication.TokenAuthentication]
@method_decorator(cache_page(300))
@method_decorator(vary_on_headers("Authorization",))
def dispatch(self, *args, **kwargs):
return super(ArticleViewSet, self).dispatch(*args, **kwargs)
Кто-нибудь может подсказать, как я могу этого добиться...
You cannot.
Cache pages are intended to be static responses, unchanged for the duration of the cache. That's the whole point.
If you want updated content after a POST, you need to not cache that view at all.
Instead, you could cache individual objects in the viewset, using Django's per-view caching. That will allow individual objects to be invalidated from the cache when they are updated, giving you fresh data after POST requests, while still caching most of the content.
Note however that caching viewsets is somewhat fraught, as the various list, detail, create etc. views all use the same URL, but have very different caching requirements.
Generally, REST API endpoints are not great candidates for caching in the first place. The overhead of serialization and deserialization, and the highly dynamic nature of the content, often outweighs any gains from caching.