• redis 基本操作-python 使用redis-手机验证接口-发送短信接口


    复习

    """
    1、git项目开发
    	提供公钥成为开发者、copy项目、开发项目
    	先commit、再pull(可能出现冲突)、最后push
    	特殊功能可以新建dev的子分支进行开发:git checkout -b 子分支,
    		切换回dev分支合并子分支内容:git merge 子分支
    2、短信
    	注册并申请通信短信服务应用
    	安装指定模块,根据申请的应用配置得到发送短信的对象,对象调用方法完成短信的发送
    	二次封装短信模块
    	
    3、redis
    	内存、no-sql、可以缓存(数据奔溃可以修复)、数据可持久化、支持高并发
    """
    

    今日内容

    """
    1、redis使用
    
    2、登录注册接口
    
    3、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, '短信发送成功')
    
  • 相关阅读:
    美联储主席和欧洲央行说了什么
    12月CPI,PPI有哪些变化
    中国人民银行行长易纲就贯彻落实中央经济工作会议精神接受采访谈
    2018年个人的一些简单预测
    从首套房利率走势看市场
    百城价格房价周期和郑州、武汉房价比较分析
    国际非农超预期美联储主席态度软化,国内适度宽松货币+积极财政仍是主基调
    三大经济体年2018年末形势一览
    从房地产住宅销售面积增速看房地产行业
    枚举类
  • 原文地址:https://www.cnblogs.com/suwanbin-thought/p/11768135.html
Copyright © 2020-2023  润新知