Skip to content

Commit 5de7385

Browse files
author
Justin007
committed
workerId update
1 parent 8c05815 commit 5de7385

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

IdGenerator.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ class IdGenerator
1515
{
1616

1717
const TIMESTAMP_SHITF = 16;
18-
const SEED_SHITF = 12;
18+
const WORKER_SHITF = 12;
1919

20+
protected $workerId;
2021

21-
public function __construct()
22+
23+
public function __construct($workerId = 1)
2224
{
25+
$this->workerId = $workerId;
2326
}
2427

2528
protected function timestamp()
@@ -31,17 +34,17 @@ public function next()
3134
{
3235
$timestamp = $this->timestamp();
3336
return ($timestamp << self::TIMESTAMP_SHITF)
34-
| ($this->getSeed() << self::SEED_SHITF)
37+
| ($this->getWorkerId() << self::SEED_SHITF)
3538
| $this->getCount($timestamp);
3639
}
3740

3841
/**
3942
* 4bit for seed, 可用于业务和环境区分
4043
* @return int
4144
*/
42-
public function getSeed()
45+
public function getWorkerId()
4346
{
44-
return 1;
47+
return $this->workerId;
4548
}
4649

4750
/**

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# distributeIdGenerator
2-
依赖 Illuminate\Support\Facades\Redis
2+
分布式id生成器,用于分布式环境序列号,订单号生成等
3+
依赖 Illuminate\Support\Facades\Redis

0 commit comments

Comments
 (0)