How to Cache Website using Django — Python ?

Sahil Soni
Analytics Vidhya
Published in
5 min readSep 8, 2020

Cache

A Cache is an information technology for the temporary storage (caching) of Web documents, such as Web pages, images, and other types of Web multimedia, to reduce server lag.

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

Django comes with a robust cache system that lets you save dynamic pages so they don’t have to be calculated for each request.

You can cache the output of specific views, you can cache only the pieces that are difficult to produce, or you can cache your entire site.

Following are the options of caching:-

•Database Caching

•File System Caching

  • Local Memory Caching

How to implement Caching

Following are types of caching :

  • The per-site cache — Once the cache is set up, the simplest way to use caching is to cache your entire site.

•The per-view cache — A more granular way to use the caching framework is by caching the output of individual views.

  • Template fragment caching — This gives you more control what to cache. And only caches a particular part of HTML Template.
  • Low Level Caching — This gives you control to cache only a small data structure like list, dictionary etc. which can be pickled/serialised.

In this blog we would discuss caching an entire website(And thus consider it as part 1 of Caching Series).

The per-site cache

Once the cache is set up, the simplest way to use caching is to cache your entire site.

Following are steps to be followed:

  1. Go to settings.py file of django project and add following Middle-wares. Please note , that order of Middle-wares should be maintained.(Mandatory)

MIDDLEWARE = [

‘django.middleware.cache.UpdateCacheMiddleware’,

‘django.middleware.common.CommonMiddleware’,

‘django.middleware.cache.FetchFromCacheMiddleware’,

]

Following are some of variables which can be added in settings.py file (Optional)

CACHE_MIDDLEWARE_ALIAS — The cache alias to use for storage.

CACHE_MIDDLEWARE_SECONDS — The number of seconds each page should be cached.

CACHE_MIDDLEWARE_KEY_PREFIX — If the cache is shared across multiple sites using the same Django installation, set this to the name of the site, or some other string that is unique to this Django instance, to prevent key collisions. Use an empty string if you don’t care.

Types of Cache System :

As mentioned earlier , we can store cache data in three types of system:

  • Database Caching — Data is stored in RDBMS table
  • File System Caching-Data is stored in File
  • Local Memory Caching-Data is stored in local memory of server/computer

Database Caching

Django can store its cached data in your database. This works best if you’ve got a fast, well-indexed database server.

Add following dictionary in settings.py file of Django Project

CACHES = {

‘default’: {

‘BACKEND’: ‘django.core.cache.backends.db.DatabaseCache’,

‘LOCATION’: ‘my_cache_table’,

}

}

# my_cache_table : represents name of table created in database.

Before using the database cache, you must create the cache table with this command:

python manage.py createcachetable

This creates a table in your database that is in the proper format that Django’s database-cache system expects. The name of the table is taken from LOCATION i.e. LOCATION key of dictionary.

If you are using multiple database caches, createcachetable creates one table for each cache.

Cache Arguments

Each cache backend can be given additional arguments to control caching behavior.

TIMEOUT: The default timeout, in seconds, to use for the cache. This argument defaults to 300 seconds (5 minutes). You can set TIMEOUT to None so that, by default, cache keys never expire. A value of 0 causes keys to immediately expire (effectively “don’t cache”).

OPTIONS: Any options that should be passed to the cache backend. The list of valid options will vary with each backend, and cache backends backed by a third-party library will pass their options directly to the underlying cache library.

Cache backends that implement their own culling strategy (i.e., the locmem, filesystem and database backends) will honor the following options:

MAX_ENTRIES: The maximum number of entries allowed in the cache before old values are deleted. This argument defaults to 300.

CULL_FREQUENCY: The fraction of entries that are culled when MAX_ENTRIES is reached. The actual ratio is 1 / CULL_FREQUENCY, so set CULL_FREQUENCY to 2 to cull half the entries when MAX_ENTRIES is reached. This argument should be an integer and defaults to 3.

A value of 0 for CULL_FREQUENCY means that the entire cache will be dumped when MAX_ENTRIES is reached. On some backends (database in particular) this makes culling much faster at the expense of more cache misses.

CACHES = {

‘default’: {

‘BACKEND’: ‘django.core.cache.backends.db.DatabaseCache’,

‘LOCATION’: ‘enroll_cache’,

‘TIMEOUT’: 60,

‘OPTIONS’: {

‘MAX_ENTRIES’: 1000

}

}

}

By adding above mentioned steps , we will can observe two things:

  1. A cache table is created in database with name mentioned in LOCATION
  2. New rows are inserted whenever a user open our website.
  3. If the user open same website within the timeout period, user will get same view even though the content of real website is changed.
Cache Table

Table based Cache system is fastest and more organised.

Note: These caches keeps all the records in memory even after cache is expired or timeout . Do add a system where old caches can be deleted.

Similar steps can be followed for File Based cache and Local Memory based cache. Please follow following steps(Optional):

Filesystem Caching

The file-based backend serializes and stores each cache value as a separate file.

For File based system too we need to add middlewares as done in above steps and then add following dictionary in settings.py file

CACHES = {

‘default’: {

‘BACKEND’: ‘django.core.cache.backends.filebased.FileBasedCache’,

‘LOCATION’: ‘c:/Django code/gs80’,

}

}

Make sure the directory pointed-to by this setting exists and is readable and writable by the system user under which your Web server runs.

CACHES = {

‘default’: {

‘BACKEND’: ‘django.core.cache.backends.filebased.FileBasedCache’,

‘LOCATION’: ‘/var/tmp/django_cache’,

}

}

Local Memory Caching

This is the default cache if another is not specified in your settings file. This cache is per-process and thread-safe.

Each process will have its own private cache instance, which means no cross-process caching is possible. This obviously also means the local memory cache isn’t particularly memory-efficient.

It’s probably not a good choice for production environments. It’s nice for development.

Add following dictionary in setting.py file of Django project after adding Middlewares as mentioned in above steps.

CACHES = {

‘default’: {

‘BACKEND’: ‘django.core.cache.backends.locmem.LocMemCache’,

‘LOCATION’: ‘unique-snowflake’,

}

}

This concludes part 1 of caching in Django i.e. Cache entire website.

Please give claps and comment if you need a git hub source code for this concept.

See you soon , with per view caching, Template fragment caching and Low level data structure caching in next blogs.

--

--