取余和一致性hash算法

配置文件

<?php
$s = [];
$ss['A'] = ['host'=>'localhost','11211'];
$ss['B'] = ['host'=>'localhost','11212'];
$ss['C'] = ['host'=>'localhost','11213'];
$ss['D'] = ['host'=>'localhost','11214'];
$ss['E'] = ['host'=>'localhost','11215'];

取余算法

<?php
/**
* memcached 取模分布式算法
*/
interface hasher{
    public function _hash($str);
}

interface distribution{
    public function lookup($key);
}

class Moder implements hasher,distribution{
	protected $_ser = [];
	protected $_num = 0;//服务器数

	//获取整数值
	public function _hash($str){
		return sprintf('%u',crc32($str));		

	}

	public function lookup($key){
		$index = $this -> _hash($key) % $this->_num;
		return $this->_ser[$index];	
	}
	//添加服务器
	public function addNode($s){
		$this->_ser[] = $s;
		$this->_num +=1;
	}
	//删除节点
	public function delNode($s){
		foreach($this ->_ser as $k => $v){
			if($s == $v){
				unset($this_ser[$k]);
			}
		}
		$this->_num -= 1;
		$this->_ser = array_merge($this->_ser);
	}

}

$m = new Moder();
$m ->addNode('a');
$m ->addNode('b');
$m ->addNode('c');
$m ->addNode('d');

for($i = 1;$i <=100; $i ++){
	$key = 'key_'.$i;
	echo $key,'--->',$m -> lookup($key),PHP_EOL;
}



一致性hash算法

<?php
date_default_timezone_set('PRC');
interface hasher{
    public function _hash($str);
}

interface distribution{
    public function lookup($key);
}

class Cons implements hasher,distribution{
	protected $nodes = [];
	protected $points = [];
	protected $_mul = 64;//虚拟节点数

	//获取无符号整数值
	public function _hash($str){
		return sprintf('%u',crc32($str));
	}
	//分配节点
	public function lookup($key){
		$postion = $this->_hash($key);//获取键值的整数值
		reset($this->points);
		$neddle = key($this->points);
		foreach($this -> points as $p => $v){
			if($postion <=$p){
				$neddle = $p;
				break;
			}
		}
		return $neddle.'--->'.$this ->points[$neddle];
	}
	//添加节点
	public function addNode($node){
		$this ->nodes[$node] = [];
		for($i = 0;$i< $this->_mul;$i +=1){
			$point = $node .'_'.$i;
			$point = $this ->_hash($point);//虚拟节点转为数字
			$this -> points[$point] = $node;
			$this ->nodes[$node][] = $point;
			$this -> resort();
		}
	}

	//排序
	public function resort(){
		 ksort($this->points);
	}
	//删除节点
	public function delNode($node){
		foreach($this->nodes[$node] as $p){
			unset($this->points[$p]);//清除虚拟节点
		}
		unset($this->nodes[$node]);//清除节点
	}
	
}

$cons = new cons();
$cons -> addNode('A');
$cons -> addNode('B');
$cons -> addNode('C');
$cons -> addNode('D');
$cons -> addNode('E');
echo $cons -> _hash('name'),'--fall in节点--',$cons -> lookup('name'),PHP_EOL;
echo $cons -> _hash('title'),'--fall in节点--',$cons -> lookup('title'),PHP_EOL;
echo $cons -> _hash('aaaa'),'--fall in节点--',$cons -> lookup('aaaa'),PHP_EOL;
print_r($cons);


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a...Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值