• redis python操作api


    redis单例数据库

     1 #redis单例连接
     2 '''
     3 redis-server redis_diy.conf
     4 '''
     5 import redis
     6 conn=redis.StrictRedis(host='192.168.160.135',port='6380',db=0,password='123456')#redis配置文件,db指定redis数据库的隔离区,有0-15共16个,默认为0
     7 print(conn.dbsize())
     8 print(conn.keys())
     9 conn.set('age',18)
    10 print(conn.keys())
    11 print(conn.get('name'))
    12 print(conn.exists('name'))
    redis单例数据库

    redis-sentinel(哨兵)

     1 #redis-sentinel连接
     2 '''
     3 启动redis服务端(6390主库):
     4     redis-server msconf/redis-6390.conf
     5     redis-server msconf/redis-6391.conf
     6     redis-server msconf/redis-6392.conf
     7     redis-server msconf/redis-6393.conf
     8 启动哨兵功能:
     9     redis-sentinel redis_sentinel/redis-sentinel_26390.conf
    10     redis-sentinel redis_sentinel/redis-sentinel_26391.conf
    11     redis-sentinel redis_sentinel/redis-sentinel_26392.conf
    12 '''
    13 
    14 ## 导入redis sentinel包
    15 from redis.sentinel import Sentinel
    16 #指定sentinel的地址和端口号(连接哨兵)
    17 sentinel=Sentinel([('192.168.160.135',26390),('192.168.160.135',26391),('192.168.160.135',26392)],socket_timeout=0.1)
    18 #测试,获取以下主库和从库的信息:mymaster是哨兵配置文件中的指定的监控主库
    19 print(sentinel.discover_master('mymaster'))#主库信息
    20 print(sentinel.discover_slaves('mymaster'))#从库信息
    21 
    22 ##配置读写分离
    23 #写节点:主库
    24 master=sentinel.master_for('mymaster',password=123456,socket_timeout=0.1)#注意主库设置了安全模式有密码
    25 print(master)
    26 #读节点:从库
    27 slave=sentinel.slave_for('mymaster',password=123456,socket_timeout=0.1)#注意从库设置了安全模式有密码
    28 print(slave)
    29 
    30 #读写分离测试
    31 print(master.dbsize())
    32 print(master.keys())
    33 print(slave.keys())
    34 master.set('name4',4)
    35 print(slave.keys())
    36 print(slave.get('name4'))
    redis-sentinel(哨兵)

    redis-cluster(集群)

     1 # redis-cluster集群:https://github.com/Grokzen/redis-py-cluster
     2 # pip install redis-py-cluster
     3 from rediscluster import StrictRedisCluster
     4 #是少要链接集群中的一台数据库,可以是多台,防止有的数据库会挂掉
     5 startup_nodes = [{"host": "192.168.160.135", "port": "7000"}, {"host": "192.168.160.135", "port": "7001"},
     6                  {"host": "192.168.160.135", "port": "7002"}, {"host": "192.168.160.135", "port": "7003"},
     7                  {"host": "192.168.160.135", "port": "7004"}, {"host": "192.168.160.135", "port": "7005"} ]
     8 rc = StrictRedisCluster(startup_nodes=startup_nodes,password=123456, decode_responses=True)
     9 rc.set('name1',1234)
    10 print(rc.keys())
    11 print(rc.dbsize())
    12 print(rc.get('name1'))
    redis-cluster(集群)
  • 相关阅读:
    Scheduled定时任务的触发规则
    linux设置系统时间为当前网络时间
    idea控制台打印日志出现乱码
    Nginx配置高可用的集群
    Nginx实现负载均衡
    BigDecimal比较大小
    Java调用第三方系统接口获取数据
    sql中主要关键字的执行顺序
    ASP.NET.Core --Jenkins+Docker
    ASP.NET.Core --Swagger+Route
  • 原文地址:https://www.cnblogs.com/open-yang/p/11256449.html
Copyright © 2020-2023  润新知