Last active
December 25, 2015 08:38
-
-
Save forecho/ed2e61a4d75991fa0df9 to your computer and use it in GitHub Desktop.
Primary 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 | |
static $OUR_EPOCH = 1444647842959; | |
static $DEFAULT_SHARD_ID = 1; | |
/** | |
* 生成 bigint64 位主键 | |
* @return int | |
*/ | |
public static function generatePk() | |
{ | |
$microTime = intval(microtime(true) * 1000); | |
$id = ($microTime - static::$OUR_EPOCH) << (64-41); | |
$id |= (static::$DEFAULT_SHARD_ID << (64-41-13)); | |
$id |= (mt_rand(1, 99999999) % 1024); | |
return $id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment