ched($key);
112 return $m->get($key);
113 }
114
115
116 }
117
118 ?>
3)、测试
1、查看所有虚拟节点
一共64*3=132个虚拟节点(虚拟节点设置还是属于偏少的,一般都会设置在100~200)
2、set测试
1 include ‘./ConsistentHashMemcache.class.php‘;
2 header("content-type: text/html;charset=utf8;");
3 $arr=array(
4 ‘node1‘=>array(‘host‘=>‘192.168.95.11‘, ‘port‘=>‘11210‘),
5 ‘node2‘=>array(‘host‘=>‘192.168.95.11‘, ‘port‘=>‘11211‘),
6 ‘node3‘=>array(‘host‘=>‘192.168.95.11‘, ‘port‘=>‘11212‘),
7 );
8
9 $c=new ConsistentHashMemcache($arr);
10
11 #测试set
12 $c->setKey(‘aaa‘, ‘11111‘);
13 $c->setKey(‘bbb‘, ‘22222‘);
14 $c->setKey(‘ccc‘, ‘33333‘);
分别telnet连接192.168.95.11:(11210、11211、11212)
在节点node1中get(‘aaa’)、get(‘bbb’)能取到值
在节点node3中get(‘ccc’)能取到值
3、get测试
1 include ‘./ConsistentHashMemcache.class.php‘;
2 header("content-type: text/html;charset=utf8;");
3 $arr=array(
4 ‘node1‘=>array(‘host‘=>‘192.168.95.11‘, ‘port‘=>‘11210‘),
5 ‘node2‘=>array(‘host‘=>‘192.168.95.11‘, ‘port‘=>‘11211‘),
6 ‘node3‘=>array(‘host‘=>‘192.168.95.11‘, ‘port‘=>‘11212‘),
7 );
8
9 $c=new ConsistentHashMemcache($arr);
10 #测试get
11 echo $c->getKey(‘aaa‘).‘<br/>‘;
12 echo $c->getKey(‘bbb‘).‘<br/>‘;
13 echo $c->getKey(‘ccc‘).‘<br/>‘;
4、优缺点
相对于取模方式分布式,一致性哈希方式分布式的代码复杂性要高一点,但这也在可以接受的范围内,不构成任何阻碍问题。相反它的优点就非常显著,通过虚拟节点的方式实现,可以使不可控的存储节点能够尽可能的均匀分布在圆环上,从而达到数据均匀缓存在各个主机里。其次增加与删除虚拟节点对于之前缓存的整体数据影响非常小。