<?php
echo '<pre>'; //测试的键值的数量
$count = 30;
$mem = create_memcache();
//var_dump($mem->isPersistent());
//var_dump($mem->isPristine());
//flush_data($mem);
//test_consistent_hash_set($mem, $count);
//test_consistent_hash_get($mem, $count);
//var_dump($mem->getServerList());
//var_dump($mem->getStats());
//检测memcache 权重匹配实现
//get_server_key_statistic(); function get_server_key_statistic() {
//memcahed 分布式
//测试 memcache 一致性hash 一台 memcahed 服务器宕机之后其他的键值存储问题还有添加一台服务器之后对之前的数据影响。
$stas = array();
for ($i = 1; $i <= 30; $i++) {
$key = 'item' . $i;
$arr = $mem->getServerByKey($key);
$port = $arr['port'];
if (!isset($stas[$port])) {
$stas[$port] = 1;
} else {
$stas[$port] = ++$stas[$port];
}
$server_key = $port . 'server_key';
$stas[$server_key][] = $key;
}
print_r($stas);
} /**
* 生成么memcache 对象
*/
function create_memcache() {
$mem = new Memcached('demo');
$server = array(
array('127.0.0.1', 11211, 20),
array('127.0.0.1', 11212, 20),
array('127.0.0.1', 11213, 20),
array('127.0.0.1', 11214, 20),
array('127.0.0.1', 11215, 20),
);
if (!count($mem->getServerList())) {
$mem->addServers($server);
//Socket reading timeout, in microseconds. In cases where you cannot use non-blocking I/O this will allow you to still have timeouts on the reading of data.
$mem->setOption(Memcached::OPT_RECV_TIMEOUT, 1000);
$mem->setOption(Memcached::OPT_SEND_TIMEOUT, 1000);
$mem->setOption(Memcached::OPT_TCP_NODELAY, true);
$mem->setOption(Memcached::OPT_SERVER_FAILURE_LIMIT, 50);
$mem->setOption(Memcached::OPT_CONNECT_TIMEOUT, 500);
$mem->setOption(Memcached::OPT_RETRY_TIMEOUT, 300);
$mem->setOption(Memcached::OPT_REMOVE_FAILED_SERVERS, true);
$mem->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
$mem->setOption(Memcached::OPT_HASH, Memcached::HASH_CRC);
$mem->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
}
return $mem;
} /**
* 测试一致性hash
*/
function test_consistent_hash_set($mem, $count) {
//memcahed 分布式
//测试 memcache 一致性hash 一台 memcahed 服务器宕机之后其他的键值存储问题还有添加一台服务器之后对之前的数据影响。
$stats = array();
for ($i = 1; $i <= $count; $i++) {
$server_key = 'item' . $i;
$arr = $mem->getServerByKey($server_key);
$val = create_rand_str(6);
$key = $i;
$mem->setByKey($server_key, $key, $val);
$port = $arr['port'];
if (!isset($stats[$port])) {
$stats[$port] = 1;
} else {
$stats[$port] = ++$stats[$port];
}
$server_key_val = $port . 'server_info';
$val_key_val = $port . 'val_info';
$stats[$server_key_val][] = $server_key;
$md5_key = md5($key);
$stats[$val_key_val][] = "{$md5_key}=>{$val}";
}
print_r($stats);
} function create_rand_str($length) {
$randstr = '';
for ($i = 0; $i < $length; $i++) {
$randstr .= chr(mt_rand(33, 126));
}
return $randstr;
} /**
* 测试memcache get
*/
function test_consistent_hash_get($mem, $count) {
for ($i = 1; $i <= $count; $i++) {
$server_key = 'item' . $i;
$key = $i;
$result = $mem->getByKey($server_key, $key);
$server_info = $mem->getServerByKey($server_key);
var_dump($server_info);
exit;
$port = $server_info['port'];
$weight = $server_info['weight'];
if ($result !== FALSE) {
echo "server info : 服务器端口:{$port} 权重:{$weight}, {$server_key}——{$key}=>{$result}<br>";
}
}
} /**
* 清除数据
*/
function flush_data($mem) {
$mem->flush();
}
05-11 08:21