Django Cache System

Source: Internet
Author: User
Tags install django pip install django

Django Cache System

On a dynamic website, each time a user requests a page, the server performs the following operations: query the database, render the template, execute the business logic, and finally generate a page that can be viewed by the user.

This will consume a lot of resources. When the number of users is very large, you need to consider this issue.

Cache is used to prevent repeated computation. It saves the results that consume a large amount of resources and does not need to be computed again next time. Cache logic:

given a URL, try finding that page in the cacheif the page is in the cache:    return the cached pageelse:    generate the page    save the generated page in the cache (for next time)    return the generated page

Django provides caches of different granularities: You can cache a page or only a part of the page that is difficult to compute or consumes resources, or directly cache the entire website.

Django can also work with some "downstream" caches, such as Squid and browser-based caches. You do not directly control these types of caches, however, you can provide them with information on what part of the site should be cached and how to be cached (via HTTP headers ).

Set Cache

Set cache in CACHES in settings. Below are several available cache options:

Memcached

Django currently supports the fastest and most effective Caching System. To use Memcached, You need to download the Memcached support library, which is generally python-memcached or pylibmc.

Set BACKEND to django. core. cache. backends. memcached. MemcachedCache (when python-memcached is used) or django. core. cache. backends. memcached. PyLibMCCache (when pylibmc is used ).

Set LOCATION to ip: port or unix: path. For example:

