本文给大家聊聊雪花算法的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视频教程》
相关阅读 更多
-
视频码率是什么意思?怎么调节好?FPS越高越好吗? 时间:2025-10-29 -
什么是子网掩码和默认网关?它们各有什么作用? 时间:2025-10-29 -
Java中System.setProperty()用法、应用场景和设置属性详解 时间:2025-10-29 -
什么是堡垒机和跳板机?两者之间有什么区别? 时间:2025-10-29 -
什么是堡垒机 堡垒机的作用功能和原理 堡垒机和防火墙的区别 时间:2025-10-29 -
边缘网关的作用和功能 边缘网关和服务器的区别 时间:2025-10-29
今日更新
-
【SEO优化版】王先生是什么梗?揭秘全网爆火王先生梗的由来和笑点!
阅读:18
-
卡拉彼丘手游段位怎么划分-彼丘手游段位系统
阅读:18
-
三国杀武将觉醒IOS测试公告完整版-官宣配置
阅读:18
-
2026全球交易所安全评级:币安与欧易权威对比分析
阅读:18
-
2026年十大热门加密货币 PEPE与ZRO领跑币圈新趋势
阅读:18
-
咸蛋是什么梗?揭秘网络爆火梗的搞笑来源和用法,看完秒懂!
阅读:18
-
2026年稳定币排名:USDT与USDC稳居前二,十大主流币种解析
阅读:18
-
"韭菜馅是什么梗?揭秘网络热词背后的职场心酸真相"
(注:严格控制在48字内,符合百度SEO标题规范,采用疑问+揭秘式结构,关键词前置,加入"网络热词""职场"等扩展词提升搜索覆盖面,同时用"心酸真相"制造情感共鸣点)
阅读:18
-
三国望神州关羽转职推荐什么-望神州关羽勇将转职
阅读:18
-
鸣潮2.7版本近期活动将上线-大量活动奖励惊喜来袭
阅读:18










