instance === null) { $this->init(); } return $this->instance; } /** @psalm-assert \Memcached $this->instance */ public function init(): void { $this->instance = new \Memcached(); $defaultOptions = [ \Memcached::OPT_CONNECT_TIMEOUT => 50, \Memcached::OPT_RETRY_TIMEOUT => 50, \Memcached::OPT_SEND_TIMEOUT => 50, \Memcached::OPT_RECV_TIMEOUT => 50, \Memcached::OPT_POLL_TIMEOUT => 50, // Enable compression \Memcached::OPT_COMPRESSION => true, // Turn on consistent hashing \Memcached::OPT_LIBKETAMA_COMPATIBLE => true, // Enable Binary Protocol \Memcached::OPT_BINARY_PROTOCOL => true, ]; /** * By default enable igbinary serializer if available * * Psalm checks depend on if igbinary is installed or not with memcached * @psalm-suppress RedundantCondition * @psalm-suppress TypeDoesNotContainType */ if (\Memcached::HAVE_IGBINARY) { $defaultOptions[\Memcached::OPT_SERIALIZER] = \Memcached::SERIALIZER_IGBINARY; } $options = Server::get(IConfig::class)->getSystemValue('memcached_options', []); if (is_array($options)) { $options = $options + $defaultOptions; $this->instance->setOptions($options); } else { throw new HintException("Expected 'memcached_options' config to be an array, got $options"); } $servers = Server::get(SystemConfig::class)->getValue('memcached_servers'); if (!$servers) { $server = Server::get(SystemConfig::class)->getValue('memcached_server'); if ($server) { $servers = [$server]; } else { $servers = [['localhost', 11211]]; } } $this->instance->addServers($servers); } }