cache_backend Parameters
Parameters can be used for each cache backend. They are given as query strings in the Cache_backend settings. Valid parameters are as follows:
- Timeout: The expiration time, in seconds, for the cache. This parameter is set to 300 seconds (five minutes) by default.
- Max_entries: For memory, file system, and database backend, the maximum number of entries allowed by the cache exceeds this number before the old value is deleted. This parameter is 300 by default.
- Cull_percentage: The percentage of entries that are deleted when the max_entries is reached. The actual ratio is 1/cull_percentage, so setting up cull_frequency=2 is to remove half of the cache when the max_entries is reached.
Setting the value of Cull_frequency to 0 means that when max_entries is reached, the cache is emptied. This will greatly increase the speed of receiving access at the expense of many cache misses.
In this example, timeout is set to 60
Cache_backend = "Memcached://127.0.0.1:11211/?timeout=60"
In this example, timeout is set to 30 and Max_entries is 400:
Cache_backend = "locmem:///?timeout=30&max_entries=400"
The illegal parameters and the illegal parameter values are ignored.
Site-level Cache
Once the cache is set up, the simplest method is to use the cache to cache the entire site. You need to add ' django.middleware.cache.UpdateCacheMiddleware ' and ' Django.middleware.cache.FetchFromCacheMiddleware ' To your middleware_classes settings, in this case:
middleware_classes = (' Django.middleware.cache.UpdateCacheMiddleware ', ' Django.middleware.common.CommonMiddleware ', ' django.middleware.cache.FetchFromCacheMiddleware ',)
Attention:
No, there is no typographical error: the modified middleware must be placed at the beginning of the list, and the Fectch middleware must be placed at the end. The details are a little confusing, if you want to know the full insider, please see the middleware_classes order below.
Then, add the following required settings to your Django settings file:
- Cache_middleware_seconds: The number of seconds each page should be cached.
- Cache_middleware_key_prefix: If the cache is shared by multiple Web sites using the same Django installation, set the value to the current site name, or another unique string representing the Django instance, to avoid KEY collisions. If you don't care, you can set an empty string.
The cache middleware caches each page without a GET or post parameter. Or, if Cache_middleware_anonymous_only is set to True, only anonymous requests (that is, users that are not logged on) will be cached. This is a simple and effective way to cancel the cache of user-related pages (user-specific pages), such as the Djangos management interface. Cache_middleware_anonymous_only, you should make sure that you have started authenticationmiddleware.
In addition, the cache middleware automatically sets several header information for each httpresponse:
- When a new (not cached) version of the page is requested, the Last-modified header is set to the current date/time.
- Sets the expires header to the current date/time plus the defined cache_middleware_seconds.
- Set the Cache-control header to give the page a maximum validity period, with values from the Cache_middleware_seconds setting.
If the view sets its own cache expiration time (that is, it has a maximum age in the Cache-control of the header information), the page is cached until it expires, not cache_middleware_seconds. With the Django.views.decorators.cache adorner, you can easily set the expiry time of the view (using the Cache_control adorner) or disable the cached view (using the Never_cache adorner).