Last active
August 29, 2015 14:10
-
-
Save zhengkai/c1dce35f96ae4a8458e5 to your computer and use it in GitHub Desktop.
在多个服务器同时生成 id 的方式
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 | |
/* | |
* 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