type = $config[0]['type'] ?: 'memcached'; try { $this->memcache = new \Memcached; $this->memcache->setOption(\Memcached::OPT_COMPRESSION, true); $this->memcache->setOption(\Memcached::OPT_DISTRIBUTION, true); $this->memcache->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true); $this->memcache->setOption(\Memcached::OPT_NO_BLOCK, true); $this->memcache->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 50); $this->memcache->setOption(\Memcached::OPT_POLL_TIMEOUT, 50); $this->memcache->addServer($host, $port); } catch (Exception $ex) { $message = "Memcached connection failed : [" . $this->type . ';' . $host . ';' . $port . ']' . $ex->getMessage(); throw new Exception($message, 90301); } } public static function instance() { if (!isset(self::$instance)) { try { $instance = new self(); } catch (Exception $e) { } self::$instance = $instance; } else { $instance = self::$instance; } return $instance; } public function __destruct() { if ($this->type != 'memcached') { $this->memcache->close(); } } public function get($key) { if (empty($key)) { return ''; } else { return $this->memcache->get($key); } } public function delete($key, $time = 0) { return $this->memcache->delete($key, $time); } public function set($key, $data, $life_time_limit = 0, $memcache_compressed = 2) { if ($this->type == 'memcached') { return $this->memcache->set($key, $data, $life_time_limit); } else { return $this->memcache->set($key, $data, $memcache_compressed, $life_time_limit); } } public function replace($key, $data, $memcache_compressed = 2, $life_time_limit = 0) { if ($this->type == 'memcached') { return $this->memcache->replace($key, $data, $life_time_limit); } else { return $this->memcache->replace($key, $data, $memcache_compressed, $life_time_limit); } } public function increment($key, $value = 1) { return $this->memcache->increment($key, $value); } public function decrement($key, $value = 1) { return $this->memcache->decrement($key, $value); } public function flush() { return $this->memcache->flush(); } }