我需要用我的 Django Rest Framework 站点实现 Redis 缓存。但是,当我使用带有类的 cache_page 装饰器进行负载测试时,它会提高每秒的请求,但会发生错误“'function' object has no attribute 'get_extra_actions'”
视图.py
@cache_page(CACHE_TTL)
class ParameterViewSet(viewsets.ModelViewSet):
"""
Lists all the parameters present in the system.
Can pass filter with parent set to null to get top level
Parameters.
"""
permission_classes = (IsAuthenticated,)
queryset = Parameter.objects.all()
filter_fields = ('parent', 'target_type',)
serializers = {
'default': ParameterSerializer,
'list': ParameterSerializer,
'detail': ParameterSerializer,
'update': ParameterWriteSerializer,
'create': ParameterWriteSerializer,
'delete': ParameterWriteSerializer,
}
def get_serializer_class(self):
return self.serializers.get(
self.action,
self.serializers['default'])
def get_queryset(self):
results = Parameter.objects.all().filter(weight__gt=0).order_by(
'order_number')
if 'no_parent' in self.request.query_params:
return results.filter(parent=None)
return results
在我的 Settings.py 中,我还添加了以下代码。
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://127.0.0.1:6379/',
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
}
}
}
我也相应地添加了以下中间件
MIDDLEWARE = [
'django.middleware.cache.UpdateCacheMiddleware',
# place all other middlewares here
'django.middleware.cache.FetchFromCacheMiddleware',
]
那也行不通。