Django framework 7: configuring caching

1, Django cache Description: In the dynamic website, the ...

1, Django cache Description:

In the dynamic website, the data of the website visited by users changes in real time according to the content of the database.

In this way, there will be a problem: frequent refresh of the client will affect the performance of the server's database.

Using the cache, you can put some data that does not need to be refreshed in real time without accessing the database for a period of time.

Official introduction: https://docs.djangoproject.com/en/3.0/topics/cache/

2, According to the cache engine classification, Django can be divided into six types

Development debugging, memory cache, file cache, database cache, Memcache cache

         settings.py The following configuration is made in

#-----------1. Develop debug cache (no cache actually generated)-------------- CACHES={ 'default':{ 'BACKEND':'django.core.cache.backends.dummy.DummyCache', #The test engine used will not generate any cache 'TIMEOUT':300, # Common term #Cache lifetime 'OPTIONS':{ #Common, any type of cache can be configured 'MAX_ENTRIES': 300, #Maximum number of cache entries 'CULL_FREQUENCY': 3, #The default value is 3. When the maximum cache is reached, delete the current cache number, 1/CULL_FREQUENCY' 'KEY_PREFIX': '', #Prefix of cache Key 'VERSION': 1, #Cache Key version 'KEY_FUNCTION' : 'Function name' #The function to generate key defines how to combine the key and version into the final key. The default function will generate (prefix: Version: key) }, }, } #-----------2. Memory cache-------------- #Cache uses the least recently used (LRU) culling strategy. CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', #Memory cache engine 'LOCATION': 'unique-snowflake', #Name the memory location. When only one cache is set, it can be saved. If more than one memory cache is set, the name must be set and different from each other 'TIMEOUT':300, 'OPTIONS':{ 'MAX_ENTRIES': 300, 'CULL_FREQUENCY': 3, }, }, } #-----------3. File cache-------------- CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', #File caching engine 'LOCATION': '/var/tmp/django_cache', #Path # 'LOCATION': 'c:/foo/bar', #Windows path 'TIMEOUT':300, 'OPTIONS':{ 'MAX_ENTRIES': 300, 'CULL_FREQUENCY': 3, }, }, } #-----------4. Database cache-------------- #Before using, you need to use python manage.py Createcache table create a cache database table. The table name is the name of LOCATION #When adding a multi database cache, python manage.py Createcache table, will not operate on the existing table, and will automatically add a new table #For multiple databases, you can add cache routes by referring to the official CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.db.DatabaseCache', #Database caching engine 'LOCATION': 'my_cache_table', } } #-----------3-1. Multi database cache-------------- CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.db.DatabaseCache', #Database caching engine 'LOCATION': 'my_cache_table1', } 'default_2': { 'BACKEND': 'django.core.cache.backends.db.DatabaseCache', 'LOCATION': 'my_cache_table2', } } #-----------5. Memcache cache-------------- CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': '127.0.0.1:11211', #Memchache's ip address # 'LOCATION': 'unix:/tmp/memcached.sock', #[Unix:] is fixed, followed by the path of Unix socket file 'OPTIONS': { 'server_max_value_length': 1024 * 1024 * 2, #Configure the maximum cache space, 1024bit (1K) * 1024*2=2M } } } #-----------6. Pylibmc cache-------------- CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache', #Pylibmc engine 'LOCATION': '127.0.0.1:11211', 'OPTIONS': { 'binary': True, 'username': 'user', 'password': 'pass', 'behaviors': { 'ketama': True, } } } } #-----------7. Custom cache-------------- #Importing engine configuration files is not recommended by Django, because the engine with Django has been tested CACHES = { 'default': { 'BACKEND': 'path.to.backend', #Custom engine } }

2 June 2020, 11:06 | Views: 9193

Add new comment

For adding a comment, please log in
or create account

0 comments