title: SnowFlake meta:

  • name: description content: The snowflake algorithm generates a unique number.
  • name: keywords content: swoole|swoole extension|swoole framework|Easyswoole|Component Library|Miscellaneous Tools|SnowFlake

Snowflake algorithm

Use

Generate a unique number

Core Object Class

To implement this component function you need to load the core class:

  1. EasySwoole\Utility\Random

Core Object Method

Make

Generate a random number based on the snowflake algorithm

  • mixed $dataCenterID data center
  • mixed $workerID task process
  1. Static function make($dataCenterID = 0, $workerID = 0)

Unmake

Reverse analysis of the number generated by the snowflake algorithm

  • mixed $snowFlakeId number
  1. static function unmake($snowFlakeId)

How to use

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-1-9
  6. * Time: 10:10
  7. */
  8. require './vendor/autoload.php';
  9. $str = \EasySwoole\Utility\SnowFlake::make(1,1);//Incoming data center id (0-31), task process id (0-31)
  10. var_dump($str);
  11. var_dump(\EasySwoole\Utility\SnowFlake::unmake($str));
  12. /**
  13. * Output results:
  14. * int(194470364728922112)
  15. * object(stdClass)#3 (4) {
  16. * ["timestamp"]=>
  17. * int(1532127766018)
  18. * ["dataCenterID"]=>
  19. * int(1)
  20. * ["workerID"]=>
  21. * int(1)
  22. * ["sequence"]=>
  23. * int(0)
  24. * }
  25. */