Exemplo n.º 1
0
def connect_to_redis_cluster(redis_host, redis_port): # pragma: no cover
        """Connect To Redis Cluster.
        Arguments:
            - redis_host: hostname or IP address of the Redis server.
            - redis_port: Redis port number (default=6379)
        Return redis connection object
        Examples:
        | ${redis_conn}=   | Connect To Redis Cluster |  redis-dev.com | 6379 |
        """
        startup_nodes = [{"host":redis_host, "port":redis_port}]
        try:
            redis_conn = redis.StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True, skip_full_coverage_check=True)
        except Exception as ex:
            logger.error(str(ex))
            raise Exception(str(ex))
        return redis_conn
Exemplo n.º 2
0
    def set_db(self, **kwargs):
        default_setting_dict = {
            "host": "localhost",
            "port": 6379,
            "use_redis_cluster": False,
            "redis_db_num": 0,
            "key_hashing": None,
            "use_redis_hash": False,
            "key_delimiter": "/",
            "use_connection_pool": False,
            "ssl": False,
            "password": None
        }

        try:
            with open(kwargs['setting_file_path']) as f:
                setting_dict = json.load(f)
                # log.debug('setting_dict from json:{}'.format(setting_dict))
        except Exception as e:
            log.error(e)
            setting_dict = kwargs

        # check args
        for param, default_value in default_setting_dict.items():
            if not param in setting_dict: setting_dict[param] = default_value

        # log.debug('setting_dict:{}'.format(setting_dict))
        # password connection only with ssl

        self._host = setting_dict['host']
        self._port = setting_dict['port']
        self._redis_db_num = setting_dict['redis_db_num']
        self._key_hashing = setting_dict['key_hashing']
        self._use_redis_hash = setting_dict['use_redis_hash']
        self._key_delimiter = setting_dict['key_delimiter']
        self._use_connection_pool = setting_dict['use_connection_pool']
        log.info('use_redis_cluster:{}'.format(
            setting_dict['use_redis_cluster']))
        if setting_dict['use_redis_cluster']:
            # check cluster setting
            from rediscluster.exceptions import RedisClusterException
            try:
                from rediscluster import StrictRedisCluster
                self._db_ins = redis.StrictRedisCluster(
                    startup_nodes=setting_dict['startup_nodes'],
                    password=setting_dict['password'],
                    skip_full_coverage_check=True)  # for RedisCluster
                log.info(
                    'StrictRedisCluster is initialized with startup_nodes:{}'.
                    format(setting_dict['startup_nodes']))
                return
            except KeyError as e:
                log.error(e)
            except RedisClusterException as e:
                log.error(e)

        if setting_dict['use_connection_pool'] is not None and setting_dict[
                'use_connection_pool']:
            connection_class = redis.SSLConnection if setting_dict[
                'ssl'] else redis.connection.Connection
            self._connection_pool = redis.ConnectionPool(
                host=setting_dict['host'],
                port=setting_dict['port'],
                db=setting_dict['redis_db_num'],
                connection_class=connection_class,
                password=setting_dict['password'])
            self._db_ins = redis.StrictRedis(
                connection_pool=self._connection_pool, ssl=setting_dict['ssl'])
            log.info('StrictRedis is initialized with ConnectionPool')
        else:
            self._db_ins = redis.StrictRedis(host=setting_dict['host'],
                                             port=setting_dict['port'],
                                             db=setting_dict['redis_db_num'],
                                             ssl=setting_dict['ssl'],
                                             password=setting_dict['password'])
            log.info('StrictRedis is initialized')