Skip to content

Commit cc9d2a6

Browse files
committed
add hash alg
1 parent 9ea72a0 commit cc9d2a6

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

consistency_hash/MemServerSetting.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,19 @@ static public function getInstance(){
7777
*
7878
* @return int
7979
*/
80-
private function hashFunc($key){
81-
$sum = crc32($key);
82-
return $sum % $this->size;
80+
public function hashFunc($key){
81+
//获取8个字符
82+
$md5 = substr(md5($key),0,8);
83+
$seed = 31;
84+
$hash = 0;
85+
86+
//计算一个hash值
87+
for($i = 0; $i<8;$i++){
88+
$hash = $hash*$seed + ord($md5{$i});
89+
}
90+
91+
//位运算, max 0x7fffffff
92+
return $hash & 0xFFFFFFFF;
8393
}
8494

8595
}

0 commit comments

Comments
 (0)