Retrievel of data from Django Redis Cache is taking great amount of time

Viewed 1096

I'm trying to use Redis in caching my data so that DB calls are reduced. But the time taken for retrieval from cache is much large than the time which I'm getting when fetching the records without caching.

I'm trying to store the django queryset object which consist of around 28k+ records and it's only going to increase as the data increases in the Database.

I'm using Django - 3.0 DRF - 3.10.3 django-redis - 4.12 redis - 3.5.3

CodeWise:

here is my settings.py for setting default cache

CACHES = {
'default': {
    'BACKEND': 'django_redis.cache.RedisCache',
    'LOCATION': [
        f'redis://{REDIS_HOST}:{REDIS_PORT}/1',
        f'redis://{REDIS_HOST}:{REDIS_PORT}/2'
    ],
    'OPTIONS': {
        'CLIENT_CLASS': 'django_redis.client.ShardClient',
        'CONNECTION_POOL_KWARGS': {
            'max_connections': 100,
            'retry_on_timeout': True
        },
        'PASSWORD': os.environ.get('REDIS_PASSWORD')
    }
}

views.py from rest_framework import viewsets from django.core.cache import cache from django.conf import settings from django.core.cache.backends.base import DEFAULT_TIMEOUT from rest_framework.response import Response CACHE_TTL = 60 * 1

class RecordsList(viewsets.ModelViewSet):
   __basic_fields = ('name', 'id')
   # queryset = Records.objects.all()
   serializer_class = RecordsNewSerializer
   http_method_names = ['get', 'post', 'put', 'delete']
   filter_fields = __basic_fields
   search_fields = __basic_fields
   
   def get_queryset(self):
     if 'records' in cache:
        print('from cache')
        queryset = cache.get('records')        
     else:
        print("Saving to Cache")
        queryset = Records.objects.all()
        # queryset = queryset[1:5]
        cache.set('records', queryset, timeout=CACHE_TTL)

     return queryset

Here as you can see I'm saving the django queryset object into the Cache, Now when I'm reducing the object size to say just 100 records, the time taken to get these records is around 400ms and for the entire data it is 3000ms+. Can it be reduced as if I dont add the caching part I'm able to retrieve the entire data( 28k+ ) in around 600ms. So When I add the caching it is increasing the time for the output. Is my approach to implement caching wrong?? If so can anyone help me to get it right by guiding me on the right track!!.

0 Answers
Related