Cache of Django

Cache of Django
Techiio-author
Written by Sagar RabidasDecember 9, 2021
10 min read
Django
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss the cache of Django.

Django:-

Django is a free and open-source web application framework, written in Python. A web framework is a set of components that helps you to develop websites faster and easier.

When you're building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc.

Luckily for you, other people long ago noticed that web developers face similar problems when building a new site, so they teamed up and created frameworks (Django being one of them) that give you ready-made components to use.

Frameworks exist to save you from having to reinvent the wheel and to help alleviate some of the overhead when you’re building a new site.

blogpost

Introduction to Django Cache

Whilst a page is requested in web software several processes should be performed inside the backend to convey the page to the requester. The system may be of hitting database queries, calling templates, acting several calculations, and so forth. Acting these operations from scratch for each web page name may emerge as highly-priced for sites with heavy site visitors. One way to tackle that is by putting cache machine. The cache system will shop the outcome of a luxurious operation so that the calculation can be neglected subsequent example.

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 its 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,

Memcached: 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 caches in the market. Some among the popular ones are listed here,

  1. pyloric
  2. pymemcache
  3. Memcached cache

The below example shows Django cache system based on Memcached cache,

  • Download the Memcached software and extract the software to a desktop location.
  • 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

  • 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)

  • Install the Memcached client in python
pip install python-memcached
  • 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',
}
}

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,

  • 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.
  • 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',
]

  • Now accessing the website will trigger the cache files to be cached in the mentioned location.

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

django
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Sagar Rabidas
Software Developer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+8 more
300 Blog Posts
14 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance