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

  

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM