python redis 链接集群 阿里云集群

前言
集群redis不支持选db cluster方法里没有支持选中db的选项,java py 都不行

# pip install redis==3.5.3
# pip install redis-py-cluster==2.1.3
# 亲测,我是使用的这两个版本进行处理的
from rediscluster import RedisCluster
nodes = [{"host": "dsfwwqfggy65aadfggi.redis.rds.aliyuncs.com", "port": "6379"}]
r = RedisCluster(startup_nodes=nodes, decode_responses=True,  password='lljh1188daff9u21WWdaFaKKj')
key = 'subscribe:'

if __name__ == '__main__':
    for i in range(1, 10):
        r.set(key + str(i), i)
        r.expire(key + str(i), '1000')

还有另外一种集群方式,自己搭建后,使用节点链接(未验证过)

from rediscluster import RedisCluster
nodes = [{'host': 'xxx', 'port': xxx},
         {'host': 'xxx', 'port': xxx},
         {'host': 'xxx', 'port': xxx}
        ]
r = RedisCluster(startup_nodes=nodes, decode_responses=True,  password='lljh1188daff9u21WWdaFaKKj')
key = 'subscribe:'

if __name__ == '__main__':
    for i in range(1, 10):
        r.set(key + str(i), i)
        r.expire(key + str(i), '1000')

如果是单节点,那就直接使用redis包进行链接即可
将上述代码更换为
r = redis.Redis(host='127.0.0.1', port=6379, db=2)

posted @ 2023-06-06 12:10  darling331  阅读(39)  评论(0编辑  收藏  举报