Django cache, django

Source: Internet
Author: User

Django cache, django

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 memcache. When someone accesses the view within five minutes, the operation in the view is not executed, instead, get the cached content directly from the memory or Redis and return it.

Django provides six caching methods:

  • Development and debugging
  • Memory
  • File
  • Database
  • Memcache cache (python-memcached module)
  • Memcache cache (pylibmc module)
1. Configuration

A. Development and debugging

# This is used to start debugging. No operation is performed internally. # configuration: CACHES = {'default': {'backend': 'django. core. cache. backends. dummy. dummyCache ', # Engine 'timeout': 300, # cache TIMEOUT time (300 by default, None indicates never expiration, 0 indicates immediate expiration) 'options': {'max _ entries ': 300, # maximum number of caches (300 by default) 'ull _ FREQUENCY ': 3, # After the cache reaches the maximum number, remove the proportion of the number of caches, that is: 1/CULL_FREQUENCY (default: 3)}, 'key _ prefix': '', # cache key prefix (default: NULL) 'version': 1, # cache key version (default: 1) 'key _ function' FUNCTION name # generate the key function (default FUNCTION will generate: [Prefix: Version: key ])}} # custom key def default_key_func (key, key_prefix, version): "Default function to generate keys. constructs the key used by all other methods. by default it prepends the 'key _ prefix '. KEY_FUNCTION can be used to specify an alternate function with custom key making behavior. "return '% s: % s' % (key_prefix, version, key) def get_key_func (key_func):" Function to decide which key function to use. ults to ''default _ key_func ''. "if key_func is not None: if callable (key_func): return key_func else: return import_string (key_func) return default_key_funcView Code

B. Memory

# This cache saves the content to the memory variable # configuration: CACHES = {'default': {'backend': 'django. core. cache. backends. locmem. locMemCache ', 'location': 'unique-snowflake', }}# Note: Other configurations are similar to the Development and debugging version copying code.View Code

C. Files

# This cache saves the content to the file # configuration: CACHES = {'default': {'backend': 'django. core. cache. backends. filebased. fileBasedCache ', 'location':'/var/tmp/django_cache ', }}# Note: Other configurations are in the same development and debugging version.View Code

D. Database

# This cache saves the content to the database # configuration: CACHES = {'default': {'backend': 'django. core. cache. backends. db. databaseCache ', 'location': 'My _ cache_table', # Database Table }}# Note: run the create table command python manage. py createcachetableView Code

E. Memcache cache (python-memcached module)

# This cache uses the python-memcached module to connect to memcache CACHES = {'default': {'backend': 'django. core. cache. backends. memcached. memcachedCache ', 'location': '2017. 0.0.1: 11211 ',} CACHES = {'default': {'backend': 'django. core. cache. backends. memcached. memcachedCache ', 'location': 'unix:/tmp/memcached. sock ',} CACHES = {'default': {'backend': 'django. core. cache. backends. memcached. memcachedCache ', 'location': ['123. 19.26.240: 11211 ', '2017. 19.26.242: 11211 ',]}View Code

F. Memcache cache (pylibmc module)

# This cache uses the pylibmc module to connect to memcache CACHES = {'default': {'backend': 'django. core. cache. backends. memcached. pyLibMCCache ', 'location': '2017. 0.0.1: 11211 ',} CACHES = {'default': {'backend': 'django. core. cache. backends. memcached. pyLibMCCache ', 'location':'/tmp/memcached. sock ',} CACHES = {'default': {'backend': 'django. core. cache. backends. memcached. pyLibMCCache ', 'location': ['2017. 19.26.240: 11211 ', '2017. 19.26.242: 11211 ',]}View Code 2. Application

A. Full site use

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 the cache, enabling the full-site cache MIDDLEWARE = ['django. middleware. cache. updateCacheMiddleware ', # other middleware... 'django. middleware. cache. fetchFromCacheMiddleware ',] CACHE_MIDDLEWARE_ALIAS = "" CACHE_MIDDLEWARE_SECONDS = "" CACHE_MIDDLEWARE_KEY_PREFIX = ""View Code

B. Separate View cache

Method 1: from django. views. decorators. cache import cache_page @ cache_page (60*15) def my_view (request ):... method 2: from django. views. decorators. cache import cache_page urlpatterns = [url (R' ^ foo/([0-9] {1, 2})/$ ', cache_page (60*15) (my_view),]View Code

C. Local view usage

A. Import TemplateTag {% load cache %} B. Use cache {% cache 5000 cache key %} cache content {% endcache %}View Code

More: click here

Local File Cache case:

 

 

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.