PHP code example of peibin / laravel-leaf

1. Go to this page and download the library: Download peibin/laravel-leaf library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

peibin / laravel-leaf example snippets


// 初始化,定义成单例
$this->app->singleton(SegmentIDGenImpl::class, function ($app) {
    $idGen = new SegmentIDGenImpl($app);
    $idGen->setModel(new LeafAllocModel());
    $idGen->init();
    return $idGen;
});

// 调用方法
function segmentId(string $tag): Result
{
    $segment = app()->make(SegmentIDGenImpl::class)->get($tag);
    if ($segment->getStatus() == Result::STATUS_EXCEPTION) {
        throw new \RuntimeException('The generation number segment id is abnormal. CODE:' . $segment->getId());
    }
    return $segment;
}

$result = segmentId('test_tag');
sql
SELECT `biz_tag`, `max_id`, `step`, `update_time`
FROM `leaf_alloc`
WHERE biz_tag = 'TagName';