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

創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司,專注成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、網(wǎng)站營(yíng)銷推廣,域名注冊(cè),網(wǎng)頁(yè)空間,成都網(wǎng)站托管有關(guān)企業(yè)網(wǎng)站制作方案、改版、費(fèi)用等問題,請(qǐng)聯(lián)系創(chuàng)新互聯(lián)。
<?php
class SnowFlake
{
const TWEPOCH = 0; // 時(shí)間起始標(biāo)記點(diǎn),作為基準(zhǔn),一般取系統(tǒng)的最近時(shí)間(一旦確定不能變動(dòng))
const WORKER_ID_BITS = 5; // 機(jī)器標(biāo)識(shí)位數(shù)
const DATACENTER_ID_BITS = 5; // 數(shù)據(jù)中心標(biāo)識(shí)位數(shù)
const SEQUENCE_BITS = 12; // 毫秒內(nèi)自增位
private $workerId; // 工作機(jī)器ID
private $datacenterId; // 數(shù)據(jù)中心ID
private $sequence; // 毫秒內(nèi)序列
private $maxWorkerId = -1 ^ (-1 << self::WORKER_ID_BITS); // 機(jī)器ID最大值
private $maxDatacenterId = -1 ^ (-1 << self::DATACENTER_ID_BITS); // 數(shù)據(jù)中心ID最大值
private $workerIdShift = self::SEQUENCE_BITS; // 機(jī)器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í)間毫秒左移位數(shù)
private $sequenceMask = -1 ^ (-1 << self::SEQUENCE_BITS); // 生成序列的掩碼
private $lastTimestamp = -1; // 上次生產(chǎn)id時(shí)間戳
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)生的時(shí)間戳小于上次的生成的時(shí)間戳?xí)r,報(bào)錯(cuò)
$diffTimestamp = bcsub($this->lastTimestamp, $timestamp);
throw new Exception("Clock moved backwards. Refusing to generate id for {$diffTimestamp} milliseconds");
}
if ($this->lastTimestamp == $timestamp) {//當(dāng)生成的時(shí)間戳等于上次生成的時(shí)間戳的時(shí)候
$this->sequence = ($this->sequence + 1) & $this->sequenceMask;//序列自增一次
if (0 == $this->sequence) {//當(dāng)序列為0時(shí),重新生成最新的時(shí)間戳
$timestamp = $this->createNextTimestamp($this->lastTimestamp);
}
} else {//當(dāng)生成的時(shí)間戳不等于上次的生成的時(shí)間戳的時(shí)候,序列歸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) //生成一個(gè)大于等于 上次生成的時(shí)間戳 的時(shí)間戳
{
$timestamp = $this->createTimestamp();
while ($timestamp <= $lastTimestamp) {
$timestamp = $this->createTimestamp();
}
return $timestamp;
}
protected function createTimestamp()//生成毫秒級(jí)別的時(shí)間戳
{
return floor(microtime(true) * 1000);
}
}
?>到此,相信大家對(duì)“怎么用PHP實(shí)現(xiàn)雪花算法”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
網(wǎng)頁(yè)標(biāo)題:怎么用PHP實(shí)現(xiàn)雪花算法
鏈接地址:http://chinadenli.net/article6/gospig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、搜索引擎優(yōu)化、網(wǎng)站制作、外貿(mào)建站、網(wǎng)站策劃、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)