CACHES = {'default': {'BACKEND': 'django_redis.cache.RedisCache','LOCATION': 'redis://127.0.0.1:6379/0','OPTIONS': {'CLIENT_CLASS': 'django_redis.client.DefaultClient','CONNECTION_POOL_KWARGS':{'decode_responses':True}}},'sms': {'BACKEND': 'django_redis.cache.RedisCache','LOCATION': 'redis://127.0.0.1:6379/1','OPTIONS': {'CLIENT_CLASS': 'django_redis.client.DefaultClient','CONNECTION_POOL_KWARGS':{'decode_responses':True}}}
}
使用
from django_redis import get_redis_connection# 默认连接配置文件中的库redis_conn = get_redis_connection()#校验key是否存在if not redis_conn.exists(f'activation_{username}'):context = {'code': 10011,'error': '激活用户不存在'}return JsonResponse(context)rand = redis_conn.get(f'activation_{username}')code2 = md5(username + rand)