How it works...

Now, if you access the first viral video (such as at /en/viral-videos/1/) and refresh the page a few times, you will see that the number of impressions changes only once a minute. This is because each request is cached for 60 seconds for every user. We set caching for the view using the  @cache_page decorator.

Just like Memcached, Redis is a key-value store, and when used for caching it generates the key for each cached page based on the full URL. When two visitors access the same page simultaneously, the first visitor's request would receive the page generated by the Python code, and the second one would get the same HTML code but from the Redis server.

In our example, to ensure that each visitor gets treated separately even if they access the same URL, we are using the @vary_on_cookie decorator. This decorator checks the uniqueness of the Cookie header of the HTTP request.

Learn more about Django's cache framework from the official documentation at
https://docs.djangoproject.com/en/2.1/topics/cache/. Similarly, more details on Redis can be found at https://redis.io/.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.27.202