我正在使用 Django Rest Framework 和 DRF-Extensions 进行缓存。
我有一个带有自定义list()
和retrieve()
方法的视图集。我可以将@cache_response()
装饰器放在方法上,它会成功获取并设置到缓存中。但是,如果我尝试使用CacheResponseMixin
没有任何反应。
作品:
class SeriesViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = SeriesSerializer
def get_queryset(self):
series_type = EntityType.objects.get(name='series')
return Container.objects.filter(type=series_type)
@cache_response()
def list(self, request):
series = self.get_queryset()
serializer = SeriesSerializer(series, many=True)
return Response(serializer.data)
@cache_response()
def retrieve(self, request, pk=None):
name = pk
series = self.get_queryset()
show = series.get(data__title=name)
serializer = SeriesSerializer(show)
return Response(serializer.data)
不工作:
class SeriesViewSet(CacheResponseMixin, viewsets.ReadOnlyModelViewSet):
serializer_class = SeriesSerializer
def get_queryset(self):
series_type = EntityType.objects.get(name='series')
return Container.objects.filter(type=series_type)
def list(self, request):
series = self.get_queryset()
serializer = SeriesSerializer(series, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
name = pk
series = self.get_queryset()
show = series.get(data__title=name)
serializer = SeriesSerializer(show)
return Response(serializer.data)
没有给出错误,我的缓存条目根本没有被创建。