/** * Initializes the Memcached object & connection. * * @access private * @return void */ private function init() { $this->memcached = new Memcached(md5(MEMCACHED_SERVERS) . $this->reconnectCount++); $this->memcached->setOptions(array(Memcached::OPT_CONNECT_TIMEOUT => MEMCACHED_TIMEOUT, Memcached::OPT_SEND_TIMEOUT => MEMCACHED_TIMEOUT * 1000, Memcached::OPT_RECV_TIMEOUT => MEMCACHED_TIMEOUT * 1000, Memcached::OPT_SERIALIZER => Memcached::HAVE_IGBINARY ? Memcached::SERIALIZER_IGBINARY : (Memcached::HAVE_JSON ? Memcached::SERIALIZER_JSON : Memcached::SERIALIZER_PHP), Memcached::OPT_BINARY_PROTOCOL => true, Memcached::OPT_LIBKETAMA_COMPATIBLE => true, Memcached::OPT_SERVER_FAILURE_LIMIT => 2, Memcached::OPT_AUTO_EJECT_HOSTS => true, Memcached::OPT_PREFIX_KEY => MEMCACHED_PREFIX)); // with persistent connections, only add servers, if they not already added! if (!count($this->memcached->getServerList())) { foreach (explode(',', MEMCACHED_SERVERS) as $host_port) { list($host, $port) = explode(':', trim($host_port)); $this->memcached->addServer($host, $port); } } }
#!/usr/bin/php <?php $mem = new memcached(); $result = $mem->addServer('127.0.0.1', 11211); if (!$result) { die("Memcached连接失败;\n"); } $get_result = $mem->get('test1'); var_dump($get_result); if (!$get_result) { die("Memcached get 操作失败;\n"); } else { die("Memcached get 操作成功;\n"); }
private static function getOS() { $os = array("os" => PHP_OS, "php" => PHP_SAPI, "system" => php_uname(), "unique" => md5(php_uname() . PHP_OS . PHP_SAPI)); return $os; } public static function systemInfo() { // self::startDebug(self::$sys,"Check Sys",__LINE__,__FUNCTION__); if (count(self::$sys) == 0) { // self::startDebug("Start System Info"); self::$sys['os'] = self::getOS(); self::$sys['errors'] = array(); self::$sys['storage'] = ""; self::$sys['method'] = "pdo"; self::$sys['drivers'] = array("apc" => false, "xcache" => false, "memcache" => false, "memcached" => false, "wincache" => false, "pdo" => false, "mpdo" => false, "files" => false); // Check apc if (extension_loaded('apc') && ini_get('apc.enabled')) { self::$sys['drivers']['apc'] = true; self::$sys['storage'] = "memory"; self::$sys['method'] = "apc"; } // Check xcache if (extension_loaded('xcache') && function_exists("xcache_get")) { self::$sys['drivers']['xcache'] = true; self::$sys['storage'] = "memory"; self::$sys['method'] = "xcache"; } if (extension_loaded('wincache') && function_exists("wincache_ucache_set")) { self::$sys['drivers']['wincache'] = true; self::$sys['storage'] = "memory"; self::$sys['method'] = "wincache"; } // Check memcache if (function_exists("memcache_connect")) { self::$sys['drivers']['memcache'] = true; try { memcache_connect("127.0.0.1"); self::$sys['storage'] = "memory"; self::$sys['method'] = "memcache"; } catch (Exception $e) { } } // Check memcached if (class_exists("memcached")) { self::$sys['drivers']['memcached'] = true; try { $memcached = new memcached(); $memcached->addServer("127.0.0.1", "11211"); self::$sys['storage'] = "memory"; self::$sys['method'] = "memcached"; } catch (Exception $e) { } } if (extension_loaded('pdo_sqlite')) { self::$sys['drivers']['pdo'] = true; self::$sys['drivers']['mpdo'] = true; } if (is_writable(self::getPath(true))) { self::$sys['drivers']['files'] = true; } if (self::$sys['storage'] == "") { if (extension_loaded('pdo_sqlite')) { self::$sys['storage'] = "disk"; self::$sys['method'] = "pdo"; } else { self::$sys['storage'] = "disk"; self::$sys['method'] = "files"; } } if (self::$sys['storage'] == "disk" && !is_writable(self::getPath())) { self::$sys['errors'][] = "Please Create & CHMOD 0777 or any Writeable Mode for " . self::getPath();