首页 > 其他 > 详细

Django【进阶篇-缓存类型】

时间:2020-02-06 21:42:59      阅读:79      评论:0      收藏:0      [点我收藏+]

Session

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

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

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

 

1、配置

a、开发调试

技术分享图片
 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、内存

技术分享图片
 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、数据库

技术分享图片
 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模块)

技术分享图片
 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

g. Redis缓存(依赖:pip3 install django-redis)

技术分享图片
 1 CACHES = {
 2     "default": {
 3         "BACKEND": "django_redis.cache.RedisCache",
 4         "LOCATION": "redis://127.0.0.1:6379",
 5         "OPTIONS": {
 6             "CLIENT_CLASS": "django_redis.client.DefaultClient",
 7             "CONNECTION_POOL_KWARGS": {"max_connections": 100}
 8             # "PASSWORD": "密码",
 9         }
10     }
11 }
View Code

 

Django【进阶篇-缓存类型】

原文:https://www.cnblogs.com/fuyuteng/p/12270054.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!