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()))

  

posted @   AlamZ  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示