<?php class SnowFlake { const TWEPOCH = 1288834974657; //
Time start marker , As a benchmark , Generally, the latest time of the system is taken ( Once it's confirmed, it can't be changed ) const WORKER_ID_BITS = 5; // Machine identification number const
DATACENTER_ID_BITS = 5; // Data center identification bits const SEQUENCE_BITS = 12; // Self increasing bit in MS private
$workerId; // Working machine ID private $datacenterId; // Data center ID private $sequence; //
Millisecond sequence private $maxWorkerId = -1 ^ (-1 << self::WORKER_ID_BITS); // machine ID Maximum
private $maxDatacenterId = -1 ^ (-1 << self::DATACENTER_ID_BITS); // Data center ID Maximum
private $workerIdShift = self::SEQUENCE_BITS; // machine ID Shift to left private
$datacenterIdShift = self::SEQUENCE_BITS + self::WORKER_ID_BITS; // Data center ID Shift left
private $timestampLeftShift = self::SEQUENCE_BITS + self::WORKER_ID_BITS +
self::DATACENTER_ID_BITS; // Time MS shift left private $sequenceMask = -1 ^ (-1 <<
self::SEQUENCE_BITS); // The mask of the generated sequence private $lastTimestamp = -1; // Last production id time stamp
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 nextId() { $timestamp =
$this->timeGen(); 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) { $timestamp =
$this->tilNextMillis($this->lastTimestamp); } } else { $this->sequence = 0; }
$this->lastTimestamp = $timestamp; /*$gmpTimestamp =
gmp_init($this->leftShift(bcsub($timestamp, self::TWEPOCH),
$this->timestampLeftShift)); $gmpDatacenterId =
gmp_init($this->leftShift($this->datacenterId, $this->datacenterIdShift));
$gmpWorkerId = gmp_init($this->leftShift($this->workerId,
$this->workerIdShift)); $gmpSequence = gmp_init($this->sequence); return
gmp_strval(gmp_or(gmp_or(gmp_or($gmpTimestamp, $gmpDatacenterId),
$gmpWorkerId), $gmpSequence));*/ return (($timestamp - self::TWEPOCH) <<
$this->timestampLeftShift) | ($this->datacenterId << $this->datacenterIdShift)
| ($this->workerId << $this->workerIdShift) | $this->sequence; } protected
function tilNextMillis($lastTimestamp) { $timestamp = $this->timeGen(); while
($timestamp <= $lastTimestamp) { $timestamp = $this->timeGen(); } return
$timestamp; } protected function timeGen() { return floor(microtime(true) *
1000); } // Shift left << protected function leftShift($a, $b) { return bcmul($a,
bcpow(2, $b)); } }
 
 

Technology
©2020 ioDraw All rights reserved
Swing actual combat Understanding closure about vue in el-date-picker type=daterange The problem of date not echoing Ubuntu 18.04 swap Partition expansion Snake code --c Language Edition visual c++6.0 open 【Golang Basic series 10 】Go language On conditional sentences ifString class —— summary ,String The nature of , Memory resolution , Commonly used APIAdobe Illustrator Publish native support Apple Silicon Test version of the software study java My first class antd table sort