python redis 操作
目的:
验证redis 集群模式,高可用场景
insert.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | from rediscluster import ClusterBlockingConnectionPool, RedisCluster, ClusterConnectionPool def init(): startup_nodes = [ { 'host' : '10.12.16.50' , 'port' : 6379 }, { 'host' : '10.12.16.50' , 'port' : 6380 }, { 'host' : '10.12.16.51' , 'port' : 6379 }, { 'host' : '10.12.16.51' , 'port' : 6380 }, { 'host' : '10.12.16.52' , 'port' : 6379 }, { 'host' : '10.12.16.52' , 'port' : 6380 }] # 构建连接池 pool = ClusterConnectionPool(startup_nodes = startup_nodes) # 创建redis集群客户端 redis_client = RedisCluster(connection_pool = pool) print ( "redis集群连接成功!!!" ) return redis_client def insert(key, i): redis_client = init() # 数据写入测试 redis_client.lpush(key, i) print ( "{}-->{}" . format (key,i)) # print("测试数据写入成功!!!") def delete(key): redis_client = init() # 数据写入测试 redis_client.delete(key) print ( "{}-->{}" . format ( "测试数据" , "删除成功" )) if __name__ = = "__main__" : key = "alamTest" delete(key) for i in range ( 1 , 1000 ): import time time.sleep( 1 ) insert(key,i) print ( "-----time={}" . format (time.asctime())) |
select.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | from rediscluster import ClusterBlockingConnectionPool, RedisCluster, ClusterConnectionPool def init(): startup_nodes = [ { 'host' : '10.12.16.50' , 'port' : 6379 }, { 'host' : '10.12.16.50' , 'port' : 6380 }, { 'host' : '10.12.16.51' , 'port' : 6379 }, { 'host' : '10.12.16.51' , 'port' : 6380 }, { 'host' : '10.12.16.52' , 'port' : 6379 }, { 'host' : '10.12.16.52' , 'port' : 6380 }] # 构建连接池 pool = ClusterConnectionPool(startup_nodes = startup_nodes) # 创建redis集群客户端 redis_client = RedisCluster(connection_pool = pool) # print("redis集群连接成功!!!") return redis_client def select(keys = "alamTest" ): redis_client = init() # 数据写入测试 count = redis_client.llen(keys) print ( "{}-->{}" . format (keys,count)) # print("测试数据查看成功!!!") if __name__ = = "__main__" : while 1 : import time time.sleep( 1 ) select() print ( "-----time={}" . format (time.asctime())) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)