Skip to content

Commit

Permalink
refactor(ID): 调整工作机和数据中心的位数- 将 workerIdBits 和 datacenterIdBits 的默认值从 …
Browse files Browse the repository at this point in the history
…10 和 12 修改为 5 和 5

- 优化了 ID 生成逻辑,提高了可用性和可读性
  • Loading branch information
liushoukun committed Jan 2, 2025
1 parent f651ade commit 766ad8a
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/support/src/Helpers/ID/DatetimeIdGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public static function getInstance(
public static function buildId(
int|null $workerId = null,
int|null $datacenterId = null,
int $workerIdBits = 10,
int $datacenterIdBits = 12,
int $workerIdBits = 5,
int $datacenterIdBits = 5,
int $sequenceBits = 12,
) : string {
return static::getInstance($workerId, $datacenterId, $workerIdBits, $datacenterIdBits, $sequenceBits)->nextId();
Expand Down Expand Up @@ -112,11 +112,11 @@ public function nextId() : string

// 17 位
$datetime = date('YmdHis', (int) (floor($timestamp / 1000))).substr((int) $timestamp, -3);
// 序号 8 位


$sequence = (($this->datacenterId << $this->datacenterIdShift) |
($this->workerId << $this->workerIdShift) |
$this->sequence);
// php 给定一个整数 如何获取整数的 位数

$len = strlen(pow(2, ($this->datacenterIdShift + $this->datacenterIdBits)));
$sequenceNumber = sprintf("%0{$len}d", $sequence);
Expand Down

0 comments on commit 766ad8a

Please sign in to comment.