Introduction to Django Cache
When a page is requested in a web application several processes should be performed in the backend to bring the page to the requester. The process could be of hitting database queries, calling templates, performing several calculations, etc. performing these operations from scratch for every page call may become expensive for sites with heavy traffic. One way to tackle this is by setting a cache system. The cache system will save the outcome of an expensive operation so that the calculation can be omitted next instance.
Types of Django cache
As far as Django is concerned Django is classified into 5 major types, these five major types are the key classifications in the cache process of Django. Each of these types has its own way of caching the data. On the other hand, the intention is to store the data well and make cognitive processing. The various types of cache are discussed below:
- Memcached
- Filesystem caching
- Local memory cache
- Dummy cache
- Database cache
How did the cache work?
The process of caching can be performed in several ways, Through a database, memory, filesystem, etc. each method could be more efficient than the other. The various methods of caching are explained below,
1. Memcahed
Memcached is a daemon server that will do the process of caching. The Memcached will be running as a daemon with memory RAM allocated for it. There are several Memory cache’s in the market. Some of the popular ones are listed here,
1) pylibmc
2) pymemcache
3) memcachedcache
The below example shows the Django cache system based on memcachedcache,
1) Download the Memcached software and extract the software to a desktop location.
2) To view the various options of Memcached below command can be used. Ensure to shift to the Memcached directory before executing the command to view the options.
Cd C:\Users\ANAND\Downloads\memcached-win32-1.4.4-14
.\memcached.exe -h
3) Set the Memcached active using its commands, one sample command is given below.
.\memcached.exe -m 512 -vvv
slab class 32: chunk size 96256 perslab 10
slab class 33: chunk size 120320 perslab 8
slab class 34: chunk size 150400 perslab 6
slab class 35: chunk size 188000 perslab 5
slab class 36: chunk size 235000 perslab 4
slab class 37: chunk size 293752 perslab 3
slab class 38: chunk size 367192 perslab 2
slab class 39: chunk size 458992 perslab 2
slab class 40: chunk size 573744 perslab 1
slab class 41: chunk size 717184 perslab 1
slab class 42: chunk size 1048576 perslab 1
<624 server listening (auto-negotiate)
<652 server listening (auto-negotiate)
<656 send buffer was 65536, now 268435456
<656 server listening (udp)
<656 server listening (udp)
<656 server listening (udp)
<656 server listening (udp)
<660 send buffer was 65536, now 268435456
<660 server listening (udp)
<660 server listening (udp)
<660 server listening (udp)
<660 server listening (udp)
4) Install the Memcached client in Python
pip install python-memcached
6) In the Django project set cache values in the settings.py file,
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
Output:
2. Filesystem caching
In file system caching the cache files will be cached in a file/folder location. To set a file system cache the below are the steps,
1) Make the below changes in the settings.py file, in the below change the location mentions the position where the changes are intended to be stored.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': 'D:\sample',
}
}
Here ensure the location is absolute, Since the location needs to be absolute it must be starting from a root directory.
2) Add the highlighted components to the middleware in the same order.
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
3) Now accessing the website will trigger the cache files to be cached in the mentioned location.
Output:
3. Local-Memorycache
The local memory cache is the default cache service used by Django when no further cache services are explicitly defined.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'test',
}
}
Here the location mentions the memory stores. It can be mentioned only when more than one memory store are used.
4. Dummy cache
If the web application doesn’t want any cache in the backend, then dummy caching can be initiated. The dummy cache will not cache anything instead the cache interface will be initiated without doing anything.
CACHES = { 'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
} }
5. Database cache
The database cache is a process where the cache values are captured in the database. Here the database cache class has to be referred then the table targeted is expected to be mentioned. First, the database cache class is placed in the Django library inside the core library and within the cache library at the backends section in db and in the database cache. From there on the table has to be mentioned in the location section of the cache services.
CACHES = { 'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'cache_table',
} }
In the above code example the BACKEND value is set to the database-based library ‘django.core.cache.backends.db.DatabaseCache’, whereas the location value is set to the table for which the value is expected to be stored. The cache table needs to be created in the database. The cache table can be most probably created using the python manage.py cachetable command. The above command itself will create the table expected. More interestingly more than one table can be used for storing the cache. This means that at one instance of time more than one table can be used for cache services. This is another example of how the database cache works in Django. The database-level caching process is very similar to caching of the services in file and memory allocation levels.
Conclusion
The given article deals with how the values have to be handled in Django. It also explains on what are the various types of cache services associated with Django. All these types of Django services are compared and expressed with suitable examples along with their operation.