What is caching, when why and how to use it?

Md Sadiqul Islam
2 min readMar 11, 2023

Caching is the process of storing data in memory, typically for a limited period of time, so that it can be quickly retrieved when needed instead of fetching it again from its original source. In Django Rest Framework (DRF), caching is used to improve the performance of web applications by reducing the number of requests made to a server.

There are several types of caching available in DRF, including:

  1. Per-View Caching: This caches the response for a particular view. The cached response is returned when a subsequent request is made to the same view with the same parameters.
  2. Per-User Caching: This caches the response for a particular user. The cached response is returned when a subsequent request is made to the same view by the same user.
  3. Time-Based Caching: This caches the response for a specified period of time. The cached response is returned when a subsequent request is made to the same view within the specified time period.
  4. Cache-Control Headers: This specifies caching information for a particular response, such as whether it can be cached or not, and for how long.

To use caching in DRF, you need to first install a caching backend such as Memcached or Redis. Once you have a caching backend installed, you can enable caching in your DRF application by adding the following code to your settings.py file:

CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}

REST_FRAMEWORK = {
'DEFAULT_CACHE_RESPONSE_TIMEOUT': 60 * 15, # cache for 15 minutes
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
),
'DEFAULT_PARSER_CLASSES': (
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser',
),
}

This code sets up caching with Memcached as the backend, and enables caching for all views by setting the DEFAULT_CACHE_RESPONSE_TIMEOUT to 15 minutes.

You can also enable caching for specific views by using the @cache_page decorator:

from django.views.decorators.cache import cache_page
from rest_framework.decorators import api_view

@api_view(['GET'])
@cache_page(60 * 15) # cache for 15 minutes
def my_view(request):
...

In this example, the my_view function is decorated with @cache_page to enable caching for this specific view.

Overall, caching can greatly improve the performance of your DRF application by reducing the number of requests made to the server. However, it is important to use caching judiciously, as caching can also result in stale data being served if not properly managed.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response