Quick Contact

    Python Tutorial
    Python Panda Tutorial
    Python Selenium
    Python Flask Tutorial
    Python Django
    Numpy
    Tensorflow
    Interview Questions & Answers

    Django Caching

    In today’s era of web development, every user wants their requests on the website to be responded ASAP. There are certain methods which will make your website faster. Faster the website, greater the users it will attract.

    What are Dynamic Websites?

    Dynamic websites are a collection of webpages which are generated dynamically.

    Simple! So, why is it a big deal? because that generation of webpages means every page requires some processing. Servers aren’t cheap and so is their CPU time. Their limitation is that they need more CPU time to generate those webpages. More processing means more time added in response time, which makes the response of website slow. It is slow enough that users get annoyed.

    Dynamic websites generate highly personalized webpages. They come with tons of features. Some examples of websites are Spotify, Google, Facebook, Instagram, etc.

    A very good example can be Google Feed. The personal news feed on google app is generated dynamically. Its contents change in every 2-3 hrs. The updating process takes time if you refresh.

    Most of the websites on the Internet are becoming more and more dynamic and that makes it necessary for web developers to create faster websites. Caching comes in there. It provides a cost-effective and working solution. The more processing time a webpage takes, the more caching can improve its performance.

    There are other solutions as well:

    • Installing better server hardware
    • Installing the better server software
    • Improving database
    • Writing efficient code, etc.

    All these solutions require time and some resource. But, caching doesn’t require that much time to implement. Just learn from this tutorial and implement it easily.

    What is Caching?

    Caching is the process of storing recently generated results in memory. Those results can then be used in future when requested again.

    The computer memory works in a similar manner. CPU stores some files in the cache Memory. And, when CPU needs them again it looks for those files in Cache Memory first. Since Cache Memory is fast, the processing time improves. We will be implementing something similar on our server.

    Caching on Server

    Suppose we have a dynamic website. When a user request for some page:

    1. A Dynamic Page generates

      The requested dynamic page generates. Since it is generating for the first time, it will take a little bit longer. The CPU will process the page and that time will add to the response time.

    2. A webpage serves and gets stored in Cache Space

      Now, before serving this page to the client, it stores in Cache Space. Cache Space is a location where you temporarily store data to be retrieved for later use. Then it will serve to the client.

    3. Multiple users request for the same page

      Maybe, there are other clients requesting the same page. So, instead of generating webpage again we use the resource in Cache Space.

    4. Server responses with Cached Webpage

      Since we have that same resource stored in our Cache Space, our server will respond to these users with that webpage. The response time will decrease by the processing time. This makes the response much faster.

    5. Deletion of data in Cache Space

      Cache Space store copies of data. To keep the redundancy of data minimum, we delete the Cache Data after some time. This makes room for new Caching Data and the cycle goes on.

    Some important points:

    • CPU time of Webpage

      We discussed about dynamic websites in the previous section that caching subtracts processing time from response time. So, if our website serves static pages, there may not be any improvement. Therefore, caching is mostly used for dynamic webpages.

    • Memory type used for Cache Space

      Caching space is also a factor to improve speed. There can be three types of Cache Spaces:

    1. Main Memory
    2. Database Tables
    3. File-System or local directories

    As we can see, the main memory caching is the fastest of all. Faster the memory, better is the response time.

    • Expiry time of cached data

      Cached data should exist for a certain period of time. If the data gets deleted too early, there will be no utilization of caching. Also, if the data doesn’t get deleted, then it will waste cache memory.

      It depends on the type of resource and its demand by the users.

    Caching in Django

    Caching is important for a dynamic website. Since Django is a dynamic framework, it comes with built-in options for you to manage your cache.

    Django allows caching on different caching spaces as well as on different parts of a website. There can be certain parts of the website which can demand more CPU time and granularity to implement caching on them individually. This makes caching efficient.

    We learned about different caching spaces in the previous section. Now, let’s implement caching on all of them.

    Setting-up Caching System

    We will be setting up different caching frameworks or spaces. Your system may only need one so implement one, which fits your requirements with given constraints.

    What is Memcached Framework?

    Memcached is a memory-based cache framework. This software takes a certain part of the main memory and converts it to Cache Space. It handles that efficiently so the main memory is never completely full.

    When a request comes to the server, it will first look in Cache Space. Memcached provides faster response since the main memory is fastest of all. If the resource is not present in Cache Space, the server generates that resource. That is again stored by Memcached, if the developer has cached it.

    The Memcached framework can work on multiple servers. We can change the LOCATION key and it can have different values, like the IP addresses of other servers.

    When Memcached is expanded to multiple servers, it considers all of them as a single server. This approach reduces data-redundancy to a certain level. This is also one of the reasons Memcached Caching is preferred over other types of Caching.

    The Memcached framework is one of the most popular choices for caching. It is used by almost all the servers. It can be difficult to implement on Windows/ Mac as it is designed for Debian/ Ubuntu Operating System, which is server OS.

    To use Memcached framework in Django:

    1. Install Memcached framework on your system
    2. Install Python connectors for Memcached Framework
    3. Edit our Settings.py file

    As mentioned earlier, installing Memcached framework on Windows/ Mac OS is difficult. If it is installed then proceed. And, if you have Debian/Ubuntu OS, then there are no issues.

     

    Apply now for Advanced Python Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.