How to implement Redis Cache with Django Rest Framework?

我们两清 提交于 2020-12-15 08:00:34

问题


I need to implement Redis cache with my Django Rest Framework site. But when I do load test using cache_page decorator with a class it improves the request per second but an error occurs "'function' object has no attribute 'get_extra_actions'"

Views.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

In my Settings.py, I have also added the below code.

CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': 'redis://127.0.0.1:6379/',
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
        }
    }
}

also i have add the below middleware accordingly

MIDDLEWARE = [
    'django.middleware.cache.UpdateCacheMiddleware',

    # place all other middlewares here

    'django.middleware.cache.FetchFromCacheMiddleware',
]

that too doesn't work.


回答1:


Just from top of my head you are using the @cache_page(CACHE_TTL) at the wrong place and in the wrong way.

According to django docs' you need the cache_page decorator to be above class' method not the class itself and it need to be wrapped in method_decorator.

Overwrite ParameterViewSet's dispatch like below and it should do the job.

@method_decorator(cache_page(CACHE_TTL))
def dispatch(self, *args, **kwargs):
    return super().dispatch(*args, **kwargs)

Docs:

  • Decorating the class
  • Using cache with apiview and viewsets


来源:https://stackoverflow.com/questions/56038304/how-to-implement-redis-cache-with-django-rest-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!