CacheResponseMixin 不适用于分页

CacheResponseMixin does not work with pagination

我已将 drf-extensions 中的 CacheResponseMixin 添加到我的视图集中,但只有第一页被缓存并返回给所有其他页面,例如/?page=2 只是 returns 第 1 页的结果。

class ProductViewSet(CacheResponseMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    queryset = Product.objects.filter(withdrawn=False)
    serializer_class = ProductSerializer
    pagination_class = LargeResultsSetPagination

我正在使用 Django 1.85。这是一个错误还是我遗漏了什么?

这没有很好的记录,但阅读源代码(对于 PaginationKeyBit class)看起来你需要添加 page_kwarg = 'page'paginate_by_param = 'page'你的观点 class.

使用自定义键构造函数的最终修复:

from rest_framework_extensions.cache.mixins import CacheResponseMixin
from rest_framework_extensions.key_constructor.constructors import (
    DefaultKeyConstructor
)
from rest_framework_extensions.key_constructor.bits import (
    QueryParamsKeyBit   
)

class QueryParamsKeyConstructor(DefaultKeyConstructor):
    all_query_params = bits.QueryParamsKeyBit()

class ProductViewSet(CacheResponseMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    queryset = Product.objects.filter(withdrawn=False)
    serializer_class = ProductSerializer
    pagination_class = LargeResultsSetPagination
    list_cache_key_func = QueryParamsKeyConstructor()