使用redis-py-cloud操作redis5 stream数据

redis-py-cloud 是基于redis-py-cluster 开发的redis集群包,特别支持redis5.0的stream新特性。目前支持的命令有 xadd, xread, xreadgroup, xack 等。

 

包下载地址:

https://github.com/ChinaGoldBear/redis-py-cloud

或pip install:

pip install redis-py-cloud

 

基本操作:

1.连接集群

from rediscluster import RedisCluster

'''连接集群'''
startup_nodes = [{"host": "ip3", "port": "7000"},
                 {"host": "ip2", "port": "7000"},
                 {"host": "ip1", "port": "7000"}, ]
 
REDIS_CLUSTER = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)

 

2.xadd

REDIS_CLUSTER.xadd("mystream", "*", 100,{"name": "data"})

 

3.xread

REDIS_CLUSTER.xread("mystream","*",1) # 非阻塞读取最后一条

REDIS_CLUSTER.xread("mystream","*",1,0) # 阻塞读取最后一条

REDIS_CLUSTER.xread("mystream","*",10,0) # 阻塞读取最后10条

REDIS_CLUSTER.xread("mystream","1527849629172-0",1,0) # 阻塞读取特定消息

4. xrange

REDIS_CLUSTER.xrange("mystream","-","+") # -表示最小值, +表示最大值

REDIS_CLUSTER.xrange("mystream","1527849629172-0","+") # 指定最小消息ID的列表

REDIS_CLUSTER.xrange("mystream","-","1527849629172-0") # 指定最大消息ID的列表

 

 5.xreadgroup

REDIS_CLUSTER.xreadgroup("group_name","consumer_name","mystream",">",0) # 消息组读取,0表示阻塞读取

REDIS_CLUSTER.xreadgroup("group_name","consumer_name","mystream",">") # 消息组读取,非阻塞读取

REDIS_CLUSTER.xreadgroup("group_name","consumer_name","mystream","1527849629172-0") # 消息组特定消息读取,非阻塞读取

 

6.xack

REDIS_CLUSTER.xack("mystream","counsumer_name","1527849629172-0")  # ack消费组消息

 

 

其他stream命令请参考:

https://blog.csdn.net/enmotech/article/details/81230531

 

 

 

 

 

你可能感兴趣的:(redis)