format changed
parent
a4f80d8b49
commit
4f7900ab2b
|
|
@ -1,8 +1,7 @@
|
|||
|
||||
# 64位ID⽣成器说明
|
||||
|
||||
我们在libfastcommon中实现了64位( 8字节整数) ID⽣成器,针对PHP这样的多进程⽅式,⽣成
|
||||
的64位ID也可以做到全局唯⼀。
|
||||
我们在libfastcommon中实现了64位( 8字节整数) ID⽣成器,针对PHP这样的多进程⽅式,⽣成的64位ID也可以做到全局唯⼀。
|
||||
|
||||
提供的PHP扩展php-fastcommon,封装了64位ID⽣成器。
|
||||
|
||||
|
|
@ -16,32 +15,33 @@
|
|||
```
|
||||
|
||||
## php-fastcommon扩展提供的4个PHP函数
|
||||
|
||||
```
|
||||
resource fastcommon_id_generator_init([string $filename = "/tmp/fastcommon_id_generator.sn",
|
||||
int $machine_id = 0, int $mid_bits = 16, int $extra_bits = 0, int $sn_bits = 16])
|
||||
return resource handle for success, false for fail
|
||||
```
|
||||
* 这个函数只需要在初始化的时候调⽤⼀次即可,建议不同的实例采⽤不同的⽂件来保存序列号。
|
||||
* php程序运⾏⽤户对这个⽂件必须有读写权限,⽂件不存在会⾃动创建。
|
||||
* 返回的resoure需要保存到php变量,否则该初始化⼯作会⾃动撤销
|
||||
```
|
||||
|
||||
```
|
||||
long/string fastcommon_id_generator_next([int $extra = 0, resource $handle = null])
|
||||
return id for success, false for fail
|
||||
return long in 64 bits OS, return string in 32 bits OS
|
||||
```
|
||||
* 如果不需要存储额外信息, extra传0即可。
|
||||
* 其中$handle参数为 fastcommon_id_generator_init返回值,不传递该参数表⽰使⽤最后⼀次调⽤
|
||||
```
|
||||
|
||||
```
|
||||
fastcommon_id_generator_init 返回的handle。
|
||||
int fastcommon_id_generator_get_extra(long id [, resource $handle = null])
|
||||
return the extra data
|
||||
```
|
||||
* 使⽤了额外信息的情况下,可以使⽤这个函数获取ID中包含的extra data
|
||||
```
|
||||
|
||||
```
|
||||
bool fastcommon_id_generator_destroy([resource $handle = null])
|
||||
return true for success
|
||||
```
|
||||
* 这个函数通常不需要显式调⽤
|
||||
```
|
||||
|
|
|
|||
Loading…
Reference in New Issue