Browse Source

add README.md

pull/2/head
Albert 4 years ago
parent
commit
5bc17a02b7
3 changed files with 63 additions and 1 deletions
  1. +50
    -0
      PHP/README.md
  2. +2
    -0
      PHP/php_snowdrift.h
  3. +11
    -1
      PHP/snowdrift.c

+ 50
- 0
PHP/README.md View File

@@ -0,0 +1,50 @@
# ❄ idenerator-php-extension

## 介绍
项目更多介绍参照:https://github.com/yitter/idgenerator

## PHP环境

* PHP7 or later

## 安装方式

```shell
git clone https://gitee.com/yitter/idgenerator.git
cd idgenerator/PHP
phpize
./configure --with-php-config=/path/php-config
make
make install
```

## 如何使用(PHP)

**配置文件配置参数**:
```shell
//snowdrift.ini
snowdrift.Method=1 //1 漂移算法 2 传统算法
snowdrift.BaseTime=1582136402000
snowdrift.WorkerId=1 //默认workerid
snowdrift.WorkerIdNum=1 //支持的WorkerId数量,默认1,不超过(-1L << snowdrift.WorkerIdBitLength) ^ -1L
snowdrift.WorkerIdBitLength=6
snowdrift.SeqBitLength=6 //自增序号位数
snowdrift.MaxSeqNumber=0
snowdrift.MinSeqNumber=0
snowdrift.TopOverCostCount=2000 //最大漂移次数
```

**函数签名**:
```php
\SnowDrift::NextId(int $wid=snowdrift.WorkerId):?int //获取单个id,$wid可选,默认值=snowdrift.WorkerId
\SnowDrift::NextNumId(int $num, int $wid=snowdrift.WorkerId):?array //获取$num个id,$wid可选,默认值=snowdrift.WorkerId
```

**调用示例**
```php
$id=\SnowDrift::NextId();
$id=\SnowDrift::NextId(3);

$ids=\SnowDrift::NextNumId(10000);
$ids=\SnowDrift::NextNumId(10000,3);
```

+ 2
- 0
PHP/php_snowdrift.h View File

@@ -57,6 +57,8 @@ uint8_t WorkerId;
uint8_t WorkerIdNum; uint8_t WorkerIdNum;
uint8_t WorkerIdBitLength; uint8_t WorkerIdBitLength;
uint8_t SeqBitLength; uint8_t SeqBitLength;
uint32_t MaxSeqNumber;
uint32_t MinSeqNumber;
uint16_t TopOverCostCount; uint16_t TopOverCostCount;


ZEND_END_MODULE_GLOBALS(snowdrift) ZEND_END_MODULE_GLOBALS(snowdrift)


+ 11
- 1
PHP/snowdrift.c View File

@@ -41,11 +41,13 @@ uint8_t num = 0;


PHP_INI_BEGIN() PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("snowdrift.Method", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, Method, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.Method", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, Method, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.BaseTime", "1577808000000", PHP_INI_SYSTEM, OnUpdateLongGEZero, BaseTime, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.BaseTime", "1582136402000", PHP_INI_SYSTEM, OnUpdateLongGEZero, BaseTime, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.WorkerId", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerId, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.WorkerId", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerId, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.WorkerIdNum", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerIdNum, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.WorkerIdNum", "1", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerIdNum, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.WorkerIdBitLength", "6", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerIdBitLength, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.WorkerIdBitLength", "6", PHP_INI_SYSTEM, OnUpdateLongGEZero, WorkerIdBitLength, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.SeqBitLength", "6", PHP_INI_SYSTEM, OnUpdateLongGEZero, SeqBitLength, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.SeqBitLength", "6", PHP_INI_SYSTEM, OnUpdateLongGEZero, SeqBitLength, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.MaxSeqNumber", "0", PHP_INI_SYSTEM, OnUpdateLongGEZero, MaxSeqNumber, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.MinSeqNumber", "0", PHP_INI_SYSTEM, OnUpdateLongGEZero, MinSeqNumber, zend_snowdrift_globals, snowdrift_globals)
STD_PHP_INI_ENTRY("snowdrift.TopOverCostCount", "2000", PHP_INI_SYSTEM, OnUpdateLongGEZero, TopOverCostCount, zend_snowdrift_globals, snowdrift_globals) STD_PHP_INI_ENTRY("snowdrift.TopOverCostCount", "2000", PHP_INI_SYSTEM, OnUpdateLongGEZero, TopOverCostCount, zend_snowdrift_globals, snowdrift_globals)
PHP_INI_END() PHP_INI_END()


@@ -62,6 +64,12 @@ static int snowdrift_init()
if (shm_alloc(&shmctx) == -1) if (shm_alloc(&shmctx) == -1)
{ {
zend_throw_exception_ex(NULL, 0, "shared memory malloc failed"); zend_throw_exception_ex(NULL, 0, "shared memory malloc failed");
RETURN_FALSE;
}
if (SD_G(MaxSeqNumber) <= SD_G(MinSeqNumber))
{
zend_throw_exception_ex(NULL, 0, "MaxSeqNumber must GE then MinSeqNumber");
RETURN_FALSE;
} }
bzero(shmctx.addr, num * sizeof(snowflake)); bzero(shmctx.addr, num * sizeof(snowflake));
sf = (snowflake *)shmctx.addr; sf = (snowflake *)shmctx.addr;
@@ -73,6 +81,8 @@ static int snowdrift_init()
tmp->WorkerId = i + 1; tmp->WorkerId = i + 1;
tmp->WorkerIdBitLength = SD_G(WorkerIdBitLength); tmp->WorkerIdBitLength = SD_G(WorkerIdBitLength);
tmp->SeqBitLength = SD_G(SeqBitLength); tmp->SeqBitLength = SD_G(SeqBitLength);
tmp->MaxSeqNumber = SD_G(MaxSeqNumber);
tmp->MinSeqNumber = SD_G(MinSeqNumber);
tmp->TopOverCostCount = SD_G(TopOverCostCount); tmp->TopOverCostCount = SD_G(TopOverCostCount);
Config(tmp); Config(tmp);
} }


Loading…
Cancel
Save