ada-u/chocoflake 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ada-u/chocoflake

最新稳定版本:v1.0.0

Composer 安装命令:

composer require ada-u/chocoflake

包简介

64bit time based id generator

README 文档

README

Build Status Github Actions 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 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

统计信息

  • 总下载量: 117.62k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-12-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固