Skip to content

kissthink/chocoflake

 
 

Repository files navigation

chocoflake

Build Status Coverage Status

64bit ID Generator for PHP

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

Usage

Prerequisites

  • PHP 5.4 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

  • 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.

SharedMemory version

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

Redis version

Using Redis atomic increment operation to count up sequence.

ID Specification

The IDs consist of four elements:

  • timestamp
  • region id
  • server id
  • sequence

You can specify any bit length to each element.

License

This software is released under the MIT License, see LICENSE

About

chocoflake is an implementation of twitter Snowflake concept.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 95.1%
  • JavaScript 4.9%