Django Caching: To cache something is to save the result of an expensive calculation so that you don’t have to perform the calculation next time. (official doc).
Caching is one of the most effective ways to boost an application’s performance. For dynamic websites, when rendering a template you’ll often have to retrieve data from various sources like databases, the file system, and third-party APIs. Sometimes this is very expensive and slows as the applications grow. One of the better ways to solve this add some caching techniques and store the most requested data or data that’s doesn’t change in every user in the cache. In this method, data can be retrieved easier.
Importance of Caching
Caching is nothing but, it is a process of fetching data stored in a cache. With good cashing, you can relieve the database from a high load and also reduces the page load time, which is always good. Now we can list some of the importance of caching.
- Helps to speed up application performance and increase efficiency
- Improve user experience and encourage people to use their sites
- Stores data locally
Types of Caching
There are different types of caching available in Django. They are Memory cache, Database cache, File system cache, Local memory cache, etc. So you need to find which cache is more efficient for your application.
Memory Caching
It is a type of cache that stores data in memory called Memory cache the most efficient type of memory cache supported by Django is Memcache. Which is a fast memory-based cache that can handle high loads of data and hence increasing performance in Django applications and also decreasing the database load. It provides a fast interface for adding, retrieving, and deleting data from the cache. Here all data are stored in memory instead of a database.
To set up Memcached you need to add the following code in your settings.py of your Django applications
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
You also need to set up Memcached on your server. Here the backend key specifies the caching backend, while the location defines where the specified backend is running. In the location settings above, localhost is specified as the cache location port is 11211. One thing to note about Memcache is that it does not persist data, so if Memcache is restarted the cache is empty again and needs to be repopulated.
Database Caching
If you want to store the cache in the database then this is the best option. Here database is used to store cache data. To save cached data in the database you just want to create a table in the database by going to the settings.py file.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'cache_table',
}
}
The location specifies the table in which you want to store the cache. To create the table cache_table specified abouve and run the following command.
python manage.py createcachetable
Filesystem Caching
File system caching involves saving the cached data as a separate file. Unlike other type of caching, it requires lots of configurations. In the configurations, we need to specify the path to the directory to be used to store the cache. Now add the following configurations in your settings.py file.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': '/path/to/django_cache,
}
}
Local Memory Caching
Local memory caching is the default cache used by Django if no other caching is configured. While it is almost as fast as Memcached, it cannot scale beyond a single server. It is not suitable for use as a data cache for an application that uses more than one Web server. Therefore it is best suited for local development and testing environment.
Add the following cache configurations to set up local memory caching in the settings.py file.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'ecom',
}
}
The cache location is used to identify individual memory stores.