Skip to content

Instantly share code, notes, and snippets.

@ramsey
Created June 9, 2010 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramsey/432022 to your computer and use it in GitHub Desktop.
Save ramsey/432022 to your computer and use it in GitHub Desktop.
A consistent hashing algorithm implemented in PHP
<?php
$cont = 0;
foreach ($servers AS $ip)
{
for ($k = 0; $k < 40; $k++) {
$ss = "$ip-$k";
$digest = md5($ss, true);
for ($h = 0; $h < 4; $h++) {
$continuum[$cont]['point'] =
(ord($digest[3+$h*4]) << 24 )
| (ord($digest[2+$h*4]) << 16 )
| (ord($digest[1+$h*4]) << 8 )
| ord($digest[$h*4]);
$continuum[$cont]['ip'] = $ip;
$cont++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment