1
|
|
|
<?php |
2
|
|
|
namespace Fwolf\Util\Uuid; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* UUID generator using base-36 character, short version |
6
|
|
|
* |
7
|
|
|
* {@see Base36} algorithm is good, but the uuid is still too long. For small |
8
|
|
|
* scale system, we may need this short version. |
9
|
|
|
* |
10
|
|
|
* The second and microsecond part remain same length and algorithm, to keep |
11
|
|
|
* maximum usage of PHP microtime precision, so the lifetime and timestamp |
12
|
|
|
* offset are also same. |
13
|
|
|
* |
14
|
|
|
* As a small scale, we reduce: |
15
|
|
|
* - length of group from 2 to 1, maximum 36 servers |
16
|
|
|
* - length of custom from 7 to 0 |
17
|
|
|
* - length of random from 6 to 5 |
18
|
|
|
* |
19
|
|
|
* Random part need to ensure no duplicate in 0.000001 second, we use decimal |
20
|
|
|
* part of {@see uniqid()} with more_entropy enabled. as its integer part is |
21
|
|
|
* generated base on microtime. We got a 8 digit number, enough to avoid dup in |
22
|
|
|
* 1 microsecond. The largest number 99,999,999 in base36 is '1n.chr' cost 6 |
23
|
|
|
* bytes, so we divide it by 2 to use 5 bytes storage, should be enough too. |
24
|
|
|
* |
25
|
|
|
* Length of UUID is 16 bytes, no separator. |
26
|
|
|
* |
27
|
|
|
* |
28
|
|
|
* For safe speed in 1 microsecond (not millisecond), reference: |
29
|
|
|
* |
30
|
|
|
* @see http://github.com/mumrah/flake-java 1k / millisecond = 1/ms |
31
|
|
|
* @see https://github.com/twitter/snowflake/tree/snowflake-2010 |
32
|
|
|
* 0.5/ms |
33
|
|
|
* |
34
|
|
|
* |
35
|
|
|
* @copyright Copyright 2015-2016 Fwolf |
36
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
37
|
|
|
*/ |
38
|
|
|
class Base36Short extends Base36 |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
const LENGTH = 16; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
const LENGTH_SECOND = 6; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
const LENGTH_MICROSECOND = 4; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
const LENGTH_GROUP = 1; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
const LENGTH_CUSTOM = 0; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
const LENGTH_RANDOM = 5; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
const LENGTH_CHECK_DIGIT = 0; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
const SEPARATOR = ''; |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
protected function generateRandomPart() |
85
|
|
|
{ |
86
|
|
|
$decimalPart = substr(uniqid('', true), -8); |
87
|
|
|
|
88
|
|
|
$decimalPart = round($decimalPart / 2); |
89
|
|
|
|
90
|
|
|
$encoded = base_convert($decimalPart, 10, 36); |
91
|
|
|
|
92
|
|
|
$random = str_pad($encoded, static::LENGTH_RANDOM, '0', STR_PAD_LEFT); |
93
|
|
|
|
94
|
|
|
return $random; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|