/** * Memcached constructor. * @param Cache $cache * @param string $host * @param int $port * @throws EngineException */ public function __construct(Cache $cache, string $host, int $port = 11211) { // Check if we have Memcached PECL if (!extension_loaded("memcached")) { throw EngineException::prerequisite(__CLASS__, 'Required extension/PECL "memcached" is not installed'); } $this->cache = $cache; $this->memcached = new \Memcached(); // \Memcached::OPT_BINARY_PROTOCOL is necessary for increment/decrement methods to work as expected $this->memcached->setOption(\Memcached::OPT_BINARY_PROTOCOL, true); if (!$this->memcached->addServer($host, $port)) { throw EngineException::connectionError(__CLASS__, sprintf('Failed to add "%1$s:%2$d" as server', $host, $port)); } // Save host and port $this->host = $host; $this->port = $port; // Connected? if (!$this->isConnected()) { throw EngineException::connectionError(__CLASS__, sprintf('Failed to connect with MEMCACHED server on "%1$s:%2$d"', $host, $port)); } }