Good Place. Good Things. Good Time.

Django Cache

Django Cache

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 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 the 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 their 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 cache work?

The process of caching can be performed in several ways, Through a database, memory, or filesystem, etc. each method could be efficient than the other. The various methods of caching are explained below,

Memcahed: Memcached is a daemon server that will do the process of caching. The Memcached will be running as a daemon with a memory RAM allocated for it. There are several Memory cache’s in the market. Some among the popular ones are listed here,

1) pylibmc
2) pymemcache
3) memcachedcache

The below example shows 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:UsersANANDDownloadsmemcached-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 Django project set cache values in settings.py file,

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

Output:

Django 1

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 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:

Django 2

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.

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',
} }

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.

Recommended Articles

This is a guide to Django Cache. Here we discuss the Introduction, How cache works, and examples with code implementation respectively. You may also have a look at the following articles to learn more –

  1. Django OneToOneField
  2. Django on_delete
  3. Django DateTimeField
  4. Django GROUP BY

The post Django Cache appeared first on EDUCBA.

Read original article here: Django Cache

Read original article here: Django Cache



Disclaimers and Denial of responsibility..!

Denial of responsibility! TheTopMag.com is an automatic aggregator of all media around the world. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – jamesonline999@gmail.com . The content will be deleted within 72 hours.
You might also like
Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.