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)

猜你喜欢

转载自blog.csdn.net/brightgreat/article/details/131079678