/** * Fluhes all data * * @todo * @return boolean */ function flush() { if (is_object($this->_memcached)) { return $this->_memcached->flush_all(); } return false; }
public static function fetch($var) { if (self::support() == "apc") { if (self::apcSupport()) { return apc_fetch($var); } else { kernel::throwException("APC enabled but not available"); return false; } } elseif (self::support() == "memcached") { if (self::memcachedSupport) { return memcached::get($var); } else { kernel::throwException("Memcached enabled but not available"); return false; } } }
<?php include 'FiexiHash.php'; $hash = new FiexiHash(); $port = 11211; //普通的hash 分布 $servers = array(array('host' => '192.168.1.1', 'port' => $port), array('host' => '192.168.1.2', 'port' => $port)); $key = 'TheKey'; $value = 'TheValue'; $sc = $servers[$hash->mHash($key) % 2]; $memcached = new memcached($sc); $memcached->set($key, $value); //一致性的hash分布 $hash->addServer('192.168.1.1'); $hash->addServer('192.168.1.2'); $hash->addServer('192.168.1.3'); $hash->addServer('192.168.1.4'); $hash->addServer('192.168.1.5'); var_dump($hash->lookUp('key1')); var_dump($hash->lookUp('key2'));
/** * Connects to the memcache server set in the config */ private function connect() { if (self::$memcache == null) { self::$memcache = new MemCachedClient(array(CONFIG_MEMCACHE_HOST . ':' . CONFIG_MEMCACHE_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"); }
<?php /** * memcached diagnostic tool * * @todo document * @package MediaWiki * @subpackage Maintenance */ /** */ require_once "commandLine.inc"; require_once "memcached-client.php"; $mcc = new memcached(array('persistant' => true, 'debug' => true)); $mcc->set_servers($wgMemCachedServers); $mcc->set_debug(true); do { $bad = false; $quit = false; $line = readconsole("> "); if ($line === false) { exit; } $args = explode(" ", $line); $command = array_shift($args); switch ($command) { case "get": print "Getting {$args[0]}[{$args[1]}]\n"; $res = $mcc->get($args[0]); if (array_key_exists(1, $args)) { $res = $res[$args[1]]; }
<?php /** * memcached diagnostic tool * * @todo document * @addtogroup Maintenance */ /** */ require_once 'commandLine.inc'; $mcc = new memcached(array('persistant' => true)); $mcc->set_servers($wgMemCachedServers); #$mcc->set_debug( true ); function mccShowHelp($command) { if (!$command) { $command = 'fullhelp'; } $onlyone = true; switch ($command) { case 'fullhelp': // will show help for all commands $onlyone = false; case 'get': print "get: grabs something\n"; if ($onlyone) { break; } case 'getsock': print "getsock: lists sockets\n"; if ($onlyone) {
#!/usr/bin/php <?php // 10s memcached set 测试 $mem = new memcached(); $result = $mem->addServer('127.0.0.1', 11211); if (!$result) { die("Memcached连接失败;\n"); } $time_limit = 10; $time_start = time(); $time_length = 0; $i = 0; $error_num = 0; while ($time_length <= $time_limit) { $i++; $set_result = $mem->set('test:' . $i, time() . ' - ' . rand(1000, 10000)); if (!$set_result) { $error_num++; } $time_length = time() - $time_start; } echo "Total time : " . $time_limit . " s .\n"; echo "All : " . $i . "\n"; echo "Error : " . $error_num . "\n";
#!/usr/bin/php <?php // 10s memcached get 测试 $mem = new memcached(); $result = $mem->addServer('127.0.0.1', 11211); if (!$result) { die("Memcached连接失败;\n"); } $time_limit = 10; $time_start = time(); $time_length = 0; $i = 0; $error_num = 0; while ($time_length <= $time_limit) { $i++; $get_result = $mem->get('test:' . rand(1, 100000)); if (!$get_result) { $error_num++; } $time_length = time() - $time_start; } echo "Total time : " . $time_limit . " s .\n"; echo "All : " . $i . "\n"; echo "Error : " . $error_num . "\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();
/** * Writes the transmitted variable to IPC data. * Subclasses may never use an id < 2! * * @param mixed $data data which should be saved into IPC data * @param int $id int indicating the variable (bigger than 2!) * * @access public * @return boolean */ public function SetData($data, $id = 2) { return $this->memcached->set($this->type . ':' . $id, $data); }
<?php /** * memcached diagnostic tool * * @todo document * @package MediaWiki * @subpackage Maintenance */ /** */ require_once 'commandLine.inc'; require_once 'memcached-client.php'; $mcc = new memcached(array('persistant' => true)); $mcc->set_servers($wgMemCachedServers); #$mcc->set_debug( true ); function mccShowHelp($command) { if (!$command) { $command = 'fullhelp'; } $onlyone = true; switch ($command) { case 'fullhelp': // will show help for all commands $onlyone = false; case 'get': print "get: grabs something\n"; if ($onlyone) { break; } case 'getsock':
</select> </td> <td width=5%> <input type='submit' name='btnFilter' value='Filter'> </td> </form> </tr> </table> </div> <!-- Alerts Details section --> <?php require_once 'memcached-client.php'; global $memcache; $memcache = new memcached(array('servers' => array('127.0.0.1:11211'), 'debug' => false, 'compress_threshold' => 10240, 'persistant' => true)); $cache = $memcache->get('db0.example.com.sys_openports'); // print "db0.example.com.sys_openports -> " . $cache; $sql = "SELECT * FROM `tbl_node_type`"; $result = mysql_query($sql, $conn); $cats = array(); while ($row = mysql_fetch_array($result, MYSQL_NUM)) { if ($row[0] != 'any') { $cats[] = $row[0]; } } foreach ($cats as $i => $node_type) { $sql = "SELECT * FROM `tbl_check_type` WHERE `cht_node_type` = '" . $node_type . "' or `cht_node_type` = 'any'"; $result = mysql_query($sql, $conn); if (mysql_num_rows($result) > 0) { echo "<div class='type_header'>[{$node_type}]</div>";
/** * Checks to see if their session is still valid in memcache * @param * $session_id: The ID of the session we want to check * @return * An error string if memcache fails * A user ID if we find their session * A null if we don't find their session */ function check_session($session_id) { // If this person is logged in $user_id = memcached::get('medusa_sessionid_' . $session_id); if (is_numeric($user_id)) { // Refresh their session - if it fails, report it if (!memcached::set('medusa_sessionid_' . $session_id, $user_id)) { return 'Memcache Error: ' . memcached::report_last_error() . ' (memcached::set)'; } // Login still valid, tell them who the user is return $user_id; } else { // Login isn't valid, tell them we aren't a user return null; } }
function init_memcached() { global $CFG, $MCACHE; include_once $CFG->libdir . '/memcached.class.php'; $MCACHE = new memcached(); if ($MCACHE->status()) { return true; } unset($MCACHE); return false; }
function db_query($sql, &$res, $timeout, $reload) { global $memcached_host; $options = array('servers' => array($memcached_host), 'debug' => false, 'compress_threshold' => 510240, 'persistant' => false); if ($timeout < 11) { return db_query_mysql($sql, $res, $timeout, "0"); } $mc = new memcached($options); $key = md5($sql); $res = $mc->get($key); $mc->disconnect_all(); if (!$res) { return db_query_mysql($sql, $res, $timeout, "1", $key); //添加memcached } else { // echo "cached<br>"; if ($reload) { return db_query_mysql($sql, $res, $timeout, "2", $key); //替换memcached } } return true; }
#!/usr/bin/php <?php $mem = new memcached(); $result = $mem->addServer('127.0.0.1', 11211); if (!$result) { die("Memcached连接失败;\n"); } $set_result = $mem->set('test1', 'test1-info-value'); var_dump($set_result); if (!$set_result) { die("Memcached set 操作失败;\n"); } else { die("Memcached set 操作成功;\n"); }