/** * Returns data * * @param string $key * @return mixed */ function get($key) { if (is_object($this->_memcached)) { return $this->_memcached->get($key); } 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; } } }
#!/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"); }
$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]]; } if ($res === false) { #print 'Error: ' . $mcc->error_string() . "\n"; print "MemCached error\n"; } elseif (is_string($res)) { print "{$res}\n"; } else { var_dump($res); } break; case "getsock": $res = $mcc->get($args[0]); $sock = $mcc->get_sock($args[0]);
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 // 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";
/** * Returns the requested variable from IPC data. * * @param int $id int indicating the variable * * @access public * @return mixed */ public function GetData($id = 2) { return $this->memcached->get($this->type . ':' . $id); }
</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 = array();
/** * 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; } }