複習
"""
一、git項目開發
提供公鑰成爲開發者、copy項目、開發項目
先commit、再pull(可能出現衝突)、最後push
特殊功能能夠新建dev的子分支進行開發:git checkout -b 子分支,
切換回dev分支合併子分支內容:git merge 子分支
二、短信
註冊並申請通訊短信服務應用
安裝指定模塊,根據申請的應用配置獲得發送短信的對象,對象調用方法完成短信的發送
二次封裝短信模塊
三、redis
內存、no-sql、能夠緩存(數據奔潰能夠修復)、數據可持久化、支持高併發
"""
今日內容
"""
一、redis使用
二、登陸註冊接口
三、celery異步任務框架
"""
redis
基礎命令
啓動服務:
>: redis-server &
啓動客戶端鏈接redis
>: redis-cli -h localhost -p 6379 -n 數據庫編號(0~15)
鏈接成功後切換數據庫
>: select 數據庫編號
哈希操做
"""
經常使用方法:
單增:hset key field value
單查:hget key field
全部鍵值:hgetall key
單刪:hdel key field
全部key:hkeys key
全部值:hvals key
"""
列表操做
"""
右增: rpush key v1 v2 ... vn
左增: lpush key v1 v2 ... vn
修改: lset key index value
左刪: lpop key
右刪: rpop key
插入:linsert key before|after old_value new_value
區間:lrange key begin_index end_index
"""
集合操做
"""
增:sadd key v1 v2 ... vn
差集:sdiff key1 key2
並集:sinter key1 key2
交集:sunion key1 key2
查:smembers key
隨機刪:spop key
"""
有序集合
"""
增:zadd key score1 value1 score2 value2 ... scoren valuen
區間個數:zcount key begin_score end_score
排行低到高:zrange key begin_index end_index
排行高到低:zrevrange key begin_index end_index
"""
python使用redis
依賴
>: pip3 install redis
直接使用
import redis
r = redis.Redis(host='127.0.0.1', port=6379)
鏈接池使用
import redis
pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
r = redis.Redis(connection_pool=pool)
緩存使用:要額外安裝 django-redis
# 1.將緩存存儲位置配置到redis中: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}
}
}
}
# 2.操做cache模塊直接操做緩存:views.py
from django.core.cache import cache # 結合配置文件實現插拔式
# 存放token,能夠直接設置過時時間
cache.set('token', 'header.payload.signature', 10)
# 取出token
token = cache.get('token')
手機驗證接口
from rest_framework.views import APIView
from .models import User
from utils.response import APIResponse
import re
# 註冊邏輯:1.校驗手機號是否存在 2.發送驗證碼 3.完成註冊
class MobileAPIView(APIView):
def post(self, request, *args, **kwargs):
mobile = request.data.get('mobile')
if not mobile or not re.match(r'^1[3-9]\d{9}$', mobile):
return APIResponse(1, '數據有誤')
try:
User.objects.get(mobile=mobile)
return APIResponse(2, '已註冊')
except:
return APIResponse(0, '未註冊')
發送短信接口
# 發送驗證碼接口分析
from libs import txsms
from django.core.cache import cache
class SMSAPIView(APIView):
def post(self, request, *args, **kwargs):
# 1)拿到前臺的手機號
mobile = request.data.get('mobile')
if not mobile or not re.match(r'^1[3-9]\d{9}$', mobile):
return APIResponse(2, '數據有誤')
# 2)調用txsms生成手機驗證碼
code = txsms.get_code()
# 3)調用txsms發送手機驗證碼
result = txsms.send_sms(mobile, code, 5)
# 4)失敗反饋信息給前臺
if not result:
return APIResponse(1, '短信發送失敗')
# 5)成功服務器緩存手機驗證碼 - 用緩存存儲(方便管理) - redis
cache.set('sms_%s' % mobile, code, 5 * 60)
# 6)反饋成功信息給前臺
return APIResponse(0, '短信發送成功')