Skip to content

Instantly share code, notes, and snippets.

@zhengkai
Last active August 29, 2015 14:10
Show Gist options
  • Save zhengkai/c1dce35f96ae4a8458e5 to your computer and use it in GitHub Desktop.
Save zhengkai/c1dce35f96ae4a8458e5 to your computer and use it in GitHub Desktop.
在多个服务器同时生成 id 的方式
<?php
/*
* https://gist.github.com/zhengkai/c1dce35f96ae4a8458e5
*
* author: Zheng Kai (zhengkai@gmail.com)
*/
function gen_id($i) {
$iSize = 10;
$iGroup = 3;
$iGroupTotal = $iSize * $iGroup;
$iOverflow = $i % $iGroupTotal;
$iBase = $i - $iOverflow;
$iSub = $iOverflow % $iGroup;
$iStep = (int)floor($iOverflow / $iGroup);
return $iBase + $iStep + $iSub * $iSize;
}
foreach (range(0, 50) as $i) {
$iFinal = gen_id($i);
echo sprintf('from %5d to %5d', $i, $iFinal), "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment