Django Caching – Speed Up Your Django WebApp with Caching

Django Caching

In this article, we’ll implement Django caching. We will learn what cache is, why to use them, and then finally, we will code and implement caching in our Web Application.

So let’s get started !!

What is Caching?

Caching is the process of saving the result of a time-consuming calculation so that next time in the future, when required, you can have the result ready in hand.

Even computer CPU store cache files in memory so that those files can be shown faster next time, thus saving a lot of processing time. Most of the websites like FB, WhatsApp also use caching to improve website speeds.

Django Framework has a set of pre-built options which can be used to cache the websites.

The need for caching

Every time you visit dynamic websites (websites containing dynamic elements like templates, views, data in the server, etc.), the server needs to load the template, view, and retrieve data from the server before displaying it. All this processing requires time.

But in today’s era, every user wants his request to be responded quickly, and even a delay of milliseconds can’t be afforded. So to make the websites faster, we can either do the following:

  • Improve CPU hardware
  • Improve server software
  • Improve Databases

Or we could simply use the method of caching !!

Storing the Cache information

Django cache framework also offers different ways to store the cache information:

  • Storing cache in DB
  • Storing cache in a file
  • Storing cache in the memory

We will now look at each of them individually

1) Storing cache in a DB

Here all the cache data is stored inside the database in a separate table just like the model tables.

Hence we need to tell Django to store the cache in DB. To do that, add the following code in the

        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'my_cache_table',

To store cache in a table, we also need to create a table. Hence in the console, run the code

python createcachetable

Django now creates the cache table in the DB with the name given in the – “my_cache_table”

This method is the most used, here the cache speed is dependent on the type of the DB. If you have fast DBs, then this option is the most viable.

2) Storing cache in a file

Here we store the cache as a file in our system. To store the cache as file, add the following code in the :

    'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': 'Absolute_path_to_the_directory',

Here all the cache files are stored in a folder/directory set in the LOCATION attribute.


  • The server should have access to the directory
  • The location should exist before hand.
  • Only the absolute path of the Folder/Directory should be mentioned.

This method is the slowest of all options. But here you don’t need to upgrade your hardware since it is using the already existing storage in the system.

3) Storing Cache in memory

Here we store all the cache files in memory. Django has a default caching system in the form of the in-local memory caching.

To add the caches in local memory, add the code

    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': ('Location1','Location2',...),

Here we can save the cache files in different portions. Add the location of all the portions as a tuple in the LOCATION attribute.

This method is by-far the most powerful and fastest of all the above options.

Prerequisites for Django Caching

Now to cache the website, we must first have a View and a corresponding URL path. So add the following sample View into you

def SampleView(request):
    Html = '<h1>Django Caching<h1><br><p>Welcome to Caching Tutorial</p>'
    return HttpResponse(html)

The URL path for the code will be:

path('sample/', SampleView),

Now for the next section, you can store the cache in any of the form shown above:

Storing different parts of the website as cache

In Django, we can:

  1. Cache only a particular view
  2. Or cache the full website

We will now look at them individually.

1. Per-Site cache storage

To cache the whole site, add the following code in the MIDDLEWARE section of


Note: The order of the code given above is important. Make sure they are present in the same order.

Implementation of the per-site storage cache

Run the server and go to the URL path “/sample”

Per-Site Cache 1
Per-Site Cache

Notice that the website took 13ms to load the site for the first time. Now hit reload and check again.

Per-Site Cache 2
Per-Site Cache 2

Notice that now the page reloaded in just 6ms. The time has reduced to more than half.

2. Per-View cache storage

To cache just a particular View, the syntax used will be:

#Method1: Cach_page syntax in
from django.views.decorators.cache import cache_page

def SampleView(request):
    html = '<h1>Django Caching<h1><br><p>Welcome to Caching Tutorial</p>'
    return HttpResponse(html)

#Method2: Cache_page syntax in
from django.views.decorators.cache import cache_page
urlpatterns = [
    path('sample/', cache_page(200)SampleView),

The cache_page() attribute takes only one argument – The expiry time of the cache in seconds. We can use any of the two methods shown above.

Implementation of the per-View storage cache

Run the server and hit the URL

Per-View Cache 1

The time taken is 22 ms. Now reload and check.

Per-View Cache 2
Per-View Cache 2

See now the time taken has reduced to 8ms only


That’s it, guys!! I hope you have gained good knowledge about caching and how to use them according to our Web application needs and requirements. Do practice all the codes given above to improve your understanding of the topic. See you in the next article !! Till then, keep coding!!