CACHE_BACKEND parameter and site-level Cache settings in Django

Source: Internet
Author: User
This article mainly introduces the CACHE_BACKEND parameter and site-level Cache settings in Django. Python is the most popular Pythonweb framework. For more information, see CACHE_BACKEND Parameter

Parameters may be used for each cache backend. They are given as query strings in CACHE_BACKEND settings. Valid parameters are as follows:

  • Timeout: the cache expiration time, in seconds. This parameter is set to 300 seconds (five minutes) by default ).
  • Max_entries: Maximum number of entries allowed by the cache for the memory, file system, and database backend. If this number is exceeded, the old value is deleted. The default value is 300.
  • Cull_percentage: the ratio of entries to be deleted when max_entries is reached. The actual ratio is 1/cull_percentage, so setting cull_frequency = 2 is to remove half of the cache when max_entries is reached.

Setting the value of cull_frequency to 0 means that when max_entries is reached, the cache will be cleared. This will greatly increase the access speed at the cost of a lot of cache loss.

In this example, timeout is set to 60.

CACHE_BACKEND = "memcached://127.0.0.1:11211/?timeout=60"

In this example, set timeout to 30 and max_entries to 400:

CACHE_BACKEND = "locmem:///?timeout=30&max_entries=400"

Invalid parameters and invalid parameter values are ignored.
Site-level Cache

Once the cache is set, the easiest way is to cache the entire website. You need to add 'django. middleware. cache. UpdateCacheMiddleware 'and 'django. middleware. cache. FetchFromCacheMiddleware' To Your MIDDLEWARE_CLASSES settings. In this example:

MIDDLEWARE_CLASSES = ( 'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.cache.FetchFromCacheMiddleware',)

Note:

No, there is no typographical error here: the modified middleware must be placed at the beginning of the list, while the fectch middleware must be placed at the end. For details, see the MIDDLEWARE_CLASSES sequence below.

Then, add the following settings to your Django settings file:

  • CACHE_MIDDLEWARE_SECONDS: the number of seconds that each page should be cached.
  • CACHE_MIDDLEWARE_KEY_PREFIX: If the cache is shared by multiple websites installed with the same Django, set this value to the current website name or a unique string that can represent the Django instance, to avoid key conflicts. If you don't care about it, you can set it to a null string.

Cache middleware caches every page without the GET or POST parameters. Alternatively, if CACHE_MIDDLEWARE_ANONYMOUS_ONLY is set to True, only anonymous requests (that is, not login users) will be cached. To cancel the user-specific pages (such as the Djangos Management Interface) cache, this is a simple and effective method. CACHE_MIDDLEWARE_ANONYMOUS_ONLY. Make sure that you have started AuthenticationMiddleware.

In addition, cache middleware automatically sets several header information for each HttpResponse:

  • When a new (Uncached) page is requested, set the Last-Modified header to the current date/time.
  • Set the Expires header to the current date/time plus the defined CACHE_MIDDLEWARE_SECONDS.
  • Set the Cache-Control header to give the page the longest validity period. The value is from the CACHE_MIDDLEWARE_SECONDS setting.

If the View sets its Cache expiration time (that is, it has a maximum age in the Cache-Control of the header information), the page will be cached until it expires, instead of CACHE_MIDDLEWARE_SECONDS. With the django. views. decorators. cache decorator, you can easily set the view expiration time (using the cache_control decorator) or disable the cache view (using the never_cache decorator ).

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.