Home > php教程 > PHP开发 > body text

SnowFlake algorithm for generating unique ID in PHP

高洛峰
Release: 2016-12-28 15:33:50
Original
1369 people have browsed it

Foreword: I recently needed to build a CMS system. Since the functions are relatively single and the requirements are flexible, I gave up a mature system like WP and built a relatively simple one myself. The article details page URL wants to be made into a pseudo-static URL format, that is, xxx.html. xxx has considered using an auto-incremented primary key directly, but I feel that this would expose the number of articles. Some students said that the initial value can be set higher, but it is still possible. The number of articles within a period of time is calculated based on the ID difference, so an algorithm that can generate unique IDs is needed.

The methods considered are

Use the timestamp directly, or a series of methods derived from it

The uuid that comes with Mysql

Both of the above methods can be found, so I won’t explain too much.

Finally I chose Twitter’s SnowFlake algorithm

The advantage of this algorithm is very simple. It can generate about 400W different 16 messages per second. Digital ID (decimal)

The principle is very simple

ID consists of 64 bits

The first bit is vacant

41bit is used to store milliseconds Level timestamp

10bit is used to store the machine ID

12bit is used to store the auto-incremented ID

Except for the highest bit marked as unavailable, the remaining three groups of bits are occupied All can be floated, depending on specific business needs. By default, the 41-bit timestamp can support the use of this algorithm until 2082, the 10-bit working machine ID can support 1023 machines, and the serial number supports 1 millisecond to generate 4095 auto-incrementing sequence IDs.
The following is the PHP source code

<?php
namespace App\Services;
 
abstract class Particle {
  const EPOCH = 1479533469598;
  const max12bit = 4095;
  const max41bit = 1099511627775;
 
  static $machineId = null;
 
  public static function machineId($mId = 0) {
    self::$machineId = $mId;
  }
 
  public static function generateParticle() {
    /*
    * Time - 42 bits
    */
    $time = floor(microtime(true) * 1000);
 
    /*
    * Substract custom epoch from current time
    */
    $time -= self::EPOCH;
 
    /*
    * Create a base and add time to it
    */
    $base = decbin(self::max41bit + $time);
 
 
    /*
    * Configured machine id - 10 bits - up to 1024 machines
    */
    if(!self::$machineId) {
      $machineid = self::$machineId;
    } else {
      $machineid = str_pad(decbin(self::$machineId), 10, "0", STR_PAD_LEFT);
    }
     
    /*
    * sequence number - 12 bits - up to 4096 random numbers per machine
    */
    $random = str_pad(decbin(mt_rand(0, self::max12bit)), 12, "0", STR_PAD_LEFT);
 
    /*
    * Pack
    */
    $base = $base.$machineid.$random;
 
    /*
    * Return unique time id no
    */
    return bindec($base);
  }
 
  public static function timeFromParticle($particle) {
    /*
    * Return time
    */
    return bindec(substr(decbin($particle),0,41)) - self::max41bit + self::EPOCH;
  }
}
 
?>
Copy after login

The calling method is as follows

Particle::generateParticle($machineId);//生成ID
Particle::timeFromParticle($particle);//反向计算时间戳
Copy after login

Here I have made an improvement. If the machine ID is passed 0, the 10 bits will be removed because sometimes we may not use so many IDs.

For more articles related to the SnowFlake algorithm for generating unique IDs in PHP, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template