Search code examples
redisdjango-rest-frameworkdjango-rest-viewsets

How to implement Redis Cache with Django Rest Framework?


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.


Solution

  • 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: