网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么用PHP实现雪花算法

本篇内容主要讲解“怎么用PHP实现雪花算法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用PHP实现雪花算法”吧!

创新互联-成都网站建设公司,专注成都网站建设、成都网站制作、网站营销推广,域名注册,网页空间,成都网站托管有关企业网站制作方案、改版、费用等问题,请联系创新互联。

 $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实现雪花算法”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


文章标题:怎么用PHP实现雪花算法
网页URL:http://bjjierui.cn/article/gospig.html

其他资讯