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) # 阻塞读取特定消息
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 命令请参考:
作者:copyangle 来源:CSDN 原文: https://blog.csdn.net/copyangle/article/details/81975975 版权声明:本文为博主原创文章,转载请附上博文链接!