<?php
/**
* 一致性哈希memcache分布式,采用的是虚拟节点的方式解决分布均匀性问题,查找节点采用二分法快速查找
* the last known user to change this file in the repository <$LastChangedBy: nash.xiong $>
* @author nash.xiong <nash.xiong@gmail.com>
* @copyright Copyright &copy; 2003-2012 phpd.cn
* @license
*/ class memcacheHashMap { private $_node = array();
private $_nodeData = array();
private $_keyNode = 0;
private $_memcache = null; //每个物理服务器生成虚拟节点个数 [注:节点数越多,cache分布的均匀性越好,同时set get操作时,也更耗资源,10台物理服务器,采用200较为合理]
private $_virtualNodeNum = 200; private function __construct() {
/* 放入配置文件 */
$config = array(
'127.0.0.1:11211',
'127.0.0.1:11212',
'127.0.0.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->_node[sprintf("%u",crc32($value . "_" .$i))] = $value . '_' . $i;
}
}
ksort($this->_node);
} private function __clone() {} /**
* 单例,保证只有一个实例
*/
static public function getInstance() {
static $memcacheObj = null;
if(!is_object($memcacheObj)) {
$memcacheObj = new self();
}
return $memcacheObj;
} /**
* 根据key做一致性hash后连接到一台物理memcache服务器
* @param string $key
*/
private function _connectMemcache($key) {
$this->_nodeData = array_keys($this->_node);
$this->_keyNode = sprintf("%u",crc32($key));
$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->_keyNode))
$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[$config]->connect($host,$port);
} return $this->_memcache;
}
/**
* 采用二分法从虚拟memcache节点中查找最近的节点
* @param unknown_type $m
* @param unknown_type $b
*/
private function _findServerNode($m = 0,$b = 0) {
$totle = count($this->_nodeData);
if($totle != 0 && $b == 0) $b = $totle - 1;
if($m < $b) {
$avg = intval(($m+$b) / 2);
if($this->_nodeData[$avg] == $this->_keyNode) return $this->_nodeData[$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];
} public 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($this->_connectMemcache($key)->get($key), true);
} public function delete($key) {
return $this->_connectMemcache($key)->delete($key);
}
}
    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($this->_connectMemcache($key)->get($key), true);
} public function delete($key) {
return $this->_connectMemcache($key)->delete($key);
}
}

memcache的一致性hash算法的更多相关文章

  1. memcache的一致性hash算法使用

    一.概述 1.我们的memcache客户端(这里我看的spymemcache的源码),使用了一致性hash算法ketama进行数据存储节点的选择.与常规的hash算法思路不同,只是对我们要存储数据的k ...

  2. memcache分布式 [一致性hash算法] 的php实现

    最近在看一些分布式方面的文章,所以就用php实现一致性hash来练练手,以前一般用的是最原始的hash取模做分布式,当生产过程中添加或删除一台memcache都会造成数据的全部失效,一致性hash就是 ...

  3. Nginx+Memcache+一致性hash算法 实现页面分布式缓存(转)

    网站响应速度优化包括集群架构中很多方面的瓶颈因素,这里所说的将页面静态化.实现分布式高速缓存就是其中的一个很好的解决方案... 1)先来看看Nginx负载均衡 Nginx负载均衡依赖自带的 ngx_h ...

  4. php 实现一致性hash 算法 memcache

    散列表的应用 涉及到数据查找比对,首先考虑到使用HashSet.HashSet最大的好处就是实现查找时间复杂度为O(1).使用HashSet需要解决一个重要问题:冲突问题.对比研究了网上一些字符串哈希 ...

  5. 对一致性Hash算法,Java代码实现的深入研究

    一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法.一致性 ...

  6. 一致性hash算法简介与代码实现

    一.简介: 一致性hash算法提出了在动态变化的Cache环境中,判定哈希算法好坏的四个定义: 1.平衡性(Balance) 2.单调性(Monotonicity) 3.分散性(Spread) 4.负 ...

  7. Java实现一致性Hash算法深入研究

    一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中”一致性Hash算法”部分,对于为什么要使用一致性Hash算法和一致性Hash算法的算法原 ...

  8. 对一致性Hash算法及java实现(转)

    一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中"一致性Hash算法"部分,对于为什么要使用一致性Hash算法.一致性 ...

  9. 对一致性Hash算法,Java代码实现的深入研究(转)

    转载:http://www.cnblogs.com/xrq730/p/5186728.html 一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读 ...

随机推荐

  1. CCTableView的使用和注意事项

    #include "cocos-ext.h" using namespace cocos2d::extension; class TableViewTestLayer: publi ...

  2. android一些基础知识

    android应用基于JAVA, 支持SQL,由于底层是LINUX,所以支持C/C++ 目前有两种编程:基于ADT的JAVA编程,基于NDK的C编程 Android编程环境需要哪些:官方推荐用JDK+ ...

  3. use selenium in scrapy webdriver

    https://pypi.python.org/pypi/selenium from selenium import webdriver from selenium.webdriver.common. ...

  4. scrapy yield Request

    import scrapy from myproject.items import MyItem class MySpider(scrapy.Spider): name = ’example.com’ ...

  5. customerized convert from field type to DB field's type

    @LastModifiedDate @Convert(converter = LocalDateTime2TimestampConverter.class) @Slf4j public class L ...

  6. spring+hibernate+struts整合(2)

    spring和struts2的整合 1:配置Web.xml文件 <filter> <filter-name>struts2</filter-name> <fi ...

  7. 怎么删除远程登录连接的ip

    通过远程桌面可以登录到远程电脑上进行相应的操作,在登录过后会在本地电脑上留下登录过的IP以及登录用户名相关信息,可能会给远程的电脑带来安全隐患,下面介绍一下清除远程桌面历史记录的方法. 1.删除我的文 ...

  8. next_permutation()—遍历全排列

    # next_permutation()--遍历全排列 template <class BidirectionalIterator> bool next_permutation (Bidi ...

  9. hdu 4628 动态规划

    思路:首先就是状态压缩,然后判断哪些状态是回文串.最后就是动态方程:dp[i]=min(dp[i],dp[j]+1).这个方程得前提条件是状态(j-i)为回文串. #include<iostre ...

  10. hihocoder 1310 岛屿

    #1310 : 岛屿 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给你一张某一海域卫星照片,你需要统计: 1. 照片中海岛的数目 2. 照片中面积不同的海岛数目 3. ...