Django 之缓存

1、缓存python

因为Django是动态网站,全部每次请求均会去数据进行相应的操做,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中,5分钟内再有人来访问时,则再也不去执行view中的操做,而是直接从内存或者Redis中以前缓存的内容拿到,并返回。数据库

Django中提供了6种缓存方式:django

  • 开发调试
  • 内存
  • 文件
  • 数据库
  • Memcache缓存(python-memcached模块)
  • Memcache缓存(pylibmc模块)

一、配置缓存

a、开发调试ide

 1 # 此为开始调试用,实际内部不作任何操做
 2     # 配置:
 3         CACHES = {  4             'default': {  5                 'BACKEND': 'django.core.cache.backends.dummy.DummyCache',     # 引擎
 6                 'TIMEOUT': 300,                                               # 缓存超时时间(默认300,None表示永不过时,0表示当即过时)
 7                 'OPTIONS':{  8                     'MAX_ENTRIES': 300,                                       # 最大缓存个数(默认300)
 9                     'CULL_FREQUENCY': 3,                                      # 缓存到达最大个数以后,剔除缓存个数的比例,即:1/CULL_FREQUENCY(默认3)
10  }, 11                 'KEY_PREFIX': '',                                             # 缓存key的前缀(默认空)
12                 'VERSION': 1,                                                 # 缓存key的版本(默认1)
13                 'KEY_FUNCTION' 函数名                                          # 生成key的函数(默认函数会生成为:【前缀:版本:key】)
14  } 15  } 16 
17 
18     # 自定义key
19     def default_key_func(key, key_prefix, version): 20         """
21  Default function to generate keys. 22 
23  Constructs the key used by all other methods. By default it prepends 24  the `key_prefix'. KEY_FUNCTION can be used to specify an alternate 25  function with custom key making behavior. 26         """
27         return '%s:%s:%s' % (key_prefix, version, key) 28 
29     def get_key_func(key_func): 30         """
31  Function to decide which key function to use. 32 
33  Defaults to ``default_key_func``. 34         """
35         if key_func is not None: 36             if callable(key_func): 37                 return key_func 38             else: 39                 return import_string(key_func) 40         return default_key_func
View Code

b、内存memcached

 1 # 此缓存将内容保存至内存的变量中
 2     # 配置:
 3         CACHES = {  4             'default': {  5                 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',  6                 'LOCATION': 'unique-snowflake',  7  }  8  }  9 
10     # 注:其余配置同开发调试版本
View Code

c、文件函数

 1 # 此缓存将内容保存至文件
 2     # 配置:
 3 
 4         CACHES = {  5             'default': {  6                 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',  7                 'LOCATION': '/var/tmp/django_cache',  8  }  9  } 10     # 注:其余配置同开发调试版本
View Code

d、数据库post

 1 # 此缓存将内容保存至数据库
 2 
 3     # 配置:
 4         CACHES = {  5             'default': {  6                 'BACKEND': 'django.core.cache.backends.db.DatabaseCache',  7                 'LOCATION': 'my_cache_table', # 数据库表
 8  }  9  } 10 
11     # 注:执行建立表命令 python manage.py createcachetable
View Code

e、Memcache缓存(python-memcached模块)
网站

 1 # 此缓存使用python-memcached模块链接memcache
 2 
 3     CACHES = {  4         'default': {  5             'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',  6             'LOCATION': '127.0.0.1:11211',  7  }  8  }  9 
10     CACHES = { 11         'default': { 12             'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 13             'LOCATION': 'unix:/tmp/memcached.sock', 14  } 15  } 16 
17     CACHES = { 18         'default': { 19             'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 20             'LOCATION': [ 21                 '172.19.26.240:11211', 22                 '172.19.26.242:11211', 23  ] 24  } 25     }
View Code

f、Memcache缓存(pylibmc模块)url

 1 # 此缓存使用pylibmc模块链接memcache
 2     
 3     CACHES = {  4         'default': {  5             'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',  6             'LOCATION': '127.0.0.1:11211',  7  }  8  }  9 
10     CACHES = { 11         'default': { 12             'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache', 13             'LOCATION': '/tmp/memcached.sock', 14  } 15  } 16 
17     CACHES = { 18         'default': { 19             'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache', 20             'LOCATION': [ 21                 '172.19.26.240:11211', 22                 '172.19.26.242:11211', 23  ] 24  } 25     }
View Code

二、应用

a. 全站使用

 1 使用中间件,通过一系列的认证等操做,若是内容在缓存中存在,则使用FetchFromCacheMiddleware获取内容并返回给用户,当返回给用户以前,判断缓存中是否已经存在,若是不存在则UpdateCacheMiddleware会将缓存保存至缓存,从而实现全站缓存  2 
 3     MIDDLEWARE = [  4         'django.middleware.cache.UpdateCacheMiddleware',  5         # 其余中间件...
 6         'django.middleware.cache.FetchFromCacheMiddleware',  7  ]  8 
 9     CACHE_MIDDLEWARE_ALIAS = ""
10     CACHE_MIDDLEWARE_SECONDS = ""
11     CACHE_MIDDLEWARE_KEY_PREFIX = ""
View Code

b. 单独视图缓存

 1 方式一:  2         from django.views.decorators.cache import cache_page  3 
 4         @cache_page(60 * 15)  5         def my_view(request):  6  ...  7 
 8  方式二:  9         from django.views.decorators.cache import cache_page 10 
11         urlpatterns = [ 12             url(r'^foo/([0-9]{1,2})/$', cache_page(60 * 15)(my_view)), 13         ]
View Code

c、局部视图使用

1 a. 引入TemplateTag 2 
3         {% load cache %} 4 
5  b. 使用缓存 6 
7         {% cache 5000 缓存key %} 8  缓存内容 9         {% endcache %}
View Code

更多:猛击这里

相关文章
相关标签/搜索