redis集群
# 安装所需要的包
pip install redis-py-cluster -i https://mirrors.aliyun.com/pypi/simple/
# code
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from rediscluster import RedisCluster def create_redis_cluster(startup_nodes,password): """ 创建RedisCluster对象并返回 :param startup_nodes: Redis集群节点的配置列表 :return: RedisCluster对象 """ return RedisCluster(startup_nodes=startup_nodes, decode_responses=True,password=password) def get_key_from_redis_cluster(rc, key_name): """ 从Redis集群中查询指定的key :param rc: RedisCluster对象 :param key_name: 要查询的key的名称 :return: key对应的值,如果key不存在则返回None """ try: value = rc.get(key_name) return value except Exception as e: print(f"查询key时发生错误: {e}") return None def close_redis_cluster(rc): """ 关闭RedisCluster连接 :param rc: RedisCluster对象 """ rc.close() # 示例用法 def main(): # Redis集群节点的配置 startup_nodes = [ {"host": "10.18.xx.xx", "port": "xxxx"}, ] password = "f38xxxxxxxxxx5817" # 创建RedisCluster对象 rc = create_redis_cluster(startup_nodes,password) # 要查询的key的名称 key_list = ["clbps_proc","clbps_verify","clbps_fverify"] for key in key_list: value = get_key_from_redis_cluster(rc, key) print(f"key={key} value={value}") close_redis_cluster(rc) if __name__ == "__main__": main()
-----------------------------------------------------------------------------------------------------------------------------------------