怎么用PHP實現(xiàn)雪花算法

本篇內(nèi)容主要講解“怎么用PHP實現(xiàn)雪花算法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么用PHP實現(xiàn)雪花算法”吧!

創(chuàng)新互聯(lián)-成都網(wǎng)站建設公司,專注成都網(wǎng)站建設、成都網(wǎng)站制作、網(wǎng)站營銷推廣,域名注冊,網(wǎng)頁空間,成都網(wǎng)站托管有關(guān)企業(yè)網(wǎng)站制作方案、改版、費用等問題,請聯(lián)系創(chuàng)新互聯(lián)。

<?php
class SnowFlake
{
    const TWEPOCH = 0; // 時間起始標記點,作為基準,一般取系統(tǒng)的最近時間(一旦確定不能變動)
    const WORKER_ID_BITS     = 5; // 機器標識位數(shù)
    const DATACENTER_ID_BITS = 5; // 數(shù)據(jù)中心標識位數(shù)
    const SEQUENCE_BITS      = 12; // 毫秒內(nèi)自增位
    private $workerId; // 工作機器ID
    private $datacenterId; // 數(shù)據(jù)中心ID
    private $sequence; // 毫秒內(nèi)序列
    private $maxWorkerId     = -1 ^ (-1 << self::WORKER_ID_BITS); // 機器ID最大值
    private $maxDatacenterId = -1 ^ (-1 << self::DATACENTER_ID_BITS); // 數(shù)據(jù)中心ID最大值
    private $workerIdShift      = self::SEQUENCE_BITS; // 機器ID偏左移位數(shù)
    private $datacenterIdShift  = self::SEQUENCE_BITS + self::WORKER_ID_BITS; // 數(shù)據(jù)中心ID左移位數(shù)
    private $timestampLeftShift = self::SEQUENCE_BITS + self::WORKER_ID_BITS + self::DATACENTER_ID_BITS; // 時間毫秒左移位數(shù)
    private $sequenceMask       = -1 ^ (-1 << self::SEQUENCE_BITS); // 生成序列的掩碼
    private $lastTimestamp = -1; // 上次生產(chǎn)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) {//當產(chǎn)生的時間戳小于上次的生成的時間戳時,報錯
            $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實現(xiàn)雪花算法”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!

網(wǎng)頁標題:怎么用PHP實現(xiàn)雪花算法
鏈接地址:http://bm7419.com/article6/gospig.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航、搜索引擎優(yōu)化網(wǎng)站制作、外貿(mào)建站網(wǎng)站策劃、服務器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站