smorken/sequence-generator

近似唯一的序列生成器

v1.3 2020-09-03 13:34 UTC

This package is auto-updated.

Last update: 2024-08-29 05:13:08 UTC


README

默认情况下,基于(F64工厂)生成一个64位整数

  • 32位时间戳(默认纪元为2020-01-01 00:00:00)
  • 基于客户端IP和可选提供的整数的24位半唯一标识符(这是基于部分哈希,因此不能保证唯一)
  • 每个标识符每秒旋转8位序列(每个标识符每秒256个ID)

F48工厂

  • 30位时间戳
  • 12位标识符
  • 6位序列(每个标识符每秒64个ID)

Composer

安装

$ composer require smorken/sequence-generator

Laravel

当包含在composer中时应该自动加载。如果没有,请将服务提供者添加到config/app.php

...
    Smorken\SeqGen\ServiceProvider::class,
...

通过DI或从容器中检索获取实例

$factory = app(\Smorken\SeqGen\Contracts\Factory::class);
$id = $factory->create('127.0.0.1');
//or with optional secondary identifier
$id = $factory->create('127.0.0.1', 12345678);

独立

$cache = new PSR16CacheProvider();
$if = new \Smorken\SeqGen\Identifiers\Factory(new \Smorken\SeqGen\Identifiers\Ip(), new \Smorken\SeqGen\Identifiers\IntVal());
$s = new \Smorken\SeqGen\Sequence();
$t = new \Smorken\SeqGen\Timestamp('2020-01-01 00:00:00');
$factory = new \Smorken\SeqGen\Factories\F64($cache, $if, $s, $t);
$id = $factory->create('127.0.0.1');
//or with optional secondary identifier
$id = $factory->create('127.0.0.1', 12345678);