安装包如下
pip install redis-py-cluster
redis-py-cluster源码地址https://github.com/Grokzen/redis-py-cluster
创建⽂件redis_cluster.py,示例码如下:
from rediscluster import RedisCluster
if __name__ == '__main__':
try:
# 构建所有的节点,Redis会使⽤CRC16算法,将键和值写到某个节点上
startup_nodes = [
{'host': '192.168.X.12', 'port': '7000'},
{'host': '192.168.X.12', 'port': '7002'},
{'host': '192.168.X.12', 'port': '7001'},
]
# 构建StrictRedisCluster对象
src = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)
# 设置键为name、值为abcd的数据
result = src.set('name2', 'abcd')
print(result)
# 获取键为name
name = src.get('name2')
print(name)
except Exception as e:
print(e)
运行效果: