conn = new \Memcached(); // This call does not actually connect to server yet if (!$this->conn->addServer($host, $port)) { throw new \Exception('Unable to add memcached server'); } } public function get(string $key, $default = null) { $value = $this->conn->get($key); if ($value === false) { return $default; } return $value; } public function set(string $key, $value, $ttl = null): void { $expiration = $ttl === null ? 0 : time() + $ttl; $result = $this->conn->set($key, $value, $expiration); if ($result === false) { Logger::warning('Failed to store an item in memcached', [ 'key' => $key, 'code' => $this->conn->getLastErrorCode(), 'message' => $this->conn->getLastErrorMessage(), 'number' => $this->conn->getLastErrorErrno(), ]); // Intentionally not throwing an exception } } public function delete(string $key): void { $this->conn->delete($key); } public function clear(): void { $this->conn->flush(); } public function prune(): void { // memcached manages pruning on its own } }