django中使用redis
启动redis
1.下载django-redis
模块
pip install django-redis
2.settings.py
中
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379", # 安装redis的主机的 IP 和 端口
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {
"max_connections": 1000,
"encoding": 'utf-8'
},
"PASSWORD": "qwe123" # redis密码
}
}
}
3.使用示例
from django_redis import get_redis_connection
conn = get_redis_connection("default")
key = f"xiaohe_register_{mobile}"
conn.set(key, random_code, ex=60 * 5)
value = conn.get(key)
print("测试读取", value)