memcached 一致性hash原理

memcache 是一个分布式的缓存系统,但是本身没有提供集群功能,在大型应用的情况下容易成为瓶颈。但是客户端这个时候可以自由扩展,分两阶段实现。第一阶段:key 要先根据一定的算法映射到一台memcache服务器。第二阶段从服务器中取出缓存的值。但是有一个问题,比如其中一台服务器挂了,或者需要增加一台服务 的时候,这个时候第一阶段的算法就很重要了,怎样使得原来的数据尽可能的继续有效,减少扩展节点或缩减节点带来的冲击。下面列出想到一些解决方法:

一:hash一致性算法:

优点:

当一个节点失效的时候,其他节点的数据不会受到破坏,这个节点的数据会被分流到另外一个节点。当增加一个节点时,只会对一个节点的一分部数据有影响。

缺点:

 极容易造成节点间数据量的不平衡,可能一个节点上热点非常多,一个节点上热点很少。

下面是具体介绍:(转自:http://blog.csdn.net/sparkliang/archive/2010/02/02/5279393.aspx

consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在 cache 系统中应用越来越广泛;

1 基本场景

比如你有 N 个 cache 服务器(后面简称 cache ),那么如何将一个对象 object 映射到 N 个 cache 上呢,你很可能会采用类似下面的通用方法计算 object 的 hash 值,然后均匀的映射到到 N 个 cache ;

hash(object)%N

一切都运行正常,再考虑如下的两种情况;

1 一个 cache 服务器 m down 掉了(在实际应用中必须要考虑这种情况),这样所有映射到 cache m 的对象都会失效,怎么办,需要把 cache m 从 cache 中移除,这时候 cache 是 N-1 台,映射公式变成了 hash(object)%(N-1) ;

2 由于访问加重,需要添加 cache ,这时候 cache 是 N+1 台,映射公式变成了 hash(object)%(N+1) ;

1 和 2 意味着什么?这意味着突然之间几乎所有的 cache 都失效了。对于服务器而言,这是一场灾难,洪水般的访问都会直接冲向后台服务器;

再来考虑第三个问题,由于硬件能力越来越强,你可能想让后面添加的节点多做点活,显然上面的 hash 算法也做不到。

  有什么方法可以改变这个状况呢,这就是 consistent hashing...

2 hash 算法和单调性

         Hash 算法的一个衡量指标是单调性( Monotonicity ),定义如下:

        单调性是指如果已经有一些内容通过哈希分派到了相应的缓冲中,又有新的缓冲加入到系统中。哈希的结果应能够保证原有已分配的内容可以被映射到新的缓冲中去,而不会被映射到旧的缓冲集合中的其他缓冲区。

容易看到,上面的简单 hash 算法 hash(object)%N 难以满足单调性要求。

3 consistent hashing 算法的原理

consistent hashing 是一种 hash 算法,简单的说,在移除 / 添加一个 cache 时,它能够尽可能小的改变已存在 key 映射关系,尽可能的满足单调性的要求。

下面就来按照 5 个步骤简单讲讲 consistent hashing 算法的基本原理。

3.1 环形hash 空间

考虑通常的 hash 算法都是将 value 映射到一个 32 为的 key 值,也即是 0~2^32-1 次方的数值空间;我们可以将这个空间想象成一个首( 0)尾( 2^32-1 )相接的圆环,如下面图 1 所示的那样。

memcache 集群

图 1 环形 hash 空间

3.2 把对象映射到hash 空间

接下来考虑 4 个对象 object1~object4 ,通过 hash 函数计算出的 hash 值 key 在环上的分布如图 2 所示。

hash(object1) = key1;

… …

hash(object4) = key4;

memcache 集群

图 2 4 个对象的 key 值分布

3.3 把cache 映射到hash 空间

Consistent hashing 的基本思想就是将对象和 cache 都映射到同一个 hash 数值空间中,并且使用相同的 hash 算法。

假设当前有 A,B 和 C 共 3 台 cache ,那么其映射结果将如图 3 所示,他们在 hash 空间中,以对应的 hash 值排列。

hash(cache A) = key A;

… …

hash(cache C) = key C;

memcache 集群

图 3 cache 和对象的 key 值分布

 

说到这里,顺便提一下 cache 的 hash 计算,一般的方法可以使用 cache 机器的 IP 地址或者机器名作为 hash 输入。

3.4 把对象映射到cache

现在 cache 和对象都已经通过同一个 hash 算法映射到 hash 数值空间中了,接下来要考虑的就是如何将对象映射到 cache 上面了。

在这个环形空间中,如果沿着顺时针方向从对象的 key 值出发,直到遇见一个 cache ,那么就将该对象存储在这个 cache 上,因为对 象和 cache 的 hash 值是固定的,因此这个 cache 必然是唯一和确定的。这样不就找到了对象和 cache 的映射方法了吗?!

依然继续上面的例子(参见图 3 ),那么根据上面的方法,对象 object1 将被存储到 cache A 上; object2 和 object3 对应到 cache C ; object4 对应到 cache B ;

3.5 考察cache 的变动

前面讲过,通过 hash 然后求余的方法带来的最大问题就在于不能满足单调性,当 cache 有所变动时, cache 会失效,进而对后台服务器造成巨大的冲击,现在就来分析分析 consistent hashing 算法。

3.5.1 移除 cache

考虑假设 cache B 挂掉了,根据上面讲到的映射方法,这时受影响的将仅是那些沿 cache B 逆时针遍历直到下一个 cache ( cache C )之间的对象,也即是本来映射到 cache B 上的那些对象。

因此这里仅需要变动对象 object4 ,将其重新映射到 cache C 上即可;参见图 4 。

memcache 集群

图 4 Cache B 被移除后的 cache 映射

3.5.2 添加 cache

再考虑添加一台新的 cache D 的情况,假设在这个环形 hash 空间中, cache D 被映射在对象 object2 和 object3 之间。这时受影响的将仅是那些沿 cache D 逆时针遍历直到下一个 cache ( cache B )之间的对象(它们是也本来映射到 cache C 上对象的一部分),将这些对象重新映射到 cache D 上即可。

因此这里仅需要变动对象 object2 ,将其重新映射到 cache D 上;参见图 5 。

memcache 集群

图 5 添加 cache D 后的映射关系

4 虚拟节点

考量 Hash 算法的另一个指标是平衡性 (Balance) ,定义如下:

平衡性

        平衡性是指哈希的结果能够尽可能分布到所有的缓冲中去,这样可以使得所有的缓冲空间都得到利用。

hash 算法并不是保证绝对的平衡,如果 cache 较少的话,对象并不能被均匀的映射到 cache 上,比如在上面的例子中,仅部 署 cache A 和 cache C 的情况下,在 4 个对象中, cache A 仅存储了 object1 ,而 cache C 则存储了 object2 、 object3 和 object4 ;分布是很不均衡的。

为了解决这种情况, consistent hashing 引入了“虚拟节点”的概念,它可以如下定义:

“虚拟节点”( virtual node )是实际节点在 hash 空间的复制品( replica ),一实际个节点对应了若干个“虚拟节点”,这个对应个数也成为“复制个数”,“虚拟节点”在 hash 空间中以 hash 值排列。

仍以仅部署 cache A 和 cache C 的情况为例,在图 4 中我们已经看到, cache 分布并不均匀。现在我们引入虚拟节点,并设置“复制个数”为 2 ,这就意味着一共会存 在 4 个“虚拟节点”, cache A1, cache A2 代表了 cache A ; cache C1, cache C2 代表了 cache C ;假设一种比较理想的情况,参见图 6 。

memcache 集群

图 6 引入“虚拟节点”后的映射关系

 

此时,对象到“虚拟节点”的映射关系为:

objec1->cache A2 ; objec2->cache A1 ; objec3->cache C1 ; objec4->cache C2 ;

因此对象 object1 和 object2 都被映射到了 cache A 上,而 object3 和 object4 映射到了 cache C 上;平衡性有了很大提高。

引入“虚拟节点”后,映射关系就从 { 对象 -> 节点 } 转换到了 { 对象 -> 虚拟节点 } 。查询物体所在 cache 时的映射关系如图 7 所示。

memcache 集群

图 7 查询对象所在 cache

 

“虚拟节点”的 hash 计算可以采用对应节点的 IP 地址加数字后缀的方式。例如假设 cache A 的 IP 地址为 202.168.14.241 。

引入“虚拟节点”前,计算 cache A 的 hash 值:

Hash(“202.168.14.241”);

引入“虚拟节点”后,计算“虚拟节”点 cache A1 和 cache A2 的 hash 值:

Hash(“202.168.14.241#1”);  // cache A1

Hash(“202.168.14.241#2”);  // cache A2

5 小结

Consistent hashing 的基本原理就是这些,具体的分布性等理论分析应该是很复杂的,不过一般也用不到。

http://weblogs.java.net/blog/2007/11/27/consistent-hashing 上面有一个 java 版本的例子,可以参考。

http://blog.csdn.net/mayongzhan/archive/2009/06/25/4298834.aspx 转载了一个 PHP 版的实现代码。

http://www.codeproject.com/KB/recipes/lib-conhash.aspx C语言版本


一些参考资料地址:

http://portal.acm.org/citation.cfm?id=258660

http://en.wikipedia.org/wiki/Consistent_hashing

http://www.spiteful.com/2008/03/17/programmers-toolbox-part-3-consistent-hashing/

 http://weblogs.java.net/blog/2007/11/27/consistent-hashing

http://tech.idv2.com/2008/07/24/memcached-004/

http://blog.csdn.net/mayongzhan/archive/2009/06/25/4298834.aspx


此文章转载于:

http://www.open-open.com/lib/view/open1340337319596.html



php对一致性hash分布不均,使用虚拟节点,看了http://bbs.phpchina.com/forum.php?mod=viewthread&tid=233897这个之后,自己对查找算法进行了修改:

  1. <?php   
  2. class memcacheHashMap{  
  3.     private $_node = array();  
  4.       
  5.     private $_nodeData = array();  
  6.       
  7.     private $_keyNode = 0;  
  8.       
  9.     private $_memcache = null;  
  10.       
  11.       
  12.     // 每个物理服务器生成虚拟节点的个数  
  13.     private $_virtualNodeNum = 200;  
  14.       
  15.     private function __construct(){  
  16.         // 配置文件  
  17.         $config = array(  
  18.             "127.0.0.1:11211",  
  19.             "127.0.0.1:11212",  
  20.             "127.0.0.1:11213",  
  21.             "127.0.0.1:11214",  
  22.             "127.0.0.1:11215"  
  23.         );  
  24.           
  25.         if (!$config){  
  26.             throw new Exception("cache config null");  
  27.         }  
  28.           
  29.         // 设置虚拟节点  
  30.         foreach($config as $key=>$value){  
  31.               
  32.             for ($i = 0; $i < $this->_virtualNodeNum; $i++){  
  33.                 $this->_node[sprintf("%u", crc32($value."#".$i))] = $value."#".$i;  
  34.             }  
  35.         }  
  36.           
  37.         // 排序  
  38.         ksort($this->_node);  
  39.           
  40. //      print_r($this->_node);  
  41.     }  
  42.       
  43.     // 单例模式  
  44.     static public function getInstance(){  
  45.         static $memcacheObj = null;  
  46.         if (!is_object($memcacheObj)) {  
  47.             $memcacheObj = new self();  
  48.         }  
  49.         return $memcacheObj;  
  50.     }  
  51.       
  52.     private function _connectMemcache($key){  
  53.         $this->_nodeData = array_keys($this->_node);  
  54. //      echo "all node: ";  
  55. //      print_r($this->_nodeData);  
  56.         $this->_keyNode = sprintf("%u", crc32($key));  
  57. //      $this->_keyNode = 1803717635;  
  58. //      var_dump($this->_keyNode);  
  59.           
  60.         // 获取key值对应的最近的节点  
  61.         $nodeKey = $this->_findServerNode(0, count($this->_nodeData)-1);  
  62. //      var_dump($nodeKey);  
  63. //      echo "$this->_keyNode :search node:$nodeKey  IP:{$this->_node[$nodeKey]} ";  
  64.           
  65.         //获取对应的真实ip  
  66.         list($config$num) = explode("#"$this->_node[$nodeKey]);  
  67.           
  68.         if (empty($config)){  
  69.             throw new Exception("serach ip config error");  
  70.         }  
  71.           
  72.         if (!isset($this->_memcache[$config])){  
  73.             $this->_memcache[$config] = new Memcache;  
  74.             list($host$port) = explode(":"$config);  
  75.             $this->_memcache[$config]->connect($host$port);  
  76.         }  
  77.           
  78.         return $this->_memcache[$config];  
  79.           
  80.           
  81.           
  82.     }  
  83.     /** 
  84.      * 采用二分法从虚拟memcache节点中查找最近的节点 
  85.      * @param int $low 开始位置 
  86.      * @param int $high 结束位置 
  87.      *  
  88.      */  
  89.     private function _findServerNode($low$high){  
  90.           
  91.         // 开始下标小于结束下标  
  92.         if ($low < $high){  
  93.               
  94.             $avg = intval(($low+$high)/2);  
  95.               
  96.             if ($this->_nodeData[$avg] == $this->_keyNode){  
  97.                 return $this->_nodeData[$avg];  
  98.             }elseif ($this->_keyNode < $this->_nodeData[$avg]){  
  99.                 return $this->_findServerNode($low$avg-1);  
  100.             }else{  
  101.                 return $this->_findServerNode($avg+1, $high);  
  102.             }  
  103.         }else if(($low == $high)){  
  104.             // 大于平均值  
  105.             if ($low ==0 || $low == count($this->_nodeData)-1){  
  106.                 return $this->_nodeData[$low];  
  107.             }  
  108. //          var_dump($low);  
  109.             if ($this->_nodeData[$low] < $this->_keyNode){  
  110.                   
  111.                 if (abs($this->_nodeData[$low] - $this->_keyNode) < abs($this->_nodeData[$low+1]-$this->_keyNode)){  
  112.                     return $this->_nodeData[$low];  
  113.                 }else{  
  114.                     return $this->_nodeData[$low+1];  
  115.                 }  
  116.           
  117.             }else {  
  118.                 if (abs($this->_nodeData[$low] - $this->_keyNode) < abs($this->_nodeData[$low-1]-$this->_keyNode)){  
  119.                     return $this->_nodeData[$low];  
  120.                 }else{  
  121.                     return $this->_nodeData[$low-1];  
  122.                 }  
  123.             }  
  124.         }else{  
  125.             if ( ($low == 0)&&($high < 0) ){  
  126.                 return $this->_nodeData[$low];  
  127.             }  
  128.           
  129.             if (abs($this->_nodeData[$low] - $this->_keyNode) < abs($this->_nodeData[$high]-$this->_keyNode)){  
  130.                 return $this->_nodeData[$low];  
  131.             }else{  
  132.                 return $this->_nodeData[$high];  
  133.             }  
  134.         }  
  135.     }  
  136.       
  137.     public function set($key$value$expire=0){  
  138. //  var_dump($key);  
  139.         return $this->_connectMemcache($key)->set($key, json_encode($value), 0, $expire);  
  140.     }  
  141.       
  142.       
  143.     public function add($key$vakue$expire=0){  
  144.         return $this->_connectMemcache($key)->add($key, json_encode($value), 0, $expire);  
  145.     }  
  146.       
  147.     public function get($key){  
  148.         return $this->_connectMemcache($key)->get($key, true);  
  149.     }  
  150.       
  151.     public function delete($key){  
  152.         return $this->_connectMemcache($key)->delete($key);  
  153.     }  
  154.       
  155.           
  156.       
  157. }  
  158.   
  159.   
  160. $runData['BEGIN_TIME'] = microtime(true);  
  161. //测试一万次set加get  
  162. for($i=0;$i<10000;$i++) {  
  163.         $key = md5(mt_rand());  
  164. //        var_dump($key);  
  165.         $b = memcacheHashMap::getInstance()->set($key, time(), 10);  
  166. }  
  167. echo "一致性hash:";  
  168. var_dump(number_format(microtime(true) - $runData['BEGIN_TIME'],6));  
  169. $runData['BEGIN_TIME'] = microtime(true);   
  170. $mnew Memcache;  
  171. $m->connect('127.0.0.1', 11211);   
  172. for($i=0;$i<10000;$i++) {  
  173.         $key = md5(mt_rand());  
  174.         $b = $m->set($key, time(), 0, 10);  
  175. }  
  176. echo "单台机器:";  
  177. var_dump(number_format(microtime(true) - $runData['BEGIN_TIME'],6));  

测试结果:






根据http://blog.csdn.net/mayongzhan/article/details/4298834进行了修改与测试

下边查找真实节点的原理是:将虚拟后的节点排序,返回第一个比key哈希后大的节点,若不存在则返回第一个


  1. <?php  
  2. /** 
  3.  * 一致性hahs实现类 
  4.  *  
  5.  */  
  6. class FlexiHash{  
  7.     /** 
  8.      * var int 
  9.      * 虚拟节点 
  10.      */  
  11.     private $_replicas = 200;  
  12.       
  13.     /** 
  14.      * 使用hash方法 
  15.      */  
  16.     private $_hasher = null;  
  17.       
  18.     /** 
  19.      * 真实节点计数器 
  20.      *  
  21.      */  
  22.     private $_tagertCount = 0;  
  23.       
  24.     /** 
  25.      * 位置对应节点,用户lookup中根据位置确定要访问的节点 
  26.      */  
  27.     private $_positionToTarget = array();  
  28.       
  29.     /** 
  30.      * 节点对应位置,用于删除节点 
  31.      */  
  32.     private $_targetToPositions = array();  
  33.       
  34.     /** 
  35.      * 是否已排序 
  36.      */  
  37.     private $_positionToTargetSorted = false;  
  38.       
  39.     /** 
  40.      * @ $hasher hash方法 
  41.      * @ $replicas 虚拟节点的个数 
  42.      *  
  43.      * 确定要使用的hash方法和虚拟的节点数,虚拟节点越多,分布越均匀,但程序的分布式运算越慢 
  44.      */  
  45.     public function __construct(FlexiHash_Hasher $hasher=null, $replicas = null){  
  46.         // hash方法  
  47.         $this->_hasher = $hasher?$hashernew FlexiHash_Crc32Hasher();  
  48.         // 虚拟节点的个数  
  49.         if (!empty($replicas)){  
  50.             $this->_replicas = $replicas;  
  51.         }  
  52.     }  
  53.       
  54.     /** 
  55.      * 增加节点,根据虚拟节点数,把节点分布到更多的虚拟位置上 
  56.      */  
  57.     public function addTarget($target){  
  58.           
  59.         if (isset($this->_targetToPositions[$target])) {  
  60.             throw new FlexiHash_Exception("Target $target already exists.");  
  61.         }  
  62.           
  63.         $this->_targetToPositions[$target] = array();  
  64.           
  65.         for ($i = 0; $i < $this->_replicas; $i++) {  
  66.               
  67.             // 根据规定的方法hash  
  68.             $position = $this->_hasher->hash($target.$i);  
  69.               
  70.             // 虚拟节点对应的真实的节点  
  71.             $this->_positionToTarget[$position] = $target;  
  72.               
  73.             // 真实节点包含的虚拟节点  
  74.             $this->_targetToPositions[$target][] = $position;  
  75.         }  
  76.           
  77.           
  78.         $this->_positionToTargetSorted = false;  
  79.           
  80.         // 真实节点个数  
  81.         $this->_targetCount++;  
  82.           
  83.         return $this;  
  84.     }  
  85.       
  86.     /** 
  87.      * 添加多个节点 
  88.      *  
  89.      */  
  90.     public function addTargets($targets){  
  91.         foreach ($targets as $target){  
  92.             $this->addTarget($target);  
  93.         }  
  94.         return $this;  
  95.     }  
  96.       
  97.     /** 
  98.      * 移除某个节点 
  99.      *  
  100.      */  
  101.     public function removeTarget($target){  
  102.         if (!isset($this->_targetToPositions[$target])){  
  103.             throw new FlexiHash_Exception("target $target does not exist ");  
  104.         }  
  105.           
  106.         foreach($this->_targetToPositions[$targetas $position){  
  107.             unset($this->_positionToTarget[$position]);  
  108.         }  
  109.           
  110.         unset($this->_targetToPositions[$target]);  
  111.           
  112.         $this->_targetCount--;  
  113.           
  114.         return $this;  
  115.     }  
  116.       
  117.     /** 
  118.      * 获取所有节点 
  119.      *  
  120.      */  
  121.     public function getAllTargets(){  
  122.         return array_keys($this->_targetToPositions);  
  123.     }  
  124.       
  125.       
  126.     /** 
  127.      * 根据key查找hash到的真实节点 
  128.      *  
  129.      */  
  130.     public function lookup($resource){  
  131.         $targets = $this->lookupList($resource, 1);  
  132.           
  133.         if (empty($targets)){  
  134.             throw new FlexiHash_Exception("no targets exist");  
  135.         }  
  136.           
  137.         return $targets[0];  
  138.     }  
  139.       
  140.     /** 
  141.      * 查找资源存在的节点 
  142.      *  
  143.      * 描述:根据要求的数量,返回与$resource哈希后数值相等或比其大并且是最小的数值对应的节点,若不存在或数量不够,则从虚拟节点排序后的前一个或多个 
  144.      */  
  145.     public function lookupList($resource$requestedCount){  
  146.           
  147.         if (!$requestedCount) {  
  148.             throw new FlexiHash_Exception('Invalid count requested');  
  149.         }  
  150.           
  151.         if (empty($this->_positionToTarget)) {  
  152.             return array();  
  153.         }  
  154.           
  155.         // 直接节点只有一个的时候  
  156.         if ($this->_targetCount == 1 ){  
  157.             return array_unique(array_values($this->_positionToTarget));  
  158.         }  
  159.           
  160.         // 获取当前key进行hash后的值  
  161.         $resourcePosition = $this->_hasher->hash($resource);  
  162.       
  163.         $results = array();  
  164.           
  165.         $collect = false;  
  166.           
  167.         $this->_sortPositionTargets();  
  168.           
  169.         // 查找与$resourcePosition 相等或比其大并且是最小的数  
  170.         foreach($this->_positionToTarget as $key => $value){  
  171.               
  172.             if (!$collect && $key > $resourcePosition){  
  173.                   
  174.                 $collect = true;  
  175.             }  
  176.               
  177.             if ($collect && !in_array($value$results)){  
  178.                 $results[] = $value;  
  179.             }  
  180.               
  181.             // 找到$requestedCount 或个数与真实节点数量相同  
  182.             if (count($results) == $requestedCount || count($results) == $this->_targetCount){  
  183.                 return $results;  
  184.             }  
  185.         }  
  186.         // 如数量不够或者未查到,则从第一个开始,将$results中不存在前$requestedCount-count($results),设置为需要的节点  
  187.         foreach ($this->_positionToTarget as $key => $value){  
  188.             if (!in_array($value$results)){  
  189.                 $results[] = $value;  
  190.             }  
  191.               
  192.             if (count($results) == $requestedCount || count($results) == $this->_targetCount){  
  193.               
  194.                 return $results;  
  195.             }  
  196.         }  
  197.           
  198.         return $results;  
  199.           
  200.     }  
  201.       
  202.     /** 
  203.      * 根据虚拟节点进行排序 
  204.      */  
  205.     private function _sortPositionTargets(){  
  206.         if (!$this->_positionToTargetSorted){  
  207.             ksort($this->_positionToTarget, SORT_REGULAR);  
  208.               
  209.             $this->_positionToTargetSorted = true;  
  210.         }  
  211.     }  
  212.       
  213. }// end class  
  214.   
  215. /** 
  216.  * hash方式 
  217.  */  
  218. interface FlexiHash_Hasher{  
  219.     public function hash($string);  
  220. }  
  221.   
  222. class FlexiHash_Crc32Hasher implements FlexiHash_Hasher{  
  223.     public function hash($string){  
  224.         return sprintf("%u",crc32($string));  
  225.     }  
  226. }  
  227.   
  228.   
  229. class FlexiHash_Md5Hasher implements FlexiHash_Hasher{  
  230.     public function hash($string){  
  231.         return substr(md5($string), 0, 8);  
  232.     }  
  233. }  
  234.   
  235. class FlexiHash_Exception extends Exception{  
  236. }  
  237.   
  238. $runData['BEGIN_TIME'] = microtime(true);  
  239.   
  240. for($i=0;$i<10000;$i++) {  
  241.   
  242.   
  243.      $targetsArray = array(  
  244.         "127.0.0.1:11211",  
  245.         "127.0.0.1:11212",  
  246.         "127.0.0.1:11213",  
  247.         "127.0.0.1:11214",  
  248.         "127.0.0.1:11215"  
  249.      );  
  250.      $flexiHashObj = new FlexiHash(new FlexiHash_Crc32Hasher(),1);  
  251.        
  252.      $result = $flexiHashObj->addTargets($targetsArray);  
  253.       $key = md5(mt_rand());  
  254.      $targets = $flexiHashObj->lookup($key);  
  255. //  var_dump($targets);  
  256.        
  257.        
  258.   
  259. }  
  260.     echo "一致性hash:";  
  261. var_dump(number_format(microtime(true) - $runData['BEGIN_TIME'],6));  
  262.   
  263.   
  264.   
  265.   
  266. $runData['BEGIN_TIME'] = microtime(true);   
  267. $mnew Memcache;  
  268. $m->connect('127.0.0.1', 11211);   
  269. for($i=0;$i<10000;$i++) {  
  270.         $key = md5(mt_rand());  
  271.         $b = $m->set($key, time(), 0, 10);  
  272. }  
  273. echo "单台机器:";  
  274. var_dump(number_format(microtime(true) - $runData['BEGIN_TIME'],6));  
  275. ?>  

测试结果:




原文地址:https://www.cnblogs.com/fonyer/p/3181399.html