glhd/bits 问题修复 & 功能扩展

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

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

glhd/bits

Composer 安装命令:

composer require glhd/bits

包简介

关键字:

README 文档

README

Build Status Latest Stable Release MIT Licensed Follow @cmorrell.com on bsky

Bits

Bits is a PHP library for generating unique 64-bit identifiers for use in distributed computing. You can use Bits to create Twitter Snowflake IDs, Sonyflake IDs, or any other unique ID that uses bit sequences.

Installation

composer require glhd/bits

Configuration

Warning

If you do not configure the worker ID and datacenter ID, you may run into concurrency issues using Bits on multiple app servers.

There are two things you will need to configure to ensure that your IDs are valid and unique.

Set the BITS_WORKER_ID and BITS_DATACENTER_ID

Snowflakes are so compact because they rely on the fact that each worker has its own ID. If you are running multiple servers in multiple datacenters, you need to give each a unique value. You need to set a unique BITS_DATACENTER_ID (0-31) for each datacenter your application uses, and each worker in the same datacenter should have a unique BITS_WORKER_ID (0-31). This means that you can have, at most, 1024 separate workers generating snowflakes at the same exact moment in time.

Note

If you use Lambda, this can be an issue. Eventually, we hope to have a serverless solution for Bits, but right now you need to manage locking/releasing IDs yourself if you're generating snowflakes in something like Laravel Vapor.

Set the BITS_EPOCH

Another reason snowflakes are compact is because they use a custom "epoch" value (rather than the Unix epoch of January 1, 1970). This is the earliest a snowflake can be generated, and also set the limit to how far in the future snowflakes can be generated. By default, this value is 2023-01-01, which should be fine for most systems. But if you're going to use time-travel to before January 2023 in your tests, this may cause problems (in which case you should set your epoch to before the earliest moment you would time-travel).

Usage

To get a new snowflake ID, simply call Snowflake::make(). This returns a new Snowflake object:

class Snowflake
{
    public readonly int $timestamp;
    public readonly int $datacenter_id;
    public readonly int $worker_id;
    public readonly int $sequence;
    
    public function id(): int;
    public function is(Snowflake $other): bool;
}

You can also use the snowflake() or sonyflake() global helper functions, if you prefer.

All Bits IDs implement __toString() and the Laravel Query\Expression interface so that you can easily pass them around without juggling types.

Usage with JavaScript

It's important to note that JavaScript only supports ~52-bit integers so if you're passing Snowflakes to JavaScript, be sure to cast them to a string.

All Bits IDs implement Jsonable and JsonSerializable, so if you pass a Bits instance to json_encode it will automatically handle this for you. But if you're using integer IDs that were generated by Bits, you will have to cast them yourself.

Usage with Eloquent Models

Bits provides a HasSnowflakes trait that behaves the same as Eloquent’s HasUuids and HasUlids traits. Simply add HasSnowflakes to your model, and whenever they're inserted or upserted, a new Snowflake will be generated for you.

You can also use Snowflake or Sonyflake as in your Eloquent $casts array to have that attribute automatically cast to a Bits instance.

use Glhd\Bits\Database\HasSnowflakes;
use Glhd\Bits\Snowflake;
use Illuminate\Database\Eloquent\Model;

class Example extends Model
{
    // Auto-generate Snowflake for new models
    use HasSnowflakes;
    
    // Any attribute can be cast to a `Snowflake` (or `Sonyflake`)
    protected $casts = [
        'id' => Snowflake::class,
    ];
}

$example = Example::create();

$example->id instanceof Snowflake; // true

echo $example->id; // 65898467809951744

Using IDs as timestamps

One nice property of Snowflakes and Sonyflakes is that they are sortable by time, and can have the timestamp extracted from the ID for later use. This means that if you want to, you can use these IDs in place of a created_at timestamp:

// Instead of:
User::where('created_at', '>', now());

// You can do (assuming users.id is a snowflake)
User::where('id', '>', app(MakesSnowflakes::class)->firstForTimestamp(now()));
// Instead of:
$created_at = $user->created_at;

// You can do (assuming User::id is cast to a Snowflake object)
$created_at = $user->id->toCarbon();

Usage with Livewire

If you're using Livewire, you can use the SnowflakeSynth synthesizer to make Snowflakes usable in your components.

All you need to do is to register the synthesizer in your AppServiceProvider:

use Glhd\Bits\Support\Livewire\SnowflakeSynth;
use Glhd\Bits\Support\Livewire\BitsSynth;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        // If only using Snowflakes:
        Livewire::propertySynthesizer(SnowflakeSynth::class);
        
        // If using Sonyflakes or a custom Bits variant:
        Livewire::propertySynthesizer(BitsSynth::class);
    }
}

If you're using a non-Snowflake variant of Bits, you can use the BitsSynth instead, which supports any version of Bits at the cost of storing a little more data.

About 64-bit Unique IDs

Snowflake format

0 0000001100100101110101100110111100101011 01011 01111 000000011101
┳ ━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━ ━━━┳━ ━┳━━━ ━┳━━━━━━━━━━
┗━ unused bit     ┗━ timestamp (41)           ┃   ┃     ┗━ sequence (12)
                              datacenter (5) ━┛   ┗━ worker (5)

Sonyflake format

0 000000011001001011101011001101111001010 11010110 1111000000011101
┳ ━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━━━┳━ ━┳━━━━━━━━━━━━━━
┗━ sign   ┗ timestamp (39)         sequence (8) ┛   ┗ machine (16)

Both of these IDs are represented by the same 64-bit integer, 56705782302306333, but convey different metadata. Depending on your scale and distribution needs, you may find one or the other format preferable, or choose to implement your own custom format.

Bits lets generate any kind of 64-bit unique ID you'd like, in the way that makes the most sense for your use-case.

glhd/bits 适用场景与选型建议

glhd/bits 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 449.64k 次下载、GitHub Stars 达 92, 最近一次更新时间为 2023 年 06 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 glhd/bits 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 glhd/bits 我们能提供哪些服务?
定制开发 / 二次开发

基于 glhd/bits 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 449.64k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 92
  • 点击次数: 16
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 92
  • Watchers: 2
  • Forks: 18
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-06