• redis和selery相关知识点


    一:redis字符串操作

    # 1 set(name, value, ex=None, px=None, nx=False, xx=False)
    
    import redis
    
    conn = redis.Redis()
    # 1  set(name, value, ex=None, px=None, nx=False, xx=False)
    #      ex,过期时间(秒)
    #      px,过期时间(毫秒)
    #      nx,如果设置为True,则只有name不存在时,当前set操作才执行,值存在,就修改不了,执行没效果
    #      xx,如果设置为True,则只有name存在时,当前set操作才执行,值存在才能修改,值不存在,不会设置新值
    
    # conn.set('name','lqz') # value 只能是字符串或byte格式
    # conn.set('name','lqz',ex=3) # ex 是过期时间,到3s过期,数据就没了
    # conn.set('name','lqz',px=3000) # px 是过期时间,到3s过期,数据就没了
    # conn.set('age',25,nx=True) # redis 实现分布式锁:https://zhuanlan.zhihu.com/p/489305763
    # conn.set('hobby', '足球', xx=False)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    # 2 setnx(name, value)   就是:set nx=True
    # conn.setnx('hobby1','橄榄球')
    
    • 1
    • 2
    # 3 psetex(name, time_ms, value)  本质就是 set px设置时间
    # conn.psetex('name',3000,'lqz')
    
    • 1
    • 2
    # 4 mset(*args, **kwargs)  传字典批量设置
    # conn.mset({'name':'xxx','age':19})
    
    • 1
    • 2
    # 5 get(name)  获取值,取到是bytes格式   ,指定:decode_responses=True,就完成转换
    # print(conn.get('name'))
    # print(str(conn.get('name')[:3],encoding='utf-8'))
    
    • 1
    • 2
    • 3
    # 6 mget(keys, *args)  #批量获取
    # res=conn.mget('name','age')
    # res=conn.mget(['name','age'])
    # print(res)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    # 7 getset(name, value)  # 先获取,再设置
    # res=conn.getset('name','lqz')
    # print(res)
    
    • 1
    • 2
    • 3
    # 8 getrange(key, start, end) # 取的是字节,前闭后闭区间
    # res=conn.getrange('name',0,1)
    # print(res)
    
    • 1
    • 2
    • 3
    # 9 setrange(name, offset, value)  # 从某个起始位置开始替换字符串
    # conn.setrange('name', 1, 'xxx')
    
    • 1
    • 2
    # 10 setbit(name, offset, value)
    # conn.setbit('name',1,0)   #lqz  00000000   00000000   00000000
    # res=conn.get('name')
    # print(res)
    
    • 1
    • 2
    • 3
    • 4
    # 11 getbit(name, offset)
    # res=conn.getbit('name',1)
    # print(res)
    
    • 1
    • 2
    • 3
    # 12 bitcount(key, start=None, end=None)
    # print(conn.bitcount('name',0,3))  # 3 指的是3个字符
    
    • 1
    • 2
    # 13 strlen(name)  # 统计字节长度
    # print(conn.strlen('name'))
    # print(len('lqz政'))  # len 统计字符长度
    
    • 1
    • 2
    • 3
    # 14 incr(self, name, amount=1)  # 计数器
    # conn.incr('age',amount=3)
    
    • 1
    • 2
    # 15 incrbyfloat(self, name, amount=1.0)
    
    • 1
    # 16 decr(self, name, amount=1)
    # conn.decr('age')
    
    • 1
    • 2
    # 17 append(key, value)
    conn.append('name','nb')
    conn.close()
    
    • 1
    • 2
    • 3

    二:redis hash操作

    import redis
    
    conn = redis.Redis(decode_responses=True)
    # 1 hset(name, key, value)
    # conn.hset('userinfo', 'name', '彭于晏')
    # conn.hset('userinfo', 'age', '32')
    # conn.hset('xx',mapping={'name':'xxx','hobby':'篮球'})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    # 2 hmset(name, mapping)   弃用了
    # conn.hmset('yy',{'a':'a','b':'b'})
    
    • 1
    • 2
    # 3 hget(name,key)
    # res=conn.hget('userinfo','age')
    # print(res)
    
    • 1
    • 2
    • 3
    # 4 hmget(name, keys, *args)
    # res=conn.hmget('userinfo',['name','age'])
    # print(res)
    
    • 1
    • 2
    • 3
    # 5 hgetall(name)    慎用,可能会造成 阻塞 尽量不要在生产代码中执行它
    # res=conn.hgetall('userinfo')
    # print(res)
    
    • 1
    • 2
    • 3
    # 6 hlen(name)
    # res=conn.hlen('userinfo')
    # print(res)
    
    • 1
    • 2
    • 3
    # 7 hkeys(name)
    # res=conn.hkeys('userinfo')
    # print(res)
    
    • 1
    • 2
    • 3
    # 8 hvals(name)
    # res=conn.hvals('userinfo')
    # print(res)
    
    • 1
    • 2
    • 3
    # 9 hexists(name, key)
    # res=conn.hexists('userinfo','name')
    # print(res)
    
    • 1
    • 2
    • 3
    # 10 hdel(name,*keys)
    # conn.hdel('userinfo','age')
    
    • 1
    • 2
    # 10 hdel(name,*keys)
    # conn.hdel('userinfo','age')
    
    • 1
    • 2
    # 11 hincrby(name, key, amount=1)
    # conn.hincrby('userinfo','age')
    
    • 1
    • 2
    # 12 hincrbyfloat(name, key, amount=1.0)
    # conn.hincrbyfloat('userinfo','age',5.44)
    
    • 1
    • 2
    ## 联合起来讲:不建议使用hgetall,分片取值
    # 分批获取               生成器应用在哪了?
    # 13 hscan(name, cursor=0, match=None, count=None)
    # hash类型没有顺序---》python字典 之前没有顺序,3.6后有序了    python字段的底层实现
    # for i in range(1000):
    #     conn.hset('test_hash','key_%s'%i,'鸡蛋%s号'%i)
    
    # count 是要取的条数,但是不准确,有点上下浮动
    # 它一般步单独用
    # res=conn.hscan('test_hash',cursor=0,count=19)
    # print(res)
    # print(res[0])
    # print(res[1])
    # print(len(res[1]))
    # res=conn.hscan('test_hash',cursor=res[0],count=19)
    # print(res)
    # print(res[0])
    # print(res[1])
    # print(len(res[1]))
    
    
    # 咱么用它比较多,它内部封装了hscan,做成了生成器,分批取hash类型所有数据
    # 14 hscan_iter(name, match=None, count=None)  获取所有hash的数据
    res = conn.hscan_iter('test_hash',count=100)
    print(res)  # 生成器
    for item in res:
        print(item)
    
    conn.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    三:redis列表操作

    1 lpush(name,values)
    2 lpushx(name,value)
    3 rpushx(name, value) 表示从右向左操作
    4 llen(name)
    5 linsert(name, where, refvalue, value))
    6 lset(name, index, value)
    7 lrem(name, value, num)
    8 lpop(name)
    9 lindex(name, index)
    10 lrange(name, start, end)
    11 ltrim(name, start, end)
    12 rpoplpush(src, dst)
    13 blpop(keys, timeout)
    14 brpoplpush(src, dst, timeout=0)
    15 自定义增量迭代
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    四:redis管道

    1.redis数据库,是否支持事务?
    redis事务机制可以保证一致性和隔离性
    无法保证持久性:对于redis而言,本身是内存数据库,所以持久化不是必须属性
    原子性:需要需要自己进行检查,尽可能保证
    
    
    redis 不像mysql一样,支持事务,事务的4大特性不能全部满足,但是能满足一部分,通过redis的管道实现
    
    redis本身不支持事务,但是可以通过管道实现部分事务
    
    
    redis 通过管道,来保证命令要么都成功,要么都失败,完成事务的一致性,但是管道只能用在单例模式,集群环境中,不支持pipline
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.redis代码实现事务

    import redis
    
    
    conn = redis.Redis()
    pipline = conn.pipeline(transaction=True)
    pipline.decr('a', 2)  # a减2
    raise Exception('我吖奔了')
    pipline.incr('b', 2)  # a加2
    pipline.execute()
    conn.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    五:redis其他操作

    # 集合,有序集合 --- redis模块提供的方法API
    # 通用操作:无论是5大类型的那种,都支持
    
    • 1
    • 2
    import redis
    
    conn = redis.Redis()
    
    # 1 delete(*names)
    conn.delete('age', 'name')
    
    # 2 exists(name)
    res = conn.exists('xx')
    print(res)
    
    # 3 keys(pattern='*')
    res = conn.keys('*o*')
    res = conn.keys('?o*')
    print(res)
    
    # 4 expire(name, time)
    conn.expire('test_hash', 3)
    
    # 5 rename(src, dst)  对redis的name重命名
    conn.rename('xx', 'xxxxxx')
    
    # 6 move(name, db)  将redis的某个值移动到指定的db下
    # 默认操作的都是0库,总共默认有16个库
    conn.move('xxx', 2)
    
    # 7 randomkey()  随机获取一个redis的name(不删除)
    # res=conn.randomkey()
    # print(res)
    
    # 8 8 type(name)  查看类型
    # res = conn.type('aa')  # list  hash set
    # print(res)
    
    conn.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    六:django中集成redis

    1.方式一:直接使用
    # 方式一:直接使用
    from user.POOL import pool
    import redis
    def index(request):
        conn = redis.Redis(connection_pool=pool)
        conn.incr('page_view')
        res = conn.get('page_view')
        return HttpResponse('被你看了%s次' % res)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    2.方式二:使用第三方模块:django-redis
    -下载
        -配置文件配置
            CACHES = {
            "default": {
                "BACKEND": "django_redis.cache.RedisCache",
                "LOCATION": "redis://127.0.0.1:6379/0",
                "OPTIONS": {
                    "CLIENT_CLASS": "django_redis.client.DefaultClient",
                    "CONNECTION_POOL_KWARGS": {"max_connections": 100}
                    # "PASSWORD": "123",
                }
            }
        }
        -使用
        from django_redis import get_redis_connection
        def index(request):
            conn = get_redis_connection(alias="default") # 每次从池中取一个链接
            conn.incr('page_view')
            res = conn.get('page_view')
            return HttpResponse('被你看了%s次' % res)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    3.方式三:借助于django的缓存使用redis
    -如果配置文件中配置了 CACHES  ,以后django的缓存,数据直接放在redis中
    -以后直接使用cache.set 设置值,可以传过期时间
    -使用cache.get 获取值
    -强大之处在于,可以直接缓存任意的python对象,底层使用pickle实现的
    
    • 1
    • 2
    • 3
    • 4

    七:celery介绍

    1.介绍
    分布式的异步任务   框架
    
    • 1
    2.celery的作用?
    1.完成异步任务:可以提高项目的并发量(之前开启线程来实现)
    2.完成延迟任务
    3.完成定时任务
    
    • 1
    • 2
    • 3
    3.架构
    1.消息中间件:broker提交的任务(函数)都放在这里,celery本身不提供消息中间件,需要借助第三方:redis, rabbitmq
    2.任务执行单位:worker,真正执行任务的地方,一个个进程,执行函数
    3.结果存储:backend,函数return的结果存储在这里,celery本身不提供结果存储,借助于第三方:redis, 数据库,rabbitmq
    
    • 1
    • 2
    • 3

    在这里插入图片描述

  • 相关阅读:
    【网络安全---XSS漏洞(1)】XSS漏洞原理,产生原因,以及XSS漏洞的分类。附带案例和payload让你快速学习XSS漏洞
    【FreeCodeCamp】 ResponsiveWebDesign网页设计 测试2学习笔记
    我的Mysql突然挂了(Communications link failure)
    linux嵌入式开发所用工具
    VS2008用“CTRL+F”查找对话框没弹出来
    2023 羊城杯 final
    virtualBox5.2.34安装问题
    C 学生管理系统 打印/修改指定位置信息
    NET Framework合集
    Debian安装Docker环境
  • 原文地址:https://blog.csdn.net/Yydsaoligei/article/details/127876037