Wallogit.com
2017 © Pedro PelĆ”ez
Class for genereting id's fast with php
A class written to generate unique id's fast. It's based on Twiter's snowflake With a more straight forward approach, and probably is a little bit less efficient., (*1)
Example: 60664889587010008, (*2)
Format: An integer formed by 17 digits. [XXXXXXXX][YYY][WW][ZZZZ] where:, (*3)
CURRENT_UNIX_TIMESTAMP - 1506204000
These are a few of the cases where such a lib could be useful:, (*4)
There are a lot of alternatives to these problems, like for example using some other GUID/UUID scheme. But I wanted the same things that Twitter's snowflake wanted:, (*5)
created_on columns to my database, since my ids were time based I could use the first 8 digits of it to convert it back to a UNIX_TIMESTAMP and have the data created_on historical values. You can also use Twitter's or Flicker's solutions, they are great. Is my implementation superior by any mean? Probably not. I decided to write it mainly because: 1) I could. 2) I wanted some good use case to study how to work with sockets on PHP., (*6)
On my localhost env (8gb RAM, SSD, IntelĀ® Core⢠i7-7500U CPU @ 2.70GHz Ć 4) it generates ā 20 thousand ids per second per process via TCP, using the lib directly on the source code allows you to generate hundreds of thousands of ids per second though., (*7)
You can install it by only downloading the class file on the src folder, or you can install it with composer. Composer package name: webingpro/id-generator, (*8)
You can either use it directly on your source code with the following one liner:, (*9)
$id = IdGenerator\IdGenerator::getId();
If you have multiple codebases that need to generate unique ids between them, you should probably use this as a service., (*10)
The service will listen to a TCP port and then you need to communicate with that service via a socket. There is a client already written in PHP but it should be straightforward to write the clients in other languages as well., (*11)
1) To start the service run the following on your terminal:
php src/IdGenerator/Socket/Server.php port=4317 host=127.0.0.1 workerId=01, (*12)
2) If everything went ok you should see the following message:, (*13)
IdGenerator Listening to 127.0.0.1:4317
3) To test the service open a new terminal tab and enter:, (*14)
telnet 127.0.0.1 4317
4) Once you are connected to the service via telnet you can run the following commands, (*15)
GET_ID //To return new ids QUIT //To close the connection
, (*16)
If you want to use this in production as a service you should probably use something like supervisord to control the spawned processes and make sure that they are reinitialized if something goes wrong., (*17)
There is an example config file for supervisord at the root of this repository., (*18)
Once the serve is running with supervisord or something similar, if you are using PHP, you can use the provided Client class., (*19)
Example:, (*20)
<?php
use IdGenerator\Socket\Client;
require("vendor/autoload.php"); //Require either the composer autoloader or the Client class file
$client = new Client("127.0.0.1","4317"); //Make sure to point to the correct host and port
$id = $client->getId();
var_dump($id);
On the provided PHP Client if the client can't connect to the service it will throw an warning
and generate the ID locally using the IdGenerator Object.
When doing so the workerId for the local IdGenerator will be "00", unless you change it by defining the constant:
ID_GENERATOR_MACHINE_ID on your code to something else., (*21)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:, (*22)
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software., (*23)
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE., (*24)