2017 © Pedro Peláez
 

library chocoflake

64bit time based id generator

image

ada-u/chocoflake

64bit time based id generator

  • Tuesday, December 23, 2014
  • by ada-u
  • Repository
  • 1 Watchers
  • 7 Stars
  • 3,543 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 140 % Grown

The README.md

chocoflake

Build Status Github Actions Coverage Status, (*1)

64bit ID Generator for PHP

chocoflake is an implementation of twitter Snowflake concept. This provides generating IDs based on time in a distributed environment., (*2)

Usage

Prerequisites

  • PHP 8.0 or later

Installation

Command

$ composer.phar require ada-u/chocoflake:dev-master

composer.json

{
  "require": {
    "ada-u/chocoflake": "dev-master"
  }
}
$ composer.phar install

Sample

Configuration, (*3)

  • 41 bit - for timestamp
  • 5 bit - for region id
  • 5 bit - for server id
  • 12 bit - for sequence per milliseconds
  • 1414334507356 - service start epoch (unix timestamp)
$config = new IdConfig(41, 5, 5, 12, 1414334507356);
$service = new ChocoflakeService($config);

$worker = $service->createIdWorkerOnSharedMemory(new RegionId(1), new ServerId(1));

$id = $worker->generate();
4194439168

ID Generator

I implemented two ID generators, Redis and SharedMemory version., (*4)

SharedMemory version

Using shared memory and semaphore (as mutex) to prevent multiple processes are in the critical section at the same time., (*5)

Redis version

Using Redis atomic increment operation to count up sequence., (*6)

ID Specification

The IDs consist of four elements:, (*7)

  • timestamp
  • region id
  • server id
  • sequence

You can specify any bit length to each element., (*8)

Test

$ docker-compose run php vendor/bin/phpunit
Creating chocoflake_php_run ... done
PHPUnit 9.5.6 by Sebastian Bergmann and contributors.

....                                                                4 / 4 (100%)

Time: 00:00.353, Memory: 6.00 MB

OK (4 tests, 4 assertions)

License

This software is released under the MIT License, see LICENSE, (*9)

The Versions

23/12 2014

dev-master

9999999-dev https://github.com/ada-u/chocoflake

64bit time based id generator

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar ada-u

generator id chocoflake idgen

15/12 2014

0.0.1

0.0.1.0 https://github.com/ada-u/chocoflake

64bit time based id generator

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Avatar ada-u

generator id chocoflake idgen