怎么用PHP实现雪花算法

发布时间:2021-11-23 15:36:48 作者:iii
来源:亿速云 阅读:149

本篇内容主要讲解“怎么用PHP实现雪花算法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用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实现雪花算法”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. 雪花算法(04)机器信息
  2. 雪花算法(03)生成时间

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:plink PED文件格式是怎样的

下一篇:c语言怎么实现含递归清场版扫雷游戏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》