版本信息
- python版本 3.7
- pip install redis==3.5.3
- pip install redis-py-cluste==2.1.3
集群连接
- 单节点连接
通过集群里面的任意节点连接到集群,连接之后,客户端会向服务器发送cluster slots
命令获取集群哈希槽的分布信息。
| import random |
| import rediscluster |
| import string |
| |
| cli = rediscluster.RedisCluster(host='192.168.7.41', port=8001, password="") |
| for i in range(1000): |
| cli.set(''.join(random.sample(string.ascii_letters, 10)), i, ex=1200) |
| print(cli) |
| cli.close() |
- 连接池连接
| import random |
| import threading |
| |
| import rediscluster |
| from rediscluster import ClusterConnectionPool |
| import string, time |
| |
| startup_nodes = [ |
| {"host": "192.168.7.41", "port": 8001}, |
| ] |
| pool = ClusterConnectionPool(startup_nodes=startup_nodes, password="", max_connections=10, max_connections_per_node=10) |
| cli = rediscluster.RedisCluster(connection_pool=pool) |
| |
| |
| def test(): |
| for i in range(100): |
| cli.set(''.join(random.sample(string.ascii_letters, 10)), i, ex=1200) |
| |
| |
| ths = [threading.Thread(target=test) for n in range(10)] |
| for t in ths: |
| t.start() |
| for t in ths: |
| t.join() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步