The django development tutorial uses cached files to cache pages.

Source: Internet
Author: User
Tags http 200

The django development tutorial uses cached files to cache pages.

Preface

Since Django is a dynamic website, each request will perform corresponding operations on the Data. When the access volume of the program is large, the time consumption will inevitably become more obvious. The simplest solution is to use: cache, the cache saves the return value of a view to the memory or Redis. When someone accesses the view within five minutes, the operation in the view will not be executed, instead, get the cached content directly from the memory or Redis and return it.

First, let's take a look at the browser cache.

Browser cache mechanism

The Cache-control policy, Cache-Control, and Expires, are used to specify the validity period of the current resource and determine whether the browser directly caches data from the browser or resends the request to the server for data. However, Cache-Control has more options and more detailed settings. If both settings are set, the priority is higher than Expires.

Or the above request, the value of the Cache-Control header returned by the web server ismax-age=300(5 minutes ).

Last-Modified/If-Modified-SinceLast-Modified/If-Modified-Since must be used with Cache-Control. LLast-Modified: indicates the last modification time of the response resource. When the web server responds to the request, it informs the browser of the last modification time of the resource. LIf-Modified-Since: when the resource expires (max-age identified by Cache-Control), it is found that the resource has a Last-Modified declaration, if-Modified-Since is added to the request to the web server again, which indicates the request time. After receiving the request, the web server compares the header If-Modified-Since with the last modification time of the requested resource. If the last modification time is relatively new, it indicates that the resource has been modified, the system will respond to the content of the entire resource (written in the Response Message package), HTTP 200; if the last modification time is relatively old, if no new changes are made to the resource, the system responds to HTTP 304 (no package body is required, saving browsing) and notifies the browser to continue using the saved cache.

In actual development and application, we will use caching. In fact, we can also use caching in django development. Currently, django provides us with many caching methods. I can see as many as six, there may be others that are not traced. I use the File Cache. To put it bluntly, I put the cached data into the requesting computer, which also reduces the pressure on some servers, let's take a look at my configuration.

CACHES = { 'default': {  'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',  'LOCATION': '/var/tmp/django_cache', }}

This is because we use the cache file, so we have configured it. Let's take a look at our usage. First, we can cache it globally.

Middleware is used to perform a series of authentication and other operations. If the content exists in the cache, FetchFromCacheMiddleware is used to obtain the content and return it to the user. before returning it to the user, determine whether the cache already exists, if this parameter does not exist, UpdateCacheMiddleware saves the cache to implement full-site cache.

The settings are as follows:

MIDDLEWARE = ['django. middleware. cache. updateCacheMiddleware ', # Put It In The First middleware location # other middleware... 'django. middleware. cache. fetchFromCacheMiddleware ', # Put it to the last one] CACHE_MIDDLEWARE_ALIAS = "" CACHE_MIDDLEWARE_SECONDS = "" CACHE_MIDDLEWARE_KEY_PREFIX = ""

In addition, we cache individual views:

Method 1: add the decorator directly to the application

From django. views. decorators. cache import cache_page @ cache_page (60*15) def ceshi (request): posts = Article. objects. filter (tag _ name = u'test') post_list = fenye (request, posts = posts) return render (request, 'index.html ', {'Post _ list ': post_list ,})

In fact, I also use another method, that is, adding a url, because I am an object-oriented programming method, so I use the following.

url(r'^$', cache_page(60*2)(HomeView.as_view()), name='home'),

In this way, we configure and start our project, and then we can check whether our cache is effective. First, let's take a look.

As you can see, I made a two-minute cache on the login interface, so let's see if our file is effective.

This proves that our current cache is successful. In fact, we can also use redis to cache data.

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.