Get and memoize connections to instances

def _get_connection(self, key, host='', port='', user='', password='', database_url='', tags=None, use_cached=True):
        "Get and memoize connections to instances"
        if key in self.dbs and use_cached:
            return self.dbs[key]
        try:
            connect_kwargs = self._get_connect_kwargs(
                host=host, port=port, user=user, password=password, database_url=database_url
            )

            connection = pg.connect(**connect_kwargs)
            connection.set_isolation_level(pg.extensions.ISOLATION_LEVEL_AUTOCOMMIT)

        # re-raise the CheckExceptions raised by _get_connect_kwargs()
        except CheckException:
            raise

        except Exception:
            redacted_url = self._get_redacted_dsn(host, port, user, database_url)
            message = u'Cannot establish connection to {}'.format(redacted_url)

            self.service_check(
                self.SERVICE_CHECK_NAME,
                AgentCheck.CRITICAL,
                tags=self._get_service_checks_tags(host, port, database_url, tags),
                message=message,
            )
            raise

        self.dbs[key] = connection
        return connection