安装 django-redis
pip install django-redis==4.11.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
配置
# settings.py
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 100}
# "PASSWORD": "密码",
}
使用
# 在哪里使用,就在那里调用
# views.py
from django_redis import get_redis_connection
conn = get_redis_connection()
conn.set(phone, random_code, ex=30) # 将手机号、随机短信码存入,并设置过期时间为30s