更新时间:2023-11-06 GMT+08:00
连接DCS集群实例
from redis.cluster import RedisCluster as Redis from redis.cluster import ClusterNode redis_client = None def create_redis_client(context): logger = context.getLogger() redis_cluster_nodes = context.getUserData( "redis_ip_address") redis_password = context.getUserData( "redis_password") startup_nodes = [] for redis_node in redis_cluster_nodes.split(','): node_info = redis_node.split(":") startup_nodes.append(ClusterNode(host=node_info[0], port=node_info[1])) logger.info("redis startup_nodes={}".format(startup_nodes)) client = Redis(startup_nodes=startup_nodes, password=redis_password, cluster_error_retry_attempts=3, socket_timeout=2, socket_connect_timeout=2, health_check_interval=60, max_connections=50, decode_responses=True) return client def initializer(context): global redis_client redis_client = create_redis_client(context) redis_client.ping() def handler(event, context): logger = context.getLogger() redis_client.set('foo', 'bar') value = redis_client.get('foo') logger.info("redis get key foo value={}".format(value)) return value
参数 |
默认值 |
说明 |
---|---|---|
host |
localhost |
连接Redis实例的IP地址/域名 |
port |
6379 |
连接端口号 |
cluster_error_retry_attempts |
3 |
当遇到TimeoutError、ConnectionError或ClusterDownError时,在抛出异常之前重试的次数 |
其他参数配置可以参考表3。
父主题: 示例代码