CACHES = {    'default': {        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',        'LOCATION': '127.0.0.1:11211',    }}

Or

CACHES = {    'default': {        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',        'LOCATION': 'unix:/tmp/memcached.sock',    }}

When using pylibmc, remove the unix:/Prefix:

CACHES = {    'default': {        'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',        'LOCATION': '/tmp/memcached.sock',    }}

You can also run the Memcached process on multiple machines. The program will treat these machines as a separate cache without copying the cache value on each machine:

CACHES = {    'default': {        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',        'LOCATION': [            '172.19.26.240:11211',            '172.19.26.242:11212',            '172.19.26.244:11213',        ]    }}

Because Memcached is a memory-based Cache, data is only stored in the memory. If the server crashes, data will be lost. Therefore, do not use the memory cache as the only data storage method.

Database caching

Django can also store cached data in the database.

CACHES = {    'default': {        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',        'LOCATION': 'my_cache_table',    }}

LOCATION is the name of the table in the database. It can be used without being used in the database.

Create a cache table:

python manage.py createcachetable

When using multiple databases, you also need to write a Router for the cache table:

class CacheRouter(object):    """A router to control all database cache operations"""    def db_for_read(self, model, **hints):        "All cache read operations go to the replica"        if model._meta.app_label == 'django_cache':            return 'cache_replica'        return None    def db_for_write(self, model, **hints):        "All cache write operations go to primary"        if model._meta.app_label == 'django_cache':            return 'cache_primary'        return None    def allow_migrate(self, db, app_label, model_name=None, **hints):        "Only install the cache model on primary"        if app_label == 'django_cache':            return db == 'cache_primary'        return None
Filesystem caching

You can also use files to store cached data.

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

LOCATION is the cache data storage directory.

Windows:

CACHES = {    'default': {        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',        'LOCATION': 'c:/foo/bar',    }}
Local-memory caching

Django uses the cache system by default. data is stored in local memory:

CACHES = {    'default': {        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',        'LOCATION': 'unique-snowflake',    }}
Dummy caching (for development)

Used during development:

CACHES = {    'default': {        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',    }}
Using a custom cache backend

You can also use other cache systems, such as Redis and django-redis at https://github.com/niwinz/django-redis.

Download: pip install django-redis

Settings:

CACHES = {    "default": {        "BACKEND": "django_redis.cache.RedisCache",        "LOCATION": "redis://127.0.0.1:6379/1",        "OPTIONS": {            "CLIENT_CLASS": "django_redis.client.DefaultClient",        }    }}
Cache arguments

The CACHES settings have several additional parameters:

TIMEOUT: Cache timeout. The default value is 300 s. It can be set to None, that is, never timeout.

OPTIONS:Locmem, filesystem, and database cache systems with their own removal policies have the following parameters:

MAX_ENTRIES:The maximum number of entries in the cache. If the number is greater than this value, the old entries will be deleted. The default value is 300.

CULL_FREQUENCY:When MAX_ENTRIES is reached, the access rate is accepted. The actual ratio is 1/cull_frequency. Therefore, if set to 2, half of the cache is removed when max_entries is reached. If set to 0, the cache is cleared when max_entries is reached. The default value is 3.

KEY_PREFIX:A string that is automatically included in the cache key value.

VERSION:The version number used when the cache key value is generated.

KEY_FUNCTION:The method used to generate the key value.

Cache website

To Cache the entire website, first add two middleware:

MIDDLEWARE = [    'django.middleware.cache.UpdateCacheMiddleware',    'django.middleware.common.CommonMiddleware',    'django.middleware.cache.FetchFromCacheMiddleware',]

Note that the update middleware should be placed first, and the fetch middleware should be placed at the end.

Add the following values to settings:

CACHE_MIDDLEWARE_ALIAS: cache alias for storage

CACHE_MIDDLEWARE_SECONDS: time when the page is cached

CACHE_MIDDLEWARE_KEY_PREFIX: When the cache is used by different sites, it is used to prevent the cache key value conflict. It is generally set to the site name.

The FetchFromCacheMiddleware middleware is used to cache responses with a 200 status code obtained through the GET and HEAD methods. A url with different query strings is cached as different pages.

UpdateCacheMiddleware middleware sets several headers in response to HttpResponse:

Set Last-Modified to the latest page refresh time, set Expires to the expiration time (add CACHE_MIDDLEWARE_SECONDS to the current time), and set the maximum validity period of the Cache-Control page (CACHE_MIDDLEWARE_SECONDS ).

You can also set the expiration time for the views logical function:

Use django. views. decorators. cache. cache_control () to set the cache expiration time. Use django. views. decorators. cache. never_cache () to disable caching.

Cache page

Use django. views. decorators. cache. cache_page () to cache a page:

from django.views.decorators.cache import cache_page@cache_page(60 * 15)def my_view(request):    ...

60*15 indicates that the cache lasts for 15 minutes.

The cache parameter can be set to which cache System in CACHES is used. The default value is default:

@cache_page(60 * 15, cache="special_cache")def my_view(request):    ...

The key_prefix parameter works the same as the CACHE_MIDDLEWARE_KEY_PREFIX parameter:

@cache_page(60 * 15, key_prefix="site1")def my_view(request):    ...

You can use this method in the url:

from django.views.decorators.cache import cache_pageurlpatterns = [    url(r'^foo/([0-9]{1,2})/$', cache_page(60 * 15)(my_view)),]
Template fragment Cache
{% load cache %}{% cache 500 sidebar %}    .. sidebar ..{% endcache %}

The {% cache %} template tag caches the block content, including at least two parameters: the cache time and the name of the cache segment.

You can cache different copies for a segment based on the changed dynamic data:

{% load cache %}{% cache 500 sidebar request.user.username %}    .. sidebar for logged in user ..{% endcache %}
CACHE API

Obtain the cache system based on the cache alias in the CACHES settings:

>>> from django.core.cache import caches>>> cache1 = caches['myalias']>>> cache2 = caches['myalias']>>> cache1 is cache2True

Obtain the default cache:

>>> from django.core.cache import cache

Basic usage: set (key, value, timeout) and get (key )::

>>> cache.set('my_key', 'hello, world!', 30)>>> cache.get('my_key')'hello, world!'

Key is a string and value is a python object of picklable. When the value of timeout is set to None, the cache will never expire. If the value is set to 0, the cache will not be cached.

 

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.