Cache
Django
DynamicWeb
background frame, generated in real time page accessed by the user, database operation several times, but multiple database access for the entire operatingWeb
system, will affect the efficiency, especially when traffic increases, the pressure on the database also We will keep rising.With respect to the disk and memory operations, the cost of database access operations to pay much greater
First browser request,
cache
will cache the entire contents of a single variable, or the like to the hard disk or the page memory, and setresponse
the headWhen a browser initiates a request again, will be compared with the cache expiration time, if the cache time is relatively new, it will re-request data and cached and then returned
response
to the client, if the cache has not expired, then extracted directly from the cache data back toresponse
the client
Cache-Control
HTTP
Protocol headerCache-Control
,Cache-Control
withExpires
the same effect, is to specify the duration of the current resources, control whether the browser cache fetch data directly from the browser or re-send the request to the server to take data. ButCache-Control
more options, more detailed settings, if at the same time set, it takes precedence over theExpires
In
python
usememcached
we need additional installationmemcached
asmemcache
client support
1 |
pip3 install python-memcached -i https://pypi.tuna.tsinghua.edu.cn/simple |
Cache Settings
memcached
-
installation
memcached
1
2apt-get install memcached # debian
yum install memcached # centos -
Profiles:
/etc/memcached.conf
There are two configuration files may need to modify the parameters
1
2-m 64 #memcached memory size can be used by
-l 127.0 .0 .1 # listening IP address -
On | Off
memcached
Service1
2systemctl start memcached # open
systemctl stop memcached # close -
Check the service status
1
systemctl status memcached
Configuration settings
Use
memcached
the cache, you first need the projectsettings
to configure the next file
1 |
= {CACHES 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', # specified engine used by the cache 'LOCATION': '172.16.19.26:11211', # specified cache server address, often the host address } } |
View Cache
Can be cached for only some view function
Use
django.views.decorators.cache
decorator incache_page
a view function can be decorated
- The code table model classes
1 |
class People(models.Model): |
- Code view function
1 |
django.views.decorators.cache from Import cache_page |
- Template page code
1 |
<!DOCTYPE html> |
After the first refresh your browser,
Now add a new data object in the database
Then continue to refresh the browser, reads the front page will result cache, but does not display the newly added user
In addition to the cache is provided in view of the decorator function can also use the same function disposed decorator route matching section
The station's cache
The entire station is provided all the cache view, need to add the following section arranged end to end in the profile of the middleware
1 |
= MIDDLEWARE [ 'django.middleware.cache.UpdateCacheMiddleware', # header to add middleware # response will be cached 'django.middleware.security.SecurityMiddleware', ... 'django.middleware.cache.FetchFromCacheMiddleware', # tail to add middleware # cached response taken out ] |
As well as with the global variable current site-wide cache valid time
1 |
= CACHE_MIDDLEWARE_SECONDS 10 # of seconds per page caching, default is 600 |
Local Cache
The main local cache to cache in the template page, select an area, when the user accesses the same page again, such as setting the unexpired cache, the cache does not regenerate local time rendering
1 |
{% Load cache%} |
Time template variables, for example, do a simple test, backstage view function every time visit, return to the current time
1 |
import time |
模板页面在使用时的代码
1 |
{% load cache %} |
手动缓存
除了以上应用于各个业务中的缓存方式,还可以使用
django
所提供的cache
接口进行缓存设置以及获取
- 设置缓存
1 |
from django.core.cache import cache |
- 获取缓存
1 |
#获取缓存数据 |
- 避免
key
值重复导致更新缓存,可以使用cache.add
函数,基本用法与set
相同
1 |
status = cache.add('add_key', 'New value') |
- 清除缓存,通过
cache.delete
方法,该方法接收一个缓存key
值
1 |
cache.delete('cache_key') |
- 清空缓存,通过
cache.clear
方法,直接从缓存中清除所有
1 |
cache.clear() |
注意
memcached
不允许使用超过250
个字符或包含空格或控制字符的缓存键值使用这样的键值将会导致异常