{"id":376,"date":"2020-12-04T17:24:26","date_gmt":"2020-12-04T09:24:26","guid":{"rendered":"https:\/\/wp.dj47.top\/?p=376"},"modified":"2021-10-20T16:11:04","modified_gmt":"2021-10-20T08:11:04","slug":"php%e4%bd%bf%e7%94%a8snowflake%e7%ae%97%e6%b3%95%e7%94%9f%e6%88%90%e5%94%af%e4%b8%80id","status":"publish","type":"post","link":"https:\/\/www.djlog.cn\/?p=376","title":{"rendered":"PHP\u4f7f\u7528SnowFlake\u7b97\u6cd5\u751f\u6210\u552f\u4e00ID"},"content":{"rendered":"<pre class=\"language-php\"><code>class IdWork\r\n{\r\n \r\n    \/\/\u5f00\u59cb\u65f6\u95f4,\u56fa\u5b9a\u4e00\u4e2a\u5c0f\u4e8e\u5f53\u524d\u65f6\u95f4\u7684\u6beb\u79d2\u6570\u5373\u53ef\r\n    const twepoch =  1474992000000;\/\/2016\/9\/28 0:0:0\r\n \r\n    \/\/\u673a\u5668\u6807\u8bc6\u5360\u7684\u4f4d\u6570\r\n    const workerIdBits = 5;\r\n \r\n    \/\/\u6570\u636e\u4e2d\u5fc3\u6807\u8bc6\u5360\u7684\u4f4d\u6570\r\n    const datacenterIdBits = 5;\r\n \r\n    \/\/\u6beb\u79d2\u5185\u81ea\u589e\u6570\u70b9\u7684\u4f4d\u6570\r\n    const sequenceBits = 12;\r\n \r\n    protected $workId = 0;\r\n    protected $datacenterId = 0;\r\n \r\n    static $lastTimestamp = -1;\r\n    static $sequence = 0;\r\n \r\n \r\n    function __construct($workId=0, $datacenterId=0){\r\n        \/\/\u673a\u5668ID\u8303\u56f4\u5224\u65ad\r\n        $maxWorkerId = -1 ^ (-1 &lt;&lt; self::workerIdBits);\r\n        if($workId &gt; $maxWorkerId || $workId&lt; 0){\r\n            throw new Exception(\"workerId can't be greater than \".$this-&gt;maxWorkerId.\" or less than 0\");\r\n        }\r\n        \/\/\u6570\u636e\u4e2d\u5fc3ID\u8303\u56f4\u5224\u65ad\r\n        $maxDatacenterId = -1 ^ (-1 &lt;&lt; self::datacenterIdBits);\r\n        if ($datacenterId &gt; $maxDatacenterId || $datacenterId &lt; 0) {\r\n            throw new Exception(\"datacenter Id can't be greater than \".$maxDatacenterId.\" or less than 0\");\r\n        }\r\n        \/\/\u8d4b\u503c\r\n        $this-&gt;workId = $workId;\r\n        $this-&gt;datacenterId = $datacenterId;\r\n    }\r\n \r\n    \/\/\u751f\u6210\u4e00\u4e2aID\r\n    public function nextId(){\r\n        $timestamp = $this-&gt;timeGen();\r\n        $lastTimestamp = self::$lastTimestamp;\r\n        \/\/\u5224\u65ad\u65f6\u949f\u662f\u5426\u6b63\u5e38\r\n        if ($timestamp &lt; $lastTimestamp) {\r\n            throw new Exception(\"Clock moved backwards.  Refusing to generate id for %d milliseconds\", ($lastTimestamp - $timestamp));\r\n        }\r\n        \/\/\u751f\u6210\u552f\u4e00\u5e8f\u5217\r\n        if ($lastTimestamp == $timestamp) {\r\n            $sequenceMask = -1 ^ (-1 &lt;&lt; self::sequenceBits);\r\n            self::$sequence = (self::$sequence + 1) &amp; $sequenceMask;\r\n            if (self::$sequence == 0) {\r\n                $timestamp = $this-&gt;tilNextMillis($lastTimestamp);\r\n            }\r\n        } else {\r\n            self::$sequence = 0;\r\n        }\r\n        self::$lastTimestamp = $timestamp;\r\n        \/\/\r\n        \/\/\u65f6\u95f4\u6beb\u79d2\/\u6570\u636e\u4e2d\u5fc3ID\/\u673a\u5668ID,\u8981\u5de6\u79fb\u7684\u4f4d\u6570\r\n        $timestampLeftShift = self::sequenceBits + self::workerIdBits + self::datacenterIdBits;\r\n        $datacenterIdShift = self::sequenceBits + self::workerIdBits;\r\n        $workerIdShift = self::sequenceBits;\r\n        \/\/\u7ec4\u54084\u6bb5\u6570\u636e\u8fd4\u56de: \u65f6\u95f4\u6233.\u6570\u636e\u6807\u8bc6.\u5de5\u4f5c\u673a\u5668.\u5e8f\u5217\r\n        $nextId = (($timestamp - self::twepoch) &lt;&lt; $timestampLeftShift) |\r\n            ($this-&gt;datacenterId &lt;&lt; $datacenterIdShift) |\r\n            ($this-&gt;workId &lt;&lt; $workerIdShift) | self::$sequence;\r\n        return $nextId;\r\n    }\r\n \r\n    \/\/\u53d6\u5f53\u524d\u65f6\u95f4\u6beb\u79d2\r\n    protected function timeGen(){\r\n        $timestramp = (float)sprintf(\"%.0f\", microtime(true) * 1000);\r\n        return  $timestramp;\r\n    }\r\n \r\n    \/\/\u53d6\u4e0b\u4e00\u6beb\u79d2\r\n    protected function tilNextMillis($lastTimestamp) {\r\n        $timestamp = $this-&gt;timeGen();\r\n        while ($timestamp &lt;= $lastTimestamp) {\r\n            $timestamp = $this-&gt;timeGen();\r\n        }\r\n        return $timestamp;\r\n    }\r\n \r\n}\r\n \r\n\/\/\u8c03\u7528\r\n$obj = new IdWork();\r\nprint_r($obj-&gt;nextId());<\/code><\/pre>\n<p><span style=\"color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; background-color: #ffffff;\">\u8f6c\u81ea\uff1a<a href=\"http:\/\/r6d.cn\/MEXV\">http:\/\/r6d.cn\/MEXV<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>class IdWork { \/\/\u5f00\u59cb\u65f6\u95f4,\u56fa\u5b9a\u4e00\u4e2a\u5c0f\u4e8e\u5f53\u524d\u65f6\u95f4\u7684\u6beb\u79d2\u6570\u5373\u53ef const twepoch =  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"class_list":["post-376","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=376"}],"version-history":[{"count":0,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/376\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}