1.安装
pip3 install django_redis -i https://pypi.douban.com/simple
2.settings.py配置
CACHES = {# 默认缓存"default": {"BACKEND": "django_redis.cache.RedisCache","LOCATION": "redis://1.116.131.59:8888/3", # 安装redis的主机的 IP 和 端口"OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient","CONNECTION_POOL_KWARGS": {"max_connections": 1000,"encoding": 'utf-8'},"PASSWORD": "foobared" # redis密码}},# 提供存储短信验证码"sms_code": {"BACKEND": "django_redis.cache.RedisCache","LOCATION": "redis://1.116.131.59:8888/4","OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient","CONNECTION_POOL_KWARGS": {"max_connections": 1000,"encoding": 'utf-8'},"PASSWORD": "foobared" # redis密码}}
}
3.django中使用
from django_redis import get_redis_connection
# 获取链接
conn = get_redis_connection('sms_code')
# 操作
conn.set(mobile_phone, code, ex=60 * 60 * 24 * 14)redis_code = conn.get(telephone)