memcache全都性hash的php实现方法__第1页
memcache全都性hash的php实现方法__第2页
memcache全都性hash的php实现方法__第3页
memcache全都性hash的php实现方法__第4页
memcache全都性hash的php实现方法__第5页
免费预览已结束,剩余2页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、memcache全都性hash的php实现方法_ 这篇文章主要介绍了memcache全都性hash的php实现方法,实例分析了memcache中hash全都性的实现原理与相关技巧,需要的伴侣可以参考下 本文实例讲解并描述了memcache全都性hash的php实现方法。分享给大家供大家参考。具体如下: 最近在看一些分布式方面的文章,所以就用php实现全都性hash来练练手,以前一般用的是最原始的hash取模做 分布式,当生产过程中添加或删除一台memcache都会造成数据的全部失效,全都性hash就是为了解决这个问题,把失效数据降到最低,相关资料可以 google一下! php实现效率有肯定的

2、缺失,假如要高效率,还是写扩展比较好 经测试,5个memcache,每个memcache生成100个虚拟节点,set加get1000次,与单个memcache挺直set加get慢5倍,所以效率一般,有待优化! 在阅读本文之前,最好知道二分查找法。 实现过程: memcache的配置 ip+端口+虚拟节点序列号 做hash,用法的是crc32,形成一个闭环。 对要操作的key进行crc32 二分法在虚拟节点环中查找最近的一个虚拟节点 从虚拟节点中提取真实的memcache ip和端口,做单例连接 代码如下: ?php class memcacheHashMap private $_node =

3、array(); private $_nodeData = array(); private $_keyNode = 0; private $_memcache = null; /每个物理服务器生成虚拟节点个数 注:节点数越多,cache分布的匀称性越好,同时set get操作时,也更耗资源,10台物理服务器,采纳200较为合理 private $_virtualNodeNum = 200; private function _construct() $config = array(/五个memcache服务器 127.0.0.1:11211, 127.0.0.1:11212, 127.0.0

4、.1:11213, 127.0.0.1:11214, 127.0.0.1:11215 ); if (!$config) throw new Exception(Cache config NULL); foreach ($config as $key = $value) for ($i = 0; $i $this-_virtualNodeNum; $i+) $this-_nodesprintf(%u, crc32($value . _ . $i) = $value . _ . $i;/循环为每个memcache服务器创建200个虚拟节点 ksort($this-_node);/创建出来的1000

5、个虚拟节点根据键名从小到大排序 /实例化该类 static public function getInstance() static $memcacheObj = null; if (!is_object($memcacheObj) $memcacheObj = new self(); return $memcacheObj; /依据传来的键查找到对应虚拟节点的位置 private function _connectMemcache($key) $this-_nodeData = array_keys($this-_node);/全部的虚拟节点的键的数组 $this-_keyNode = sp

6、rintf(%u, crc32($key);/算出键的hash值 $nodeKey = $this-_findServerNode();/找出对应的虚拟节点 /假如超出环,从头再用二分法查找一个最近的,然后环的头尾做推断,取最接近的节点 if ($this-_keyNode end($this-_nodeData) $this-_keyNode -= end($this-_nodeData); $nodeKey2 = $this-_findServerNode(); if (abs($nodeKey2 - $this-_keyNode) abs($nodeKey - $this-_keyNod

7、e) $nodeKey = $nodeKey2; var_dump($this-_node$nodeKey); list($config, $num) = explode(_, $this-_node$nodeKey); if (!$config) throw new Exception(Cache config Error); if (!isset($this-_memcache$config) $this-_memcache$config = new Memcache; list($host, $port) = explode(:, $config); $this-_memcache$co

8、nfig-connect($host, $port); return $this-_memcache$config; /二分法依据给出的值找出最近的虚拟节点位置 private function _findServerNode($m = 0, $b = 0) $total = count($this-_nodeData); if ($total != 0 $b = 0) $b = $total - 1; if ($m $b) $avg = intval($m+$b) / 2); if ($this-_nodeData$avg = $this-_keyNode) return $this-_no

9、deData$avg; elseif ($this-_keyNode $this-_nodeData$avg ($avg-1 = 0) return $this-_findServerNode($m, $avg-1); else return $this-_findServerNode($avg+1, $b); if (abs($this-_nodeData$b - $this-_keyNode) abs($this-_nodeData$m - $this-_keyNode) return $this-_nodeData$b; else return $this-_nodeData$m; pu

10、blic function set($key, $value, $expire = 0) return $this-_connectMemcache($key)-set($key, json_encode($value), 0, $expire); public function add($key, $value, $expire = 0) return $this-_connectMemcache($key)-add($key, json_encode($value), 0, $expire); public function get($key) return json_decode($th

11、is-_connectMemcache($key)-get($key), true); public function delete($key) return $this-_connectMemcache($key)-delete($key); $runDataBEGIN_TIME = microtime(true); /测试一万次set加get for($i=0;$i10000;$i+) $key = md5(mt_rand(); $b = memcacheHashMap:getInstance()-set($key, time(), 10); var_dump(number_format(microtime(true) - $runDataBEGIN_TIME,6); $runDataBEGIN_TIME = microtime(true); $m= new Memcache; $m-connect(127.0.0.1, 11211); for

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论