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':'驗證碼不正確'})