Caching in Django Explained

Caching in Django Explained

·

6 min read

When was the first time you encountered caching in Django?

Was it during an interview question like I did?

Most new developers don’t care or even know about caching. You are more interested in making your views presentable and mastering everything about python to worry about caching.

But, caching is a real thing in Django web development and web development as a whole. And if you want to build useful and functional dynamic websites, you need to understand how it works.

What is Caching in Web Development

Caching is the temporary storage of website files to reduce loading time and increase site performance.

Think about this, every time you visit a website, your web browser needs to download the files for your viewing.

Depending on the site you are visiting, this can take a lot of time, especially if the website comes with many files. Now, you don’t necessarily have to download these files again every time you visit the site. Instead, some of these files will be temporarily stored on your computer.

This way, the website will load much faster in the future.

So is caching important? Yes, it is important if you care about your site’s performance. and in this article, we are going to discuss Django caching and the levels provided within this framework.

Django Caching

Django offers a wide range of caching options. However, there are two main ways of caching:

  1. Django built-in caching – These are in-built solutions fully accessible to Django users.
  2. Custom caching – If you have other means of caching, Django will support you too.

Django In-built Caching System

Django offers a selection of different in-built solutions to make work easier for you.

These include:

Memcached

Memcaching is the most efficient way of setting up a caching system. As the name suggests, the Memcached system relies on memory.

This key-value storage system is used to store small amounts of data. Websites such as Facebook are known to use this kind of caching system. The data takes up a part of the RAM without touching the database or file systems.

Setting up a Memcached caching system will require you to also choose a Memcached binding. Django currently supports two bindings i.e. pylibmic and pymemcache.

There are two important considerations that you need to make before using the Memcached system.

  1. Set up the Backend depending on your preferred binding. Your backend will look something like this
django.core.cache.backends.memcached.PyMemcacheCache

or

django.core.cache.backends.memcached.PyLibMCCache
  1. Set up your LOCATION to IP: Port. In this case, IP stands for your site’s IP address and the port is the port you are using. So your location will look something like this:
LOCATION: 127.0.0.1:60706

In this example, the IP address is 127.0.0.1 and the Port is 60706.

Put together, your backend code should look like this:

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

One disadvantage of using Memcached is that it is possible to lose data because you are relying entirely on memory.

Filesystem Caching

The Filesystem caching works by serializing data and storing each value as a separate file. These files are stored in the visitor’s computer allowing the data to be retrieved easily as soon as they revisit the site.

Your BACKEND should look something like this

"django.core.cache.backends.filebased.FileBasedCache

Coupled by an appropriate LOCATION such as

/var/tmp/django_cache

This is the path to where you want your data stored. If you are using windows, remember to start with the drive letter as part of the path.

For example, 'c:/foo/bar'

This way, your code will look like this:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': '/var/tmp/django_cache',
    }
}

On Windows:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': 'c:/foo/bar',
    }
}

Database Caching

It is possible to store cached data in your database, and Django supports this. In fact, you have the option of setting up your caches on a single database or multiple databases.

All you have to make sure of is that you have a fast and well-indexed database server. To set up your database cache:

Your BACKEND should look like this:

django.core.cache.backends.db.DatabaseCache

Your LOCATION should be the name of your table. You can pick any name as long as it is not already in use within your database.

Your code should look something like this:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'my_table_name',
    }
}

Remember to set up your cache table in your database. You can do this by

python manage.py createcachetable

Django will create your cache table and name it after the name you assigned on your LOCATION.

Something else you should note:

If you want to use multiple databases, createcachetable creates one table for each cache

Createcachetable is only used to create missing tables; kinda like what migrate does.

You can learn more about database caching and using multiple databases here.

Local Memory

Local memory caching uses your local memory to store cached data. It is the default method of caching when none other is specified in the settings file.

Local memory caching is a great option for anyone looking for in-memory caching but can’t set up Memcached.

However, it is important to note that while local memory caching is great, it does not fare well in the production environment. Due to its lacking cross-processing caching capabilities, this kind of caching can lead to memory efficiency issues.

But, if you want to use your local memory caching, Set up the BACKEND to:

"django.core.cache.backends.locmem.LocMemCache"

And your LOCATION to your preferred memory stores. Your code will look like this:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'some-name',
    }
}

Note that you do not have to set up the LOCATION if you are only using one locmem cache. This changes the more the local memory caches increase though as you will need to keep them separate.

Dummy Caching

Dummy caching is solely used for development purposes. It is used because it does not store any cached data but will create a cached interface that you can work with.

Say, for example, you are working on a big site with plenty of cached data. You might not want to deal with all this data when working on the development side of things, or when testing out new features.

So you use dummy caching which allows you to work without necessarily changing your code.

Activating dummy caching is simple. Your code should look something like this:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
    }
}

Custom Caching

Some projects will require you to use third-party caching and applications on your Django project. In this case, Django makes provisions for developers to use out-of-the-box caching options.

This is not highly recommended so if you don’t really have to it is best to stick with what Django has to offer.

Still, if you wish to use custom caching for your site, your code will look something like this:

CACHES = {
    'default': {
        'BACKEND': 'path.to.backend',
    }
}

Conclusion

Caching in Django is not as complex as it might seem at first. Now that you have learned and will hopefully master Django's built-in caching systems, you should not stop there.

There is more to caching with Django than meets the eye. Next, we are going to learn about Django Caching Levels, what they mean and how they will impact our code.