Created
March 20, 2017 16:55
-
-
Save flowerains/3507d45214912e1a0866d62e48dfb149 to your computer and use it in GitHub Desktop.
缓存的普通hash和一致性hash的算法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 整数hash | |
function intHash($key) | |
{ | |
$md5 = substr(md5($key), 0, 8); | |
$seed = 31; | |
$hash = 0; | |
for ($i = 0;$i < 8; ++$i) { | |
$hash = $hash * $seed + ord(md5($i)); | |
++$i; | |
} | |
return $hash & 0x7FFFFFFF; | |
} | |
$value = intHash('wangchuang') % 2; | |
echo $value; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment