站長資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        用PHP搗鼓一個雪花算法

        本文給大家聊聊雪花算法的PHP實現(xiàn),希望對需要的朋友有所幫助!

        雪花算法的實現(xiàn)

        最近看了下雪花算法,自己試著寫了一下

        <?php class SnowFlake {     const TWEPOCH = 0; // 時間起始標(biāo)記點,作為基準(zhǔn),一般取系統(tǒng)的最近時間(一旦確定不能變動)     const WORKER_ID_BITS     = 5; // 機器標(biāo)識位數(shù)     const DATACENTER_ID_BITS = 5; // 數(shù)據(jù)中心標(biāo)識位數(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) {//當(dāng)產(chǎn)生的時間戳小于上次的生成的時間戳?xí)r,報錯             $diffTimestamp = bcsub($this->lastTimestamp, $timestamp);             throw new Exception("Clock moved backwards.  Refusing to generate id for {$diffTimestamp} milliseconds");         }         if ($this->lastTimestamp == $timestamp) {//當(dāng)生成的時間戳等于上次生成的時間戳的時候             $this->sequence = ($this->sequence + 1) & $this->sequenceMask;//序列自增一次             if (0 == $this->sequence) {//當(dāng)序列為0時,重新生成最新的時間戳                 $timestamp = $this->createNextTimestamp($this->lastTimestamp);             }         } else {//當(dāng)生成的時間戳不等于上次的生成的時間戳的時候,序列歸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);     } } ?>

        推薦學(xué)習(xí):《PHP視頻教程》

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號
        主站蜘蛛池模板: 精品无码一区在线观看| 久久久久国产精品| 999久久久无码国产精品| 精品精品国产欧美在线小说区| 国产成人精品高清在线观看93 | 亚洲精品乱码久久久久久自慰| 久久久国产精品福利免费| 精品无码人妻夜人多侵犯18| 亚洲国产精品成人久久蜜臀| 精品国产一区二区22| 欧美国产日本精品一区二区三区| 麻豆成人久久精品二区三区免费| 亚洲国产精品日韩专区AV| 久久久精品久久久久特色影视| 成人免费精品网站在线观看影片 | 99久久99这里只有免费费精品| 一本久久a久久精品综合香蕉| 久久免费国产精品| 国产精品美女网站| 99久久婷婷国产综合精品草原| 国产福利在线观看精品| 精品国产第一国产综合精品| 日韩精品无码久久久久久 | 亚洲精品456播放| 久久精品国产色蜜蜜麻豆| 国产高清精品在线| 亚洲第一精品在线视频| 国产在线精品网址你懂的 | 国产精品无码av在线播放| 人人妻人人澡人人爽精品日本| 少妇伦子伦精品无码STYLES| 亚洲国产精品无码专区影院| 亚洲乱码精品久久久久..| 亚洲色精品aⅴ一区区三区| 亚洲av无码成人精品区在线播放| 色偷偷88欧美精品久久久 | 亚洲精品无码激情AV| 亚洲精品成a人在线观看| 中文精品久久久久人妻不卡| 亚洲日韩精品A∨片无码| 亚洲精品乱码久久久久久按摩|