Redis与Cache

Django实战教程

CACHES的配置

本项目使用的redis进行的缓存 sudo apt-get install -y redis 配合hiredis访问redis

1
2
git clone https://github.com/redis/hiredis.git
cd hiredis & make & makeinstall

虚拟环境配合安装包

1
2
3
pip install django-redis
pip install django-redis-cache
pip install hiredis

settings.py里关于caches的配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
CACHES = {
# "default":{
# "BACKEND": "django_redis.cache.RedisCache",
# "LOCATION": "127.0.0.1:6379",
# 'OPTIONS': {
# #"CLIENT_CLASS": "redis_cache.client.DefaultClient",
# "DB": 0,
# "PARSER_CLASS": "redis.connection.HiredisParser",
# "CONNECTION_POOL_CLASS": "redis.BlockingConnectionPool",
# "PICKLE_VERSION": -1
# }
# },
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': ['127.0.0.1:6379'],
'OPTIONS': {
'DB': 1,
'PARSER_CLASS': 'redis.connection.HiredisParser',
'CONNECTION_POOL_CLASS': 'redis.BlockingConnectionPool',
'CONNECTION_POOL_CLASS_KWARGS': {
'max_connections': 50,
'timeout': 20,
},
'MAX_CONNECTIONS': 1000,
'PICKLE_VERSION': -1,
},
},
# 存放微信公众号 token
'wechat': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': ['127.0.0.1:6379'],
'OPTIONS': {
'DB': 2,
'PARSER_CLASS': 'redis.connection.HiredisParser',
'CONNECTION_POOL_CLASS': 'redis.BlockingConnectionPool',
'CONNECTION_POOL_CLASS_KWARGS': {
'max_connections': 50,
'timeout': 20,
},
'MAX_CONNECTIONS': 1000,
'PICKLE_VERSION': -1,
},
}
}

****


📚 返回目录

本文作者:Samjoe Yang

本文链接: https://need.uno/django-course-rediscache/

版权声明:本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。

评论