成都网站建设设计

将想法与焦点和您一起共享

用python访问redis的几种常用方式

1,配置sentinel

创新互联建站主要从事成都网站设计、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务瓮安,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108

from redis.sentinel import Sentinel

sentinel = Sentinel([('192.168.0.210', 26379),('192.168.1.220', 26380),('192.168.1.220', 26379)], socket_timeout=0.1)
master = sentinel.master_for('mymaster',password='admin')
slave = sentinel.slave_for('mymaster',password='admin')
master.sadd('s4',1,2,3,4)
print master.smembers('s4')

2,直连 redis

pool = redis.ConnectionPool(host='127.0.0.1', port=6379, password='admin',db=0)
r = redis.Redis(connection_pool=pool)

3,连接 redis cluster

from rediscluster import StrictRedisCluster

redis_nodes = [{'host':'192.168.1.210','port':3790},
{'host':'192.168.1.210','port':3791},
{'host':'192.168.1.210','port':3793},
{'host':'192.168.1.220','port':3794},
{'host':'192.168.1.220','port':3795},
{'host':'192.168.1.220','port':3796}
]
redisconn = StrictRedisCluster(startup_nodes=redis_nodes,decode_responses=True,password='admin')


分享标题:用python访问redis的几种常用方式
分享路径:http://chengdu.cdxwcx.cn/article/iicojg.html