雪花算法生成数字id_php雪花算法SnowFlake生成唯一ID

php雪花算法SnowFlake生成唯一ID

龙行    PHP    2019-6-5    3090    0评论

这个算法的好处很简单可以在每秒产生约400W个不同的16位数字ID(10进制)

demo1

/**

* 雪花算法类

* @package app\helpers

*/

class SnowFlake

{

const EPOCH = 1479533469598;

const max12bit = 4095;

const max41bit = 1099511627775;

static $machineId = null;

public static function machineId($mId = 0) {

self::$machineId = $mId;

}

public static function generateParticle() {

/*

* Time - 42 bits

*/

$time = floor(microtime(true) * 1000);

/*

* Substract custom epoch from current time

*/

$time -= self::EPOCH;

/*

* Create a base and add time to it

*/

$base = decbin(self::max41bit + $time);

/*

* Configured machine id - 10 bits - up to 1024 machines

*/

if(!self::$machineId) {

$machineid = self::$machineId;

} else {

$machineid = str_pad(decbin(self::$machineId), 10, "0", STR_PAD_LEFT);

}

/*

* sequence number - 12 bits - up to 4096 random numbers per machine

*/

$random = str_pad(decbin(mt_rand(0, self::max12bit)), 12, "0", STR_PAD_LEFT);

/*

* Pack

*/

$base = $base.$machineid.$random;

/*

* Return unique time id no

*/

return bindec($base);

}

public static function timeFromParticle($particle) {

/*

* Return time

*/

return bindec(substr(decbin($particle),0,41)) - self::max41bit + self::EPOCH;

}

}

demo2

public function createID(){

//假设一个机器id

$machineId = 1234567890;

//41bit timestamp(毫秒)

$time = floor(microtime(true) * 1000);

//0bit 未使用

$suffix = 0;

//datacenterId 添加数据的时间

$base = decbin(pow(2,40) - 1 + $time);

//workerId 机器ID

$machineid = decbin(pow(2,9) - 1 + $machineId);

//毫秒类的计数

$random = mt_rand(1, pow(2,11)-1);

$random = decbin(pow(2,11)-1 + $random);

//拼装所有数据

$base64 = $suffix.$base.$machineid.$random;

//将二进制转换int

$base64 = bindec($base64);

$id = sprintf('%.0f', $base64);

return $id;

}

评论一下

赞助站长

赞助站长X

版权申明:此文如未标注转载均为本站原创,自由转载请表明出处《龙行博客》。

本文网址:https://www.liaotaoo.cn/236.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值