1 安装django-redis
bash
pip3 install django-redis
python
CACHES={
"default":{
"BACKEND":"django_redis.cache.RedisCache",
"LOCATION":"redis://"+redis_host+":"+redis_port, #redis主机地址和端口,数据在local_settings中
"OPTIONS":{
"CLIENT_CLASS":"django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS":{
"max_connections":1000,
"encoding":"utf-8"
},
"PASSWORD":redis_password #redis密码,数据在local_settings中
}
}
}
3、连接redis
python
conn=get_redis_connection('default') #这个default就是上一步中定义的redis
conn.set(phoneNum,code,ex=30) #设置键值
print(conn.get(phoneNum).decode('utf-8')) #读取键值