Python redis
跳到导航
跳到搜索
集群
# pip3 install redis-py-cluster from rediscluster import RedisCluster rnode = [ {"host": "192.168.0.229", "port": 6379}, {"host": "192.168.0.229", "port": 6380}, {"host": "192.168.0.148", "port": 6379}, {"host": "192.168.0.148", "port": 6380}, {"host": "192.168.0.249", "port": 6379}, {"host": "192.168.0.249", "port": 6380} ] rc = RedisCluster(startup_nodes=rnode, decode_responses=True) rc.get('test')
管道
Redis 管道技术可以在服务端未响应时,客户端可以继续向服务端发送请求,并最终一次性读取所有服务端的响应。
批量写入时,使用pipeline可以大幅度提升性能
# 使用 rp 代替 rc rp = rc.pipeline(transaction = False) # 足够数量的 rp.set 后,提交 FLAG = rp.execute()
单机
Redis 的单机使用在集群上时,如果读的数据不在连接主机上,会出现错误。
# pip3 install redis import redis rs = redis.Redis(host='192.168.0.229', port=6379, password=, db=0) # Conn Pool #rsp = redis.ConnectionPool(host='192.168.0.229', port=6379, password=, db=0) #rs = redis.Redis(connection_pool = rsp) # 有效时间 100 secs # rs.set('test', 'Hello, World!', 100) rs.set('test', 'Hello, World!') rs.get('test')