redis 存储验证码 基本使用

1 redis 存储验证码 基本使用 

1.1 setting 配置 

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1", # 使用的库1
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

2.2 把随机数字存储道数据库设置有效时间

import random
content='%06d' % random.randint(0, 999999)  # 随机6位验证码

from django_redis import get_redis_connection  # 
redis_client = get_redis_connection('default')  # 指定设置的数据库名称
redis_client.setex(my_mail, 60 * 5, content)  # email: content  键  , 有效时间300s  , 随机数字   

3 读取redis 库 里面的数据

from django_redis import get_redis_connection  #
code=request.data.get('code') # 前端获取code
redis_client = get_redis_connection('default')  # 指定数据库
redis_code=redis_client.get(email)  # phone:code   # 通过键 获取验证码

if redis_code:
	redis_code= redis_code.decode()  # 编码 转变
if not code ==redis_code:
	return Response({'msg':'验证码不正确'})

  

  

原文地址:https://www.cnblogs.com/zhangshijiezsj/p/13887725.html