Skip to content

ieu/snowflake-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snowflake

Please Note that this is not concurrency-safe.

Global ID generator based on Twitter Snowflake.

The generated ID is structured 64 bit integer:

 0           41     51     64
 +-----------+------+------+
 |timestamp  |worker|seq   |
 +-----------+------+------+

timestamp is 41 bit integer presents milliseconds since 11/04/2010 01:42:54.657 GMT.

worker is 10 bit integer presents worker ID the generator is running on

seq is 12 bit integer presents incremental sequence per millisecond

Installation

First add this repo to composer.json:

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/ieu/snowflake-php"
        }
    ]
}

Then install it through composer:

composer require ieu/snowflake:dev-master

Usage

require __DIR__ . '/vendor/autoload.php';

use Ieu\Snowflake\Snowflake;

$datacenterId = 1;
$workerId = 1;

$snowflake = new Snowflake($datacenterId, $workerId);

// Getting Id
$id = $snowflake->nextId();

Tests

./vendor/bin/phpunit

About

Global ID generator based on Snowflake for PHP

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages