def open(self, correlation_id: Optional[str]): """ Opens the component. :param correlation_id: (optional) transaction id to trace execution through call chain. """ connections = self.__connection_resolver.resolve_all(correlation_id) if len(connections) == 0: raise ConfigException(correlation_id, 'NO_CONNECTION', 'Connection is not configured') servers: List[str] = [] for connection in connections: host = connection.get_host() port = connection.get_port() or 11211 servers.append(f'{host}:{port}') options = { # TODO: this options have not support by driver, but can execute by cmd driver method # 'maxKeySize': self.__max_key_size, # 'maxExpiration': self.__max_expiration, # 'maxValue': self.__max_value, # driver don't have this config # 'retries': self.__retries, # 'remove': self.__remove, 'retry_attempts': self. __failures, # driver automatically remove dead servers from the pool (by attemps) 'max_pool_size': self.__pool_size, 'connect_timeout': self.__reconnect / 1000, 'timeout': self.__timeout / 1000, 'retry_timeout': self.__retry / 1000, 'pool_idle_timeout': self.__idle / 1000, 'default_noreply': False } self.__client = pymemcache.HashClient(servers=servers, **options)
def from_environment(cls, max_pool_size=4): """Generate a ``pymemcache.Client`` from an environment variable. This class method looks for the ``MEMCACHED_HOSTS`` environment variable and, if it is set, parses the value as a space delimited list of hostnames, optionally with ports. For example: "localhost" "localhost:11211" "1.1.1.1:11211 2.2.2.2:11211 3.3.3.3:11211" Returns: Optional[MemcacheCache]: A :class:`MemcacheCache` instance or :data:`None`, if ``MEMCACHED_HOSTS`` is not set in the environment. """ hosts_string = os.environ.get("MEMCACHED_HOSTS") if not hosts_string: return None hosts = [ cls._parse_host_string(host_string.strip()) for host_string in hosts_string.split() ] if not max_pool_size: max_pool_size = 1 if len(hosts) == 1: client = pymemcache.PooledClient(hosts[0], max_pool_size=max_pool_size) else: client = pymemcache.HashClient(hosts, use_pooling=True, max_pool_size=max_pool_size) return cls(client)
def from_environment(cls, max_pool_size=4, strict_read=False, strict_write=True): """Generate a ``pymemcache.Client`` from an environment variable. This class method looks for the ``MEMCACHED_HOSTS`` environment variable and, if it is set, parses the value as a space delimited list of hostnames, optionally with ports. For example: "localhost" "localhost:11211" "1.1.1.1:11211 2.2.2.2:11211 3.3.3.3:11211" Args: max_pool_size (int): Size of connection pool to be used by client. If set to ``0`` or ``1``, connection pooling will not be used. Default: ``4`` strict_read (bool): If :data:`False`, connection errors during read operations will be logged with a warning and treated as cache misses, but will not raise an exception in the application, with connection errors during reads being treated as cache misses. If :data:`True`, connection errors will be raised as exceptions in the application. Default: :data:`False`. strict_write (bool): If :data:`False`, connection errors during write operations will be logged with a warning, but will not raise an exception in the application. If :data:`True`, connection errors during write will be raised as exceptions in the application. Because write operations involve cache invalidation, setting this to :data:`False` may allow other clients to retrieve stale data from the cache. If there is a connection error, an internal flag will be set to clear the cache the next time any method is called on this object, to try and minimize the opportunity for clients to read stale data from the cache. Default: :data:`True`. Returns: Optional[MemcacheCache]: A :class:`MemcacheCache` instance or :data:`None`, if ``MEMCACHED_HOSTS`` is not set in the environment. """ hosts_string = os.environ.get("MEMCACHED_HOSTS") if not hosts_string: return None hosts = [ cls._parse_host_string(host_string.strip()) for host_string in hosts_string.split() ] if not max_pool_size: max_pool_size = 1 if len(hosts) == 1: client = pymemcache.PooledClient(hosts[0], max_pool_size=max_pool_size) else: client = pymemcache.HashClient(hosts, use_pooling=True, max_pool_size=max_pool_size) return cls(client, strict_read=strict_read, strict_write=strict_write)