zoukankan      html  css  js  c++  java
  • Redis

    pip3 install redis
    

    1 Python操作Redis之普通连接

    from redis import Redis
    conn=Redis(host='127.0.0.1', port=6379)
    

    2 Python操作Redis之连接池

    注意:pool必须是单例,因为将定义连接池的文件当模块导入,只会执行一遍,产生一个连接池,同一个id

    包内的py文件,如果想右键运行这个文件,导包的时候不能带点

    # 连接池
    import redis
    
    POOL = redis.ConnectionPool(host='127.0.0.1', port=6379,max_connections=100)
    
    # 导入连接池,并从连接池获得一个连接
    import redis
    from t_redis_pool import POOL
    
    conn = redis.Redis(connection_pool=POOL)
    

    3 操作之String操作

    conn.set('height',180)
    conn.set('height','190',nx=True)
    conn.set('height','190',nx=True)
    conn.set('height1','190',xx=True)
    
    	 ex,过期时间(秒)
         px,过期时间(毫秒)
         nx,如果设置为True,name不存在,执行set操作
         xx,如果设置为True,name存在,执行set操作,设置值
    
    setnx(name, value)
    setex(name, value, time)
    time 过期时间(秒 或 timedelta对象)
    
    psetex(name, time_ms, value)
    time_ms,过期时间(数字毫秒 或 timedelta对象
    
    conn.mset({'name1':'11','name3':'dasfd'})
    ret=conn.mget(['name1','name','name3'])
    
    ret=conn.getset('name1', '999')
    
    ret=conn.getrange('name1',0,0) # 前闭后闭区间
    
    conn.setrange('name1',1,88888)
    
    ret=conn.getbit('name1',9)
    
    # incr :统计网站访问量,页面访问量,接口访问量
    conn.incr('name1')  # 只要一执行,数字加1
    conn.incr('name1',-2)  # 设置负数,执行一次减少
    # 减少
    conn.decr('name1',3)
    
    conn.append('name1','oo')
    

    4 操作之Hash操作

    conn.hset('hash1','name','lqz')  # key不可以重复
    ret=conn.hget('hash1','name')  # 只能取一个
    
    conn.hmset('hash2',{'key1':'value1','key2':'value2'})
    ret=conn.hmget('hash1','name','name2')
    ret=conn.hmget('hash1',['name','name2'])
    
    ret=conn.hgetall('hash1')  # 尽量少用,数据全部取出,内存,,,
    取出hash类型内所有的数据 生成器
    ret=conn.hscan_iter('hash1')
    
    ret=conn.hlen('hash1')
    ret=conn.hkeys('hash1')
    ret=conn.hexists('hash1','name1')
    ret=conn.hdel('hash1','name')
    
    自增
    ret=conn.hincrby('hash1','name')
    

    5 操作之List操作

    ret=conn.lpush('list1',1,2,3,4,5)
    ret=conn.rpush('list1',999)
    ret=conn.lpushx('list2',1)
    
    ret=conn.lpushx('list1',888)  # 必须有这个key才能放
    ret=conn.rpushx('list1',666)
    ret=conn.llen('list1')
    
    ret=conn.linsert('list1','before','3','77777777')
    ret=conn.linsert('list1','after','3','66666666')
    
    ret=conn.lset('list1',3,'22222')  #从0开始计数
    ret=conn.lset('list1',0,'11111')
    
    ret=conn.lrem('list1',2,'5')  # 从前往后删除两个5
    ret=conn.lrem('list1',-1,'5') # 从后往前删除1个5
    ret=conn.lrem('list1',0,'5')   # 删除所有5
    
    ret=conn.lpop('list1')
    ret=conn.rpop('list1')
    
    ret=conn.lindex('list1',0)
    
    ret=conn.lrange('list1',0,2)  # 前闭后闭
    
    ret=conn.ltrim('list1',1,2)
    
    重点block,阻塞,可以写一个超时时间
    ret=conn.blpop('list1',timeout=10)
    
    conn.flushall()
    
    def scan_list(name,count=2):
        index=0
        while True:
            data_list=conn.lrange(name,index,count+index-1)
            if not data_list:
                return
            index+=count
            for item in data_list:
                yield item
    

    5 redsi的其他使用

    conn.delete('list1')
    ret=conn.delete('hash1')
    
    ret=conn.exists('hash2')
    ret=conn.keys('cache*')  #查询以cache开头的所有key
    
    ret=conn.expire('hash2',2)  # 设置过期时间
    
    ret=conn.type('name3')
    

    6 管道

    # redis支持事务
    # 管道:实现事务
    import redis
    pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
    conn = redis.Redis(connection_pool=pool)
    
    pipe = r.pipeline(transaction=False)
    pipe = conn.pipeline(transaction=True)
    pipe.multi()
    pipe.set('name', 'alex')
    pipe.set('role', 'sb') 
    pipe.execute()  # 这句话,才真正的去执行
    

    7 Django中使用redis

    # 方式一:用原来的redis操作,存取
    
    # 方式二:django-redis
    
    pip install django-redis
    
    etting中配置
        	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}
                            # "PASSWORD": "123",
                        }
                    }
                }
    
    使用cache
            from django.core.cache import cache
            cache.set('name',user) 
    

    8 接口缓存

    def list(self, request, *args, **kwargs):
        # 先去缓存拿数据
        banner_list=cache.get('banner_list')
        if not banner_list:
            print('走数据库了')
            # 缓存中没有,去数据库拿
            response = super().list(request, *args, **kwargs)
            # 加到缓存
            cache.set('banner_list',response.data,60*60*24)
            return response
        return Response(data=banner_list)
    
  • 相关阅读:
    单片机期末考试简答题汇总
    单片机期末考试填空题汇总
    世界五百强世硕科技工作经历——05
    世界五百强世硕科技工作经历——04
    8,求2~n的素数和
    7,特殊毕达哥拉斯三元组
    6,连续多位数的最大乘积
    5,打印1~n之间的所有素数
    4,打印1~n之间的盈数
    3,求1~n(10)的最小倍数
  • 原文地址:https://www.cnblogs.com/pythonwl/p/13381767.html
Copyright © 2011-2022 走看看