Skip to content

Instantly share code, notes, and snippets.

@flowerains
Created March 20, 2017 16:55
Show Gist options
  • Save flowerains/3507d45214912e1a0866d62e48dfb149 to your computer and use it in GitHub Desktop.
Save flowerains/3507d45214912e1a0866d62e48dfb149 to your computer and use it in GitHub Desktop.
缓存的普通hash和一致性hash的算法
<?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