Django Cache

Manish Sangwan   23 January,2020  

Django Caching – It’s Easy if you do it in the Smart Way!

Introduction: -

In today’s era of web development, every user wants their requests on the website to be responded ASAP. There are certain methods which will make your website faster. Faster the website, greater the users it will attract.

Let’s take an example of WhatsApp. This popular messaging application works on very low speed in bytes but you can receive text messages instantly because it uses different technology due to which we get the importance of faster response.

Caching is one of those methods which a website implements to become faster. It is cost efficient and saves CPU processing time.

You must be having some questions in your mind –  Is caching a method to make websites faster? The answer is Yes!

Then, what kind of websites will actually benefit from caching? The answer is dynamic websites.

What are Dynamic Websites?

Dynamic websites are a collection of webpages which are generated dynamically.

Simple! So, why is it a big deal? because that generation of webpages means every page requires some processing. Servers aren’t cheap and so is their CPU time.

Their limitation is that they need more CPU time to generate those webpages. More processing means more time added in response time, which makes the response of website slow. It is slow enough that users get annoyed.

Dynamic websites generate highly personalized webpages. They come with tons of features. Some examples of websites are Spotify, Google, Facebook, Instagram, etc.

A very good example can be Google Feed. The personal news feed on google app is generated dynamically. Its contents change in every 2-3 hrs. The updating process takes time if you refresh.

What is Caching?

Caching is the process of storing recently generated results in memory. Those results can then be used in future when requested again.

The computer memory works in a similar manner. CPU stores some files in the cache Memory. And, when CPU needs them again it looks for those files in Cache Memory first. Since Cache Memory is fast, the processing time improves. We will be implementing something similar on our server.

Caching on Server

Suppose we have a dynamic website. When a user request for some page:

1. A Dynamic Page generates

The requested dynamic page generates. Since it is generating for the first time, it will take a little bit longer.

The CPU will process the page and that time will add to the response time.

2. A webpage serves and gets stored in Cache Space

Now, before serving this page to the client, it stores in Cache Space. Cache Space is a location where you temporarily store data to be retrieved for later use. Then it will serve to the client.

3. Multiple users request for the same page

Maybe, there are other clients requesting the same page. So, instead of generating webpage again we use the resource in Cache Space.

4. Server responses with Cached Webpage

Since we have that same resource stored in our Cache Space, our server will respond to these users with that webpage. The response time will decrease by the processing time. This makes the response much faster.

5. Deletion of data in Cache Space

Cache Space store copies of data. To keep the redundancy of data minimum, we delete the Cache Data after some time. This makes room for new Caching Data and the cycle goes on.

Caching space is also a factor to improve speed. There can be three types of Cache Spaces:

  1. Main Memory
  2. Database Tables
  3. File-System or local directories

 

Caching in Django

Caching is important for a dynamic website. Since Django is a dynamic framework, it comes with built-in options for you to manage your cache.

Django allows caching on different caching spaces as well as on different parts of a website. There can be certain parts of the website which can demand more CPU time and granularity to implement caching on them individually. This makes caching efficient.

We learned about different caching spaces in the previous section. Now, let’s implement caching on all of them.

 

Setting-up Caching System

We will be setting up different caching frameworks or spaces. Your system may only need one so implement one, which fits your requirements with given constraints.

What is Memcached Framework?

Memcached is a memory-based cache framework. This software takes a certain part of the main memory and converts it to Cache Space. It handles that efficiently so the main memory is never completely full.

When a request comes to the server, it will first look in Cache Space. Memcached provides faster response since the main memory is fastest of all. If the resource is not present in Cache Space, the server generates that resource. That is again stored by Memcached, if the developer has cached it.

The Memcached framework can work on multiple servers. We can change the LOCATION key and it can have different values, like the IP addresses of other servers.

When Memcached is expanded to multiple servers, it considers all of them as a single server. This approach reduces data-redundancy to a certain level. This is also one of the reasons Memcached Caching is preferred over other types of Caching.

The Memcached framework is one of the most popular choices for caching. It is used by almost all the servers. It can be difficult to implement on Windows/ Mac as it is designed for Debian/ Ubuntu Operating System, which is server OS.

You can get more on its installation and documentation on www.memcached.org. This is an official website.

To use Memcached framework in Django:

  1. Install Memcached framework on your system
  2. Install Python connectors for Memcached Framework
  3. Edit our Settings.py file

As mentioned earlier, installing Memcached framework on Windows/ Mac OS is difficult. If it is installed then proceed. And, if you have Debian/Ubuntu OS, then there are no issues.

Next, we require python connectors for Memcached.

 

The link to the package will provide you all the documentation and it also has the command for the installation.

The link to python connector is https://pypi.org/project/python-memcached/

The last step is to modify settings.py file. Add these lines of code in projects settings.py file.

It completes your Memcached Framework Setup.

Types of Caching in Django

There are different options of caching in Django:

1. Database Caching

Database Caching is also one of the viable options when there is a fast database server. This type of Caching is more common and is most easily applicable.

We will add this code section to settings.py file. Remove that Memcached section or comment it out.

Now, before starting our server, we will need to specify Django where to store its data. There shall be a particular table for it.

In your command line, enter

python manage.py createcachetable

When you run this code, we get a table like this:

This table should have these fields.

2. File-System Caching

Django has this option as well. We can cache our data on our file system or any directory on the server, rather than main memory or database. This option is the most cost-efficient of all as it requires no hardware upgrades at all. The previous caching setups required better hardware as a prerequisite.

This caching is slowest of all Cache Spaces. The software does depend on what you want to use. There are lots of frameworks out there and most of them work fine with Django.

The File-based Caching means storing caches as individual files. The engine we use for file-system will serialize files. That makes it a better way to manage files.

Just add this Code Section to settings.py file. Remember to delete other CACHE lists.

This code contains the information of BACKEND engine for caching. Also, specify the location of the directory where the cache is stored.

Some Important Points:

  • The server (Apache) should have access to this directory.
  • The directory should exist before running this code.
  • LOCATION should contain the absolute directory path. Django searches from the root of your file-system.

3. Local-Memory Caching

Django has a default caching system in the form of local-memory caching. It is very powerful and robust. This system can handle multi-threaded processes and is efficient.

It is best for those projects which cannot use Memcached framework. It provides responses at the same speed.

To implement local-memory caching, add this CACHES Dictionary to your settings.py file.

This code will set up your local memory cache. You can skip the location part if you require only one portion to store cache and if you want to distinguish between caches then you should use LOCATION.

4. Dummy Caching

Django also provides you with dummy caching. This implementation allows you to develop a website in a production environment.

Suppose your website is in a production environment. There are some sections of code which use caching and you may not be capable of implementing caching in your development environment.

Then, dummy caching will provide you the solution. It actually doesn’t cache anything and makes your server to interpret that caching is implemented.

This approach is really helpful for developers. They don’t need to write code for implementing caching again. Thus, providing error-free websites and faster development.

Add this code to your settings.py file to implement Dummy Caching.

Dummy caching is important for developers who don’t have powerful personal machines. It bypasses many caching setups for you.

5. Custom Cache System

Django supports a wide variety of caching implementations. If you want something that isn’t natively supported by Django like making your own cache backend, then you might want this setting which is really helpful.

You can refer to cache backend in Django, get information on which fields are there and create your own backend accordingly.

This code might be helpful here.

Implementing Caching in Django

In previous sections, we completed the setup of our caching system. Now, to implement caching on the website, there are these 2 popular ways.

We are using Per-site Caching here:

Per-site Caching:

Suppose we want our whole website to be cached. The part of per-site caching comes there. Just by adding this your whole website will be cached.

Open your settings.py file and add these middlewares. Edit your MIDDLEWARE list.

The order of middlewares is important over here, so keep that in mind. That’s it, you have cached your website now. After that, try running your server and then you can see that your webpages are being cached.

0
Like