{"id":9157,"date":"2024-02-22T09:24:09","date_gmt":"2024-02-22T01:24:09","guid":{"rendered":"\/?p=9157"},"modified":"2024-02-22T09:24:09","modified_gmt":"2024-02-22T01:24:09","slug":"%e6%94%be%e5%bc%83%e4%bd%bf%e7%94%a8uuid%ef%bc%8culid%e6%89%8d%e6%98%af%e6%9b%b4%e5%a5%bd%e7%9a%84%e9%80%89%e6%8b%a9%ef%bc%81","status":"publish","type":"post","link":"\/?p=9157","title":{"rendered":"\u653e\u5f03\u4f7f\u7528UUID\uff0cULID\u624d\u662f\u66f4\u597d\u7684\u9009\u62e9\uff01"},"content":{"rendered":"<p>ULID \uff1aUniversally Unique Lexicographically Sortable Identifier\uff08\u901a\u7528\u552f\u4e00\u8bcd\u5178\u5206\u7c7b\u6807\u8bc6\u7b26\uff09<br \/>\nUUID \uff1aUniversally Unique Identifier\uff08\u901a\u7528\u552f\u4e00\u6807\u8bc6\u7b26\uff09<\/p>\n<h2>1. \u4e3a\u4ec0\u4e48\u4e0d\u9009\u62e9UUID<\/h2>\n<p>UUID \u76ee\u524d\u6709 5 \u4e2a\u7248\u672c\uff1a<\/p>\n<p>\u7248\u672c1\uff1a\u5728\u8bb8\u591a\u73af\u5883\u4e2d\u662f\u4e0d\u5207\u5b9e\u9645\u7684\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u8bbf\u95ee\u552f\u4e00\u7684\uff0c\u7a33\u5b9a\u7684MAC\u5730\u5740\uff0c\u5bb9\u6613\u88ab\u653b\u51fb\uff1b<br \/>\n\u7248\u672c2\uff1a\u5c06\u7248\u672c 1 \u7684\u65f6\u95f4\u6233\u524d\u56db\u4f4d\u6362\u4e3a POSIX \u7684 UID \u6216 GID\uff0c\u95ee\u9898\u540c\u4e0a\uff1b<br \/>\n\u7248\u672c3\uff1a\u57fa\u4e8e MD5 \u54c8\u5e0c\u7b97\u6cd5\u751f\u6210\uff0c\u751f\u6210\u968f\u673a\u5206\u5e03\u7684ID\u9700\u8981\u552f\u4e00\u7684\u79cd\u5b50\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u8bb8\u591a\u6570\u636e\u7ed3\u6784\u788e\u7247\u5316\uff1b<br \/>\n\u7248\u672c4\uff1a\u57fa\u4e8e\u968f\u673a\u6570\u6216\u4f2a\u968f\u673a\u6570\u751f\u6210\uff0c\u9664\u4e86\u968f\u673a\u6027\u5916\u6ca1\u6709\u63d0\u4f9b\u5176\u4ed6\u4fe1\u606f\uff1b<br \/>\n\u7248\u672c5\uff1a\u901a\u8fc7 SHA-1 \u54c8\u5e0c\u7b97\u6cd5\u751f\u6210\uff0c\u751f\u6210\u968f\u673a\u5206\u5e03\u7684ID\u9700\u8981\u552f\u4e00\u7684\u79cd\u5b50\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u8bb8\u591a\u6570\u636e\u7ed3\u6784\u788e\u7247\u5316\uff1b<\/p>\n<p>\u8fd9\u91cc\u9762\u5e38\u7528\u7684\u5c31\u662f UUID4 \u4e86\uff0c\u4f46\u662f\uff0c\u5373\u4f7f\u662f\u968f\u673a\u7684\uff0c\u4f46\u662f\u4e5f\u662f\u5b58\u5728\u51b2\u7a81\u7684\u98ce\u9669\u3002<br \/>\n\u548c UUID \u8981\u4e48\u57fa\u4e8e\u968f\u673a\u6570\uff0c\u8981\u4e48\u57fa\u4e8e\u65f6\u95f4\u6233\u4e0d\u540c\uff0cULID \u662f\u65e2\u57fa\u4e8e\u65f6\u95f4\u6233\u53c8\u57fa\u4e8e\u968f\u673a\u6570\uff0c\u65f6\u95f4\u6233\u7cbe\u786e\u5230\u6beb\u79d2\uff0c\u6beb\u79d2\u5185\u67091.21e + 24\u4e2a\u968f\u673a\u6570\uff0c\u4e0d\u5b58\u5728\u51b2\u7a81\u7684\u98ce\u9669\uff0c\u800c\u4e14\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u6bd4 UUID \u66f4\u52a0\u53cb\u597d\u3002<\/p>\n<h2>2. ULID\u7279\u6027<\/h2>\n<pre><code class=\"language-python\">ulid() # 01ARZ3NDEKTSV4RRFFQ69G5FAV<\/code><\/pre>\n<ul>\n<li>\u4e0eUUID\u7684128\u4f4d\u517c\u5bb9\u6027<\/li>\n<li>\u6bcf\u6beb\u79d21.21e + 24\u4e2a\u552f\u4e00ULID<\/li>\n<li>\u6309\u5b57\u5178\u987a\u5e8f(\u4e5f\u5c31\u662f\u5b57\u6bcd\u987a\u5e8f)\u6392\u5e8f\uff01<\/li>\n<li>\u89c4\u8303\u5730\u7f16\u7801\u4e3a26\u4e2a\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u662fUUID\u768436\u4e2a\u5b57\u7b26<\/li>\n<li>\u4f7f\u7528Crockford\u7684base32\u83b7\u5f97\u66f4\u597d\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\uff08\u6bcf\u4e2a\u5b57\u7b265\u4f4d\uff09<\/li>\n<li>\u4e0d\u533a\u5206\u5927\u5c0f\u5199<\/li>\n<li>\u6ca1\u6709\u7279\u6b8a\u5b57\u7b26\uff08URL\u5b89\u5168\uff09<\/li>\n<li>\u5355\u8c03\u6392\u5e8f\u987a\u5e8f\uff08\u6b63\u786e\u68c0\u6d4b\u5e76\u5904\u7406\u76f8\u540c\u7684\u6beb\u79d2\uff09<\/li>\n<\/ul>\n<h2>3. ULID\u89c4\u8303<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5728python(ulid-py)\u4e2d\u5b9e\u73b0\u7684ULID\u7684\u5f53\u524d\u89c4\u8303\u3002\u4e8c\u8fdb\u5236\u683c\u5f0f\u5df2\u5b9e\u73b0<\/p>\n<pre><code>01AN4Z07BY      79KA1307SR9X4MV3\n\n|----------|    |----------------|\n Timestamp          Randomness\n  10chars            16chars\n   48bits             80bits<\/code><\/pre>\n<h3>3.1 \u7ec4\u6210<\/h3>\n<p><strong>\u65f6\u95f4\u6233<\/strong><\/p>\n<ul>\n<li>48\u4f4d\u6574\u6570<\/li>\n<li>UNIX\u65f6\u95f4\uff08\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\uff09<\/li>\n<li>\u76f4\u5230\u516c\u514310889\u5e74\uff0c\u7a7a\u95f4\u90fd\u4e0d\u4f1a\u8017\u5c3d\u3002<\/li>\n<\/ul>\n<p><strong>\u968f\u673a\u6027<\/strong><\/p>\n<ul>\n<li>80\u4f4d\u968f\u673a\u6570<\/li>\n<li>\u5982\u679c\u53ef\u80fd\u7684\u8bdd\uff0c\u91c7\u7528\u52a0\u5bc6\u6280\u672f\u4fdd\u8bc1\u968f\u673a\u6027<\/li>\n<\/ul>\n<p><strong>\u6392\u5e8f<\/strong><\/p>\n<p>\u6700\u5de6\u8fb9\u7684\u5b57\u7b26\u5fc5\u987b\u6392\u5728\u6700\u524d\u9762\uff0c\u6700\u53f3\u8fb9\u7684\u5b57\u7b26\u5fc5\u987b\u6392\u5728\u6700\u540e\uff08\u8bcd\u6c47\u987a\u5e8f\uff09\u3002\u5fc5\u987b\u4f7f\u7528\u9ed8\u8ba4\u7684ASCII\u5b57\u7b26\u96c6\u3002\u5728\u540c\u4e00\u6beb\u79d2\u5185\uff0c\u4e0d\u80fd\u4fdd\u8bc1\u6392\u5e8f\u987a\u5e8f<\/p>\n<p><strong>\u7f16\u7801\u65b9\u5f0f<\/strong><\/p>\n<p>\u5982\u56fe\u6240\u793a\uff0c\u4f7f\u7528\u4e86Crockford\u7684Base32\u3002\u8be5\u5b57\u6bcd\u8868\u4e0d\u5305\u62ec\u5b57\u6bcdI\uff0cL\uff0cO\u548cU\uff0c\u4ee5\u907f\u514d\u6df7\u6dc6\u548c\u6ee5\u7528\u3002<\/p>\n<pre><code>0123456789ABCDEFGHJKMNPQRSTVWXYZ<\/code><\/pre>\n<p><strong>\u4e8c\u8fdb\u5236\u5e03\u5c40\u548c\u5b57\u8282\u987a\u5e8f<\/strong><\/p>\n<p>\u7ec4\u4ef6\u88ab\u7f16\u7801\u4e3a16\u4e2a\u516b\u4f4d\u4f4d\u7ec4\u3002\u6bcf\u4e2a\u7ec4\u4ef6\u90fd\u4ee5\u6700\u9ad8\u6709\u6548\u5b57\u8282\u5728\u524d\uff08\u7f51\u7edc\u5b57\u8282\u987a\u5e8f\uff09\u8fdb\u884c\u7f16\u7801\u3002<\/p>\n<pre><code>0                   1                   2                   3\n 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n|                      32_bit_uint_time_high                    |\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n|     16_bit_uint_time_low      |       16_bit_uint_random      |\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n|                       32_bit_uint_random                      |\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n|                       32_bit_uint_random                      |\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<\/code><\/pre>\n<h3>3.2 \u5e94\u7528\u573a\u666f<\/h3>\n<ul>\n<li>\u66ff\u6362\u6570\u636e\u5e93\u81ea\u589eid\uff0c\u65e0\u9700DB\u53c2\u4e0e\u4e3b\u952e\u751f\u6210<\/li>\n<li>\u5206\u5e03\u5f0f\u73af\u5883\u4e0b\uff0c\u66ff\u6362UUID\uff0c\u5168\u5c40\u552f\u4e00\u4e14\u6beb\u79d2\u7cbe\u5ea6\u6709\u5e8f<\/li>\n<li>\u6bd4\u5982\u8981\u6309\u65e5\u671f\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u5206\u533a\u5206\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528ULID\u4e2d\u5d4c\u5165\u7684\u65f6\u95f4\u6233\u6765\u9009\u62e9\u6b63\u786e\u7684\u5206\u533a\u5206\u8868<\/li>\n<li>\u5982\u679c\u6beb\u79d2\u7cbe\u5ea6\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\uff08\u6beb\u79d2\u5185\u65e0\u5e8f\uff09\uff0c\u53ef\u4ee5\u6309\u7167ULID\u6392\u5e8f\uff0c\u800c\u4e0d\u662f\u5355\u72ec\u7684created_at\u5b57\u6bb5<\/li>\n<\/ul>\n<h2>4. \u7528\u6cd5\uff08python\uff09<\/h2>\n<p>\u5b89\u88c5<\/p>\n<pre><code>pip install ulid-py<\/code><\/pre>\n<p>\u521b\u5efa\u4e00\u4e2a\u5168\u65b0\u7684ULID\u3002<\/p>\n<p>\u65f6\u95f4\u6233\u8bb0\u503c\uff0848\u4f4d\uff09\u6765\u81ea <code>time.time()<\/code>\uff0c\u7cbe\u5ea6\u4e3a\u6beb\u79d2\u3002<\/p>\n<p>\u968f\u673a\u503c\uff0880\u4f4d\uff09\u6765\u81ea <code>os.urandom()<\/code>\u3002<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; import ulid\n>&gt;&gt; ulid.new()\n&lt;ULID(&#039;01BJQE4QTHMFP0S5J153XCFSP9&#039;)&gt;<\/code><\/pre>\n<p>\u6839\u636e\u73b0\u6709\u7684128\u4f4d\u503c\uff08\u4f8b\u5982UUID\uff09\u521b\u5efa\u65b0\u7684ULID \u3002<br \/>\n\u652f\u6301ULID\u503c\u7c7b\u578b\u6709 int\uff0cbytes\uff0cstr\uff0c\u548cUUID\u3002<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; import ulid, uuid\n>&gt;&gt; value = uuid.uuid4()\n>&gt;&gt; value\nUUID(&#039;0983d0a2-ff15-4d83-8f37-7dd945b5aa39&#039;)\n>&gt;&gt; ulid.from_uuid(value)\n&lt;ULID(&#039;09GF8A5ZRN9P1RYDVXV52VBAHS&#039;)&gt;<\/code><\/pre>\n<p>\u4ece\u73b0\u6709\u65f6\u95f4\u6233\u503c\uff08\u4f8b\u5982<code>datetime<\/code>\u5bf9\u8c61\uff09\u521b\u5efa\u65b0\u7684ULID \u3002<br \/>\n\u652f\u6301\u65f6\u95f4\u6233\u503c\u7c7b\u578b\u6709int\uff0cfloat\uff0cstr\uff0cbytes\uff0cbytearray\uff0cmemoryview\uff0cdatetime\uff0cTimestamp\uff0c\u548cULID<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; import datetime, ulid\n>&gt;&gt; ulid.from_timestamp(datetime.datetime(1999, 1, 1))\n&lt;ULID(&#039;00TM9HX0008S220A3PWSFVNFEH&#039;)&gt;<\/code><\/pre>\n<p>\u6839\u636e\u73b0\u6709\u7684\u968f\u673a\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684ULID\u3002<\/p>\n<p>\u652f\u6301\u968f\u673a\u503c\u7c7b\u578b\u6709int\uff0cfloat\uff0cstr\uff0cbytes\uff0cbytearray\uff0cmemoryview\uff0cRandomness\uff0c\u548cULID\u3002<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; import os, ulid\n>&gt;&gt; randomness = os.urandom(10)\n>&gt;&gt; ulid.from_randomness(randomness)\n>&gt;&gt; &lt;ULID(&#039;01BJQHX2XEDK0VN0GMYWT9JN8S&#039;)&gt;<\/code><\/pre>\n<p>\u4e00\u65e6\u6709\u4e86ULID\u5bf9\u8c61\uff0c\u5c31\u6709\u591a\u79cd\u4e0e\u4e4b\u4ea4\u4e92\u7684\u65b9\u6cd5\u3002<\/p>\n<p><code>timestamp()<\/code>\u65b9\u6cd5\u5c06\u4e3a\u60a8\u63d0\u4f9bULID\u7684\u524d48\u4f4d\u7684\u65f6\u95f4\u6233\u5feb\u7167\uff0c\u800c<code>randomness()<\/code>\u65b9\u6cd5\u5c06\u4e3a\u60a8\u63d0\u4f9b\u540e80\u4f4d\u7684\u968f\u673a\u6570\u5feb\u7167\u3002<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; import ulid\n>&gt;&gt; u = ulid.new()\n>&gt;&gt; u\n&lt;ULID(&#039;01BJQM7SC7D5VVTG3J68ABFQ3N&#039;)&gt;\n>&gt;&gt; u.timestamp()\n&lt;Timestamp(&#039;01BJQM7SC7&#039;)&gt;\n>&gt;&gt; u.randomness()\n&lt;Randomness(&#039;D5VVTG3J68ABFQ3N&#039;)&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ULID \uff1aUniversally Unique Lexicographically Sortable Identifier\uff08\u901a\u7528\u552f\u4e00\u8bcd   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[21],"tags":[238,237],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9157"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9157"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9157\/revisions"}],"predecessor-version":[{"id":9158,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9157\/revisions\/9158"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9157"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}