/** * Sets data * * @param string $key * @param mixed $var * @param integer $expire * @return boolean */ function set($key, $var, $expire = 0) { if (is_object($this->_memcached)) { return $this->_memcached->set($key, $var, $expire); } return false; }
public static function store($var, $value) { if (self::support() == "apc") { if (self::apcSupport()) { apc_store($var, $value, kernel::Configuration("cacheSaveTime")); return apc_fetch($var); } else { kernel::throwException("APC enabled but not available"); return false; } } elseif (self::support() == "memcached") { if (self::memcachedSupport) { memcached::set($var, $value); 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'));
var_dump($res); } break; case "getsock": $res = $mcc->get($args[0]); $sock = $mcc->get_sock($args[0]); var_dump($sock); break; case "set": $key = array_shift($args); if ($args[0] == "#" && is_numeric($args[1])) { $value = str_repeat("*", $args[1]); } else { $value = implode(" ", $args); } if (!$mcc->set($key, $value, 0)) { #print 'Error: ' . $mcc->error_string() . "\n"; print "MemCached error\n"; } break; case "delete": $key = implode(" ", $args); if (!$mcc->delete($key)) { #print 'Error: ' . $mcc->error_string() . "\n"; print "MemCached error\n"; } break; case "dumpmcc": var_dump($mcc); break; case "quit":
#!/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";
/** * 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); }
/** * 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; } }
#!/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"); }