ada-u / chocoflake
64bit time based id generator
Installs: 109 896
Dependents: 0
Suggesters: 0
Security: 0
Stars: 14
Watchers: 2
Forks: 3
Open Issues: 1
Requires
- php: >=8.0
- ext-sysvsem: *
- ext-sysvshm: *
- mockery/mockery: 1.4.*
- predis/predis: 1.1.*
Requires (Dev)
- fabpot/php-cs-fixer: dev-master
- phpunit/phpunit: ^9
- dev-master
- v1.0.0
- 0.0.1
- dev-dependabot/composer/predis/predis-1.1.staror-2.0.staror-2.1.staror-2.2.staror-2.3.star
- dev-snyk-fix-3127708a5ad80c8c5fe3b75ffc58c19d
- dev-snyk-fix-898b46e52f567fd13fb6f8745ce31be4
- dev-snyk-fix-12b472db04f6a9332be5a41ab725c484
- dev-snyk-fix-dfd8d7eab8ce5c3129b219f51a773a85
- dev-snyk-fix-d5a0fa2ba33377fce539d26ca5d37b52
- dev-snyk-fix-c90f263ba6fee85467bcfcb05377dabb
- dev-snyk-fix-4983e12bd6f2b08e162aaa977df53a6d
- dev-snyk-fix-6ef0402b16092a7a77aa5aec0359df85
- dev-snyk-fix-550b6a09a951581c428f8f31621ca32d
- dev-snyk-fix-19ecf7eb222c691ffb1d62874782b64f
- dev-snyk-fix-7a004071dba145b80c19f4ec9337bb26
- dev-snyk-fix-551c8f88d8256c5291bb7e96858619fb
- dev-snyk-fix-1ea051b75343ddbe2f0235d2efdae40b
- dev-snyk-fix-da14a42eb1c0eff574e6bbe184af8d3b
- dev-snyk-fix-7e6875ac564ee3152e85a15912aef44b
- dev-snyk-fix-972c0a1aee84f9361efbba46544f65b3
This package is auto-updated.
Last update: 2024-12-22 12:40:31 UTC
README
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 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
- 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.
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