snowflake算法php,php_snowflake

本文介绍了PHP实现的Twitter SnowFlake算法,这是一个分布式ID生成器。该实现分为线程安全和非线程安全两个版本,由于PHP自身的特性,无法直接实现SnowFlake算法的纯PHP版本。文中提供了安装示例和代码片段,展示了如何生成唯一的64位ID,ID由时间戳、服务编号、进程ID和序列号组成。
摘要由CSDN通过智能技术生成

php_snowflake

What is php_snowflake?

Twitter distributed ID generating Algorithm SnowFlake PHP implementation version.

Requires

PHP >= 5.6 (Below 5.5 self-testing)

Linux

Description

Pure PHP can not be achieved SnowFlake algorithm,because version of thread-safe(requires tid)and

non-thread-safe(requires pid)can not generate same the format id(The only tid of this project system,

so don't worried about pid conflict of multiple processes). And PHP as scripting language can not maintain sequence,

sequence will be initialized again when the script is finished. In case of high concurrency pure PHP

Implemented version of Snowflate algorithm does not have any effect. So was born this project,

below is the different of Thread-safe version and Non-thread-safe version.

Non-thread-safe version (NTS)

0 2       15        20 28    32

---+----------------+--------------+----+----------+

00 |timestamp(ms) | service_no   |pid | sequence |

---+----------------+--------------+----+----------+

Thread-safe version (TS)

0 2        15        20 28    32

---+----------------+--------------+----+----------+

00 |timestamp(ms) | service_no   |tid | sequence |

---+----------------+--------------+----+----------+

Installation

phpize

./configure --with-php-config=/you/phppath/php-config

make

make install

Example

Attention: Interval of $service_no in the range of 0-99999. Beyond that scope, PHP will report a fatal mistake.

$service_no = 999;

for ($i=0; $i < 10; $i++) {

echo PhpSnowFlake::nextId($service_no)."\n";

}

/*

00146523488416500999000634280001

00146523488416500999000634280002

00146523488416500999000634280003

00146523488416500999000634280004

00146523488416500999000634280005

00146523488416600999000634280001

00146523488416600999000634280002

00146523488416600999000634280003

00146523488416600999000634280004

00146523488416600999000634280005

*/

License

Copyright (c) 2016 by Towers released under MIT License.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值