Download the PHP package grantholle/scru64 without Composer
On this page you can find all versions of the php package grantholle/scru64. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download grantholle/scru64
More information about grantholle/scru64
Files in grantholle/scru64
Package scru64
Short Description An implementation for generating SCRU64 ID's
License MIT
Homepage https://github.com/grantholle/scru64
Informations about the package scru64
SCRU64 for PHP
A PHP implementation of SCRU64: Sortable, Clock-based, Realm-specifically Unique identifiers.
- 63-bit non-negative integer — fits in a signed
BIGINT - Sortable by generation time, both as integer and as text
- 12-digit case-insensitive Base36 text (
0u375nxqh5cq) - 256 ms timestamp resolution, usable until year 4261
- Node ID and counter share 24 bits; size is configurable per node
Uniqueness depends on each generator being given a unique node ID. If you can't coordinate node IDs, use SCRU128 instead.
Installation
Requires PHP 8.4+ on a 64-bit platform.
Usage
Global generator
Set SCRU64_NODE_SPEC in the environment and call the static facade:
The environment variable is only needed by the facade. If you'd rather configure it in code (e.g. from your app's config during bootstrap), set the generator explicitly and the env var is ignored:
Scru64::generate() throws a RuntimeException if neither is provided.
Node spec
A node spec is <node_id>/<node_id_size>, where node_id_size is 1–23 bits:
| Spec | Meaning |
|---|---|
42/8 |
Decimal node ID 42 using 8 bits (16-bit counter) |
0xb00/12 |
Hex node ID using 12 bits (12-bit counter) |
0u2r85hm2pt3/16 |
Continue after a previously generated ID, using 16 node bits |
Fewer node bits → more IDs per 256 ms tick per node. More node bits → more nodes.
Explicit generator
generate() never blocks. When the counter overflows within a tick, the timestamp is advanced by one. Clock rollbacks of up to ~10 seconds are absorbed; a larger rollback throws a RuntimeException.
Working with IDs
Store $id->value in a BIGINT column or (string) $id in a CHAR(12) column; both sort chronologically.
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Grant Holle
- SCRU64 specification by LiosK
- All Contributors
License
The MIT License (MIT). Please see License File for more information.