zoukankan      html  css  js  c++  java
  • Python-Redis-常用操作&管道

    常用操作

      1.1 delete(*names)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # 根据删除redis中的任意数据类型
     
    print(r.get('name'))
    r.delete('name')
    print(r.get('name'))
     
    # 输出
    b'bigberg'
    None

      1.2 exists(name)

    1
    2
    3
    4
    5
    6
    7
    8
    # 检测redis的name是否存在
     
    print(r.exists('name'))
    print(r.exists('names'))
     
    #输出
    False
    True

      1.3 keys(pattern='*')

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # 根据模型获取redis的name
      
    # 更多:
        # KEYS * 匹配数据库中所有 key 。
        # KEYS h?llo 匹配 hello , hallo 和 hxllo 等。
        # KEYS h*llo 匹配 hllo 和 heeeeello 等。
        # KEYS h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo
     
    print(r.keys(pattern='n*'))
     
    # 输出
    [b'names', b'num', b'names_dst']

      1.4 expire(name ,time)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # 为某个redis的某个name设置超时时间
     
    print(r.get('num'))
    r.expire('num', 2)
    time.sleep(3)
     
    print(r.get('num'))
     
    #输出
    b'6'
    None

      1.5 rename(src, dst)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # 对redis的name重命名为
     
    print(r.get('info'))
    r.rename('info', 'info-test')
    print(r.get('info-test'))
     
    #输出
    b'this is my test'
    b'this is my test'

      1.6 move(name, db))

    1
    2
    3
    4
    5
    6
    7
    8
    # 将redis的某个值移动到指定的db下
     
    r.move('info-test', 3)
     
    127.0.0.1:6379> select 3
    OK
    127.0.0.1:6379[3]> keys *
    1) "info-test"

      1.7 randomkey()

    1
    2
    3
    4
    5
    6
    # 随机获取一个redis的name(不删除)
     
    print(r.randomkey())
     
    #输出
    b'login_user'

      1.8 type(name)

    1
    2
    3
    4
    5
    6
    # 获取name对应值的类型
     
    print(r.type('login_user'))
     
    #输出
    b'string'

    二、管道

      redis-py默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
      
    import redis
      
    pool = redis.ConnectionPool(host='10.211.55.4', port=6379)
      
    r = redis.Redis(connection_pool=pool)
      
    # pipe = r.pipeline(transaction=False)
    pipe = r.pipeline(transaction=True)
      
    pipe.set('name', 'ddt')
    pipe.set('role', 'superboy')
      
    pipe.execute()







    other 方法 print(r.get('name')) # 查询key为name的值 r.delete("gender") # 删除key为gender的键值对 print(r.keys()) # 查询所有的Key print(r.dbsize()) # 当前redis包含多少条数据 r.save() # 执行"检查点"操作,将数据写回磁盘。保存时阻塞 # r.flushdb() # 清空r中的所有数据 管道(pipeline) redis默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作, 如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。 管道(pipeline)是redis在提供单个请求中缓冲多条服务器命令的基类的子类。它通过减少服务器-客户端之间反复的TCP数据库包,从而大大提高了执行批量命令的功能。 import redis import time pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True) r = redis.Redis(connection_pool=pool) # pipe = r.pipeline(transaction=False) # 默认的情况下,管道里执行的命令可以保证执行的原子性,执行pipe = r.pipeline(transaction=False)可以禁用这一特性。 # pipe = r.pipeline(transaction=True) pipe = r.pipeline() # 创建一个管道 pipe.set('name', 'jack') pipe.set('role', 'sb') pipe.sadd('faz', 'baz') pipe.incr('num') # 如果num不存在则vaule为1,如果存在,则value自增1 pipe.execute() print(r.get("name")) print(r.get("role")) print(r.get("num")) 管道的命令可以写在一起,如: pipe.set('hello', 'redis').sadd('faz', 'baz').incr('num').execute() print(r.get("name")) print(r.get("role")) print(r.get("num"))

      

     
    other 方法
    
    print(r.get('name'))    # 查询key为name的值
    r.delete("gender")  # 删除key为gender的键值对
    print(r.keys()) # 查询所有的Key
    print(r.dbsize())   # 当前redis包含多少条数据
    r.save()    # 执行"检查点"操作,将数据写回磁盘。保存时阻塞
    # r.flushdb()        # 清空r中的所有数据
    管道(pipeline)
    redis默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,
    如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。
    
    管道(pipeline)是redis在提供单个请求中缓冲多条服务器命令的基类的子类。它通过减少服务器-客户端之间反复的TCP数据库包,从而大大提高了执行批量命令的功能。
    
    import redis
    import time
    
    pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
    r = redis.Redis(connection_pool=pool)
    
    # pipe = r.pipeline(transaction=False)    # 默认的情况下,管道里执行的命令可以保证执行的原子性,执行pipe = r.pipeline(transaction=False)可以禁用这一特性。
    # pipe = r.pipeline(transaction=True)
    pipe = r.pipeline() # 创建一个管道
    
    pipe.set('name', 'jack')
    pipe.set('role', 'sb')
    pipe.sadd('faz', 'baz')
    pipe.incr('num')    # 如果num不存在则vaule为1,如果存在,则value自增1
    pipe.execute()
    
    print(r.get("name"))
    print(r.get("role"))
    print(r.get("num"))
    管道的命令可以写在一起,如:
    
    pipe.set('hello', 'redis').sadd('faz', 'baz').incr('num').execute()
    print(r.get("name"))
    print(r.get("role"))
    print(r.get("num"))

    other 方法

    print(r.get('name'))    # 查询key为name的值
    r.delete("gender")  # 删除key为gender的键值对
    print(r.keys()) # 查询所有的Key
    print(r.dbsize())   # 当前redis包含多少条数据
    r.save()    # 执行"检查点"操作,将数据写回磁盘。保存时阻塞
    # r.flushdb()        # 清空r中的所有数据

    管道(pipeline)
    redis默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,
    如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定多个命令,并且默认情况下一次pipline 是原子性操作。

    管道(pipeline)是redis在提供单个请求中缓冲多条服务器命令的基类的子类。它通过减少服务器-客户端之间反复的TCP数据库包,从而大大提高了执行批量命令的功能。

    import redis
    import time
    
    pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
    r = redis.Redis(connection_pool=pool)
    
    # pipe = r.pipeline(transaction=False)    # 默认的情况下,管道里执行的命令可以保证执行的原子性,执行pipe = r.pipeline(transaction=False)可以禁用这一特性。
    # pipe = r.pipeline(transaction=True)
    pipe = r.pipeline() # 创建一个管道
    
    pipe.set('name', 'jack')
    pipe.set('role', 'sb')
    pipe.sadd('faz', 'baz')
    pipe.incr('num')    # 如果num不存在则vaule为1,如果存在,则value自增1
    pipe.execute()
    
    print(r.get("name"))
    print(r.get("role"))
    print(r.get("num"))

    管道的命令可以写在一起,如:

    pipe.set('hello', 'redis').sadd('faz', 'baz').incr('num').execute()
    print(r.get("name"))
    print(r.get("role"))
    print(r.get("num"))
     
  • 相关阅读:
    SWFUpload说明文档
    Ubuntu中root用户和user用户的相互切换
    不用IF比较两数大小
    Linux服务器下验证码图片不显示问题
    常用CSS语法
    常用CSS语法
    漫谈DataList的用法
    Session丢失浅析
    浅谈C#托管程序中的资源释放问题
    C#2.0 泛型详解
  • 原文地址:https://www.cnblogs.com/xiao-xue-di/p/11269598.html
Copyright © 2011-2022 走看看