DEV Community

Cover image for Caching Django Application using Redis
Samuel Mugane
Samuel Mugane

Posted on

6 3

Caching Django Application using Redis

Banner Source

Introduction

When building web applications, optimization is key for user experience. In most cases, requests made in the client-side prompt the back-end to communicate with the database.The back-end retrieves the data from the database and relays it to the client. In a normal use case, the client will prompt the server multiple times. Hence, multiple hits are made by the back-end to the database to retrieve data and process it to the client.

In Django, this can be a performance trade-off. Caching can help eliminate the performance trade-off by storing copies of data where it can be accessed more quickly in the client-side when the same request is made multiple times. A browser cache comes into play when handling client-side requests. There are more optimization tricks that one can implement as discussed in the official documentation

A visual of Browser Cache functionality

Cache Functionality
Image Source: Pressidium

Learn more about the browser cache functionality here

Getting Started

To implement caching in Django application. First, set up your Django project and application. Then install django-redis, a project by Jazzband:

pip install django-redis
Enter fullscreen mode Exit fullscreen mode

In settings.py file, add the following configuration:

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {  
          "CLIENT_CLASS":"django_redis.client.DefaultClient",         
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

You can add more configurations as provided in the package documentation.

Implementing caching on views

In this case, the caching functionality will be implemented to a view in an e-commerce API I developed, you can check it here

from rest_framework.views import APIView
from django.core.cache import cache

class ProductDetailView(APIView):

   def get(self, request, *args, **kwargs):
       productID = kwargs['pk']
       if cache.get(productID):
          product = cache.get(productID)
          print("Cache hit")
       else:
           try:
             product = Product.objects.get(pk=productID)
                cache.set(productID, product)
                print("Cache miss")
           except Product.DoesNotExist:
                return Response(status=404)
       serializer = ProductSerializer(product, many=False)
       return Response(serializer.data)

Enter fullscreen mode Exit fullscreen mode

In the code above, when a request is made to the ProductDetailView route, the database will be prompted to retrieve a product based on the productID. When the same request is made, the console will print out Cache hit and use the data stored prior to the request to serve to the client.

When the request is new in the subsequent queue, the console will print out Cache miss since no data regarding the request was cached prior to the request being made. Hence, the database will be prompted again and the back-end will process the new request. Otherwise, a code 404 will be raised to signal that the Product Model doesn't exist; hence, no data is retrievable.

Final Note

In production, the cache configuration in the settings.py file will require changes depending on the cache service used.
The example below is deployment on heroku:

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": os.getenv('REDIS_URL'),
        "OPTIONS": {
          "CLIENT_CLASS":"django_redis.client.DefaultClient",
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The location is changed to match the url of the Cache service for example ElastiCache from AWS.

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup πŸš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay