数据库
首页 > 数据库> > redis - python

redis - python

作者:互联网

pip install redis-py-cluster==1.3.6

# Redis实例创建及管道
# Note: 与python3一起使用时,decode_responses必须设置为True
# max_connections最大连接数,如果设20,那么在建立20个连接时会报错。
  
import redis, rediscluster
	
# 主从模式	
pool = redis.ConnectionPool(host=xxx, port=xxx, socket_timeout=xx, db=x, password=x, decode_responses=False)   # 多个redis实例共享一个连接池。
r = redis.StrictRedis(connection_pool=pool, decode_responses=False)   # redis.Redis是StrictRedis的子类,用于向后兼容旧版本的redis-py
pipe = r.pipeline(transaction=True)     # 默认执行每次请求都会创建(pool申请连接)和断开(归还pool)操作。用pipline实现一次请求指定多个命令.
pipe.set('key', 'value')
pipe.execute()   # 批量,数量限制?
	
# redis集群模式
rc = rediscluster_xx.StrictRedisCluster(cluster=XX, db=XX, fail_count_to_reload=xx, fail_time_to_reload=xx)  # ???
rc = rediscluster.StrictRedisCluster(startup_nodes=[{"host": "xx", "port": xx}], skip_full_coverage_check=True, socket_timeout=timeout, socket_connect_timeout=timeout)
rc = rediscluster.StrictRedisCluster(host='xx', port=xx, socket_timeout=5, skip_full_coverage_check=True)
{
    # 连接池
    from rediscluster.connection import *
    pool = ClusterConnectionPool(startup_nodes=[{"host": "xx", "port": xx}], password="*", socket_timeout=5, db=0, decode_responses=False, skip_full_coverage_check=True)   # connection_class=SSLClusterConnection
    r =rediscluster.StrictRedisCluster(connection_pool=pool)
}	

标签:python,redis,rediscluster,xx,timeout,True,pool
来源: https://www.cnblogs.com/bsszds930/p/13033372.html