本文给大家聊聊雪花算法的PHP实现,希望对需要的朋友有所帮助!
雪花算法的实现
最近看了下雪花算法,自己试着写了一下
<?php
class SnowFlake
{
const TWEPOCH = 0; // 时间起始标记点,作为基准,一般取系统的最近时间(一旦确定不能变动)
const WORKER_ID_BITS = 5; // 机器标识位数
const DATACENTER_ID_BITS = 5; // 数据中心标识位数
const SEQUENCE_BITS = 12; // 毫秒内自增位
private $workerId; // 工作机器ID
private $datacenterId; // 数据中心ID
private $sequence; // 毫秒内序列
private $maxWorkerId = -1 ^ (-1 << self::WORKER_ID_BITS); // 机器ID最大值
private $maxDatacenterId = -1 ^ (-1 << self::DATACENTER_ID_BITS); // 数据中心ID最大值
private $workerIdShift = self::SEQUENCE_BITS; // 机器ID偏左移位数
private $datacenterIdShift = self::SEQUENCE_BITS + self::WORKER_ID_BITS; // 数据中心ID左移位数
private $timestampLeftShift = self::SEQUENCE_BITS + self::WORKER_ID_BITS + self::DATACENTER_ID_BITS; // 时间毫秒左移位数
private $sequenceMask = -1 ^ (-1 << self::SEQUENCE_BITS); // 生成序列的掩码
private $lastTimestamp = -1; // 上次生产id时间戳
public function __construct($workerId, $datacenterId, $sequence = 0)
{
if ($workerId > $this->maxWorkerId || $workerId < 0) {
throw new Exception("worker Id can't be greater than {$this->maxWorkerId} or less than 0");
}
if ($datacenterId > $this->maxDatacenterId || $datacenterId < 0) {
throw new Exception("datacenter Id can't be greater than {$this->maxDatacenterId} or less than 0");
}
$this->workerId = $workerId;
$this->datacenterId = $datacenterId;
$this->sequence = $sequence;
}
public function createId()
{
$timestamp = $this->createTimestamp();
if ($timestamp < $this->lastTimestamp) {//当产生的时间戳小于上次的生成的时间戳时,报错
$diffTimestamp = bcsub($this->lastTimestamp, $timestamp);
throw new Exception("Clock moved backwards. Refusing to generate id for {$diffTimestamp} milliseconds");
}
if ($this->lastTimestamp == $timestamp) {//当生成的时间戳等于上次生成的时间戳的时候
$this->sequence = ($this->sequence + 1) & $this->sequenceMask;//序列自增一次
if (0 == $this->sequence) {//当序列为0时,重新生成最新的时间戳
$timestamp = $this->createNextTimestamp($this->lastTimestamp);
}
} else {//当生成的时间戳不等于上次的生成的时间戳的时候,序列归0
$this->sequence = 0;
}
$this->lastTimestamp = $timestamp;
return (($timestamp - self::TWEPOCH) << $this->timestampLeftShift) |
($this->datacenterId << $this->datacenterIdShift) |
($this->workerId << $this->workerIdShift) |
$this->sequence;
}
protected function createNextTimestamp($lastTimestamp) //生成一个大于等于 上次生成的时间戳 的时间戳
{
$timestamp = $this->createTimestamp();
while ($timestamp <= $lastTimestamp) {
$timestamp = $this->createTimestamp();
}
return $timestamp;
}
protected function createTimestamp()//生成毫秒级别的时间戳
{
return floor(microtime(true) * 1000);
}
}
?>推荐学习:《PHP视频教程》
相关阅读 更多
-
抖音网页版官方入口 抖音网页版在线观看官网 时间:2026-01-08 -
GitLab、Git、Github、Gitee四者的区别 时间:2026-01-08 -
顺磁性和逆磁性的区别 顺磁性和逆磁性的判断方法 时间:2026-01-08 -
顺磁性是什么意思 顺磁性材料有哪些 时间:2026-01-08 -
usim卡是什么意思 usim卡和sim卡的区别 时间:2026-01-08 -
Web前端四大渲染模式SSR、CSR、ISR、SSG的定义和区别 时间:2026-01-08
今日更新
-
qq朋友网登录入口-qq朋友网网页版一键登录
阅读:18
-
刘梗宏女孩是什么梗?揭秘网络爆火健身主播的魔性口号与粉丝狂欢现象,看完秒懂!
阅读:18
-
啵乐漫画app最新版本下载-啵乐漫画官方安装包免费下载
阅读:18
-
Freeok免费追剧app下载安装-Freeok免费追剧软件官方版本
阅读:18
-
哔哩哔哩漫画网页版官方入口-哔哩哔哩漫画官网一键直达
阅读:18
-
七夕漫画官方下载入口安卓最新版-七夕漫画官方下载入口免费安装包
阅读:18
-
夸克网页版入口-夸克浏览器2026最新网页版登录
阅读:18
-
刘国梁不懂球是什么梗?揭秘国乒教练爆笑名场面由来
阅读:18
-
1-75级抖音刷多少-抖音1-75级价格表
阅读:18
-
漫蛙漫画网安卓版下载-漫蛙漫画官方最新版本安装入口
阅读:18










