{"id":1365,"date":"2025-09-10T14:23:21","date_gmt":"2025-09-10T06:23:21","guid":{"rendered":"http:\/\/www.cmd137blog.top\/?p=1365"},"modified":"2025-09-10T14:23:25","modified_gmt":"2025-09-10T06:23:25","slug":"redis-%e5%ae%9e%e7%8e%b0%e9%ab%98%e5%b9%b6%e5%8f%91%e5%85%a8%e5%b1%80%e5%94%af%e4%b8%80-id-%e7%94%9f%e6%88%90%e5%99%a8%ef%bc%88%e9%99%84%e5%ae%8c%e6%95%b4%e4%bb%a3%e7%a0%81%ef%bc%89","status":"publish","type":"post","link":"http:\/\/www.cmd137blog.top\/?p=1365","title":{"rendered":"Redis \u5b9e\u73b0\u9ad8\u5e76\u53d1\u5168\u5c40\u552f\u4e00 ID \u751f\u6210\u5668\uff08\u9644\u5b8c\u6574\u4ee3\u7801\uff09"},"content":{"rendered":"\n<p>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u751f\u6210<strong>\u5168\u5c40\u552f\u4e00 ID<\/strong>\uff1a\u8ba2\u5355\u53f7\u3001\u7528\u6237\u53f7\u3001\u65e5\u5fd7\u8ffd\u8e2a ID\u2026\u2026\u5982\u679c\u8fd8\u5728\u7528\u6570\u636e\u5e93\u81ea\u589e ID \u6216 UUID\uff0c\u8981\u4e48\u6027\u80fd\u4e0d\u591f\uff0c\u8981\u4e48\u4e0d\u6613\u6392\u5e8f\u3001\u6d6a\u8d39\u5b58\u50a8\u7a7a\u95f4\u3002<br>\u4e0b\u9762\u662f\u4e00\u79cd<strong>Redis \u81ea\u589e + \u65f6\u95f4\u6233<\/strong>\u7684\u9ad8\u6027\u80fd ID \u751f\u6210\u5668\u5b9e\u73b0\u65b9\u5f0f\uff0c\u8f7b\u91cf\u3001\u7b80\u5355\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u4e3a\u4ec0\u4e48\u9700\u8981\u5168\u5c40\u552f\u4e00 ID \u751f\u6210\u5668\uff1f<\/h2>\n\n\n\n<p>\u5728\u5355\u4f53\u5e94\u7528\u65f6\u4ee3\uff0c\u6211\u4eec\u5e38\u7528\u6570\u636e\u5e93\u7684\u81ea\u589e ID\u3002<br>\u4f46\u5728\u5206\u5e03\u5f0f\u67b6\u6784\u4e0b\uff0c\u591a\u4e2a\u8282\u70b9\u540c\u65f6\u5199\u6570\u636e\u5e93\uff0c\u6216\u8005\u6709\u591a\u4e2a\u6570\u636e\u5e93\u5206\u7247\u65f6\uff0c\u5c31\u5bb9\u6613\u51fa\u73b0 ID \u51b2\u7a81\u6216\u96be\u4ee5\u7ef4\u62a4\u7684\u4e3b\u952e\u7b56\u7565\u3002<\/p>\n\n\n\n<p>\u5e38\u89c1\u65b9\u6848\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>UUID<\/strong>\uff1a\u5929\u7136\u552f\u4e00\uff0c\u4f46\u8fc7\u957f\uff0832\u4f4d\u5341\u516d\u8fdb\u5236\uff09\uff0c\u4e0d\u53ef\u6392\u5e8f\uff0c\u5b58\u50a8\u7d22\u5f15\u6548\u7387\u4f4e\u3002<\/li>\n\n\n\n<li><strong>Snowflake \u7b97\u6cd5<\/strong>\uff1a\u7ecf\u5178\u7684 Twitter \u96ea\u82b1\u7b97\u6cd5\uff0c\u9700\u8981\u7ef4\u62a4\u673a\u5668\u53f7\u3001\u6570\u636e\u4e2d\u5fc3\u53f7\uff0c\u5e94\u7528\u5c42\u5b9e\u73b0\u8f83\u590d\u6742\u3002<\/li>\n\n\n\n<li><strong>Redis \u81ea\u589e<\/strong>\uff1a\u5929\u7136\u5206\u5e03\u5f0f\uff0c\u6027\u80fd\u9ad8\uff0c\u53ef\u6309\u4e1a\u52a1\/\u65f6\u95f4\u7ef4\u5ea6\u751f\u6210\u5e8f\u5217\u53f7\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u5c31\u662f\u57fa\u4e8e Redis \u81ea\u589e\u7684\u65b9\u6848\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u8bbe\u8ba1\u601d\u8def<\/h2>\n\n\n\n<p>\u6211\u4eec\u5e0c\u671b\u4e00\u4e2a ID \u65e2\u80fd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4fdd\u8bc1\u552f\u4e00<\/strong>\uff1a\u4e0d\u540c\u8bf7\u6c42\u7684 ID \u4e0d\u91cd\u590d\uff1b<\/li>\n\n\n\n<li><strong>\u8d8b\u52bf\u9012\u589e<\/strong>\uff1a\u4fbf\u4e8e\u6309\u65f6\u95f4\u6392\u5e8f\uff1b<\/li>\n\n\n\n<li><strong>\u9ad8\u6027\u80fd<\/strong>\uff1a\u5728\u9ad8\u5e76\u53d1\u4e0b\u4f9d\u65e7\u7a33\u5b9a\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u628a ID \u5206\u4e24\u90e8\u5206\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u9ad8 32 \u4f4d\uff1a\u65f6\u95f4\u6233\uff08\u79d2\u7ea7\uff09\uff0c\u8868\u793a\u4ece\u67d0\u4e2a\u56fa\u5b9a\u65f6\u95f4\u70b9\u5f00\u59cb\u7ecf\u8fc7\u7684\u79d2\u6570\n\u4f4e 32 \u4f4d\uff1a\u5e8f\u5217\u53f7\uff0c\u8868\u793a\u5f53\u5929\u7b2c\u51e0\u4e2a ID\n<\/code><\/pre>\n\n\n\n<p>\u62fc\u63a5\u516c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return (\u65f6\u95f4\u6233 &lt;&lt; 32) | \u5e8f\u5217\u53f7\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u5c31\u5f97\u5230\u4e00\u4e2a 64 \u4f4d long \u7c7b\u578b\u7684 ID\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u5b8c\u6574\u4ee3\u7801\u5b9e\u73b0<\/h2>\n\n\n\n<p>\u76f4\u63a5\u4e0a\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * \u5168\u5c40\u552f\u4e00ID\u751f\u6210\u5668\n *\/\n@Component\npublic class RedisIdWorker {\n    \/\/ \u8d77\u59cb\u65f6\u95f4\u6233\uff082022-01-01 00:00:00 UTC\uff09\n    private static final long BEGIN_TIMESTAMP = 1640995200L;\n    \/\/ \u5e8f\u5217\u53f7\u5360\u7528\u4f4d\u6570\n    private static final int COUNT_BITS = 32;\n\n    private final StringRedisTemplate stringRedisTemplate;\n\n    public RedisIdWorker(StringRedisTemplate stringRedisTemplate) {\n        this.stringRedisTemplate = stringRedisTemplate;\n    }\n\n    \/**\n     * \u751f\u6210\u5168\u5c40\u552f\u4e00ID\n     * @param keyPrefix \u4e1a\u52a1\u524d\u7f00\uff08\u5982 \"order\"\u3001\"user\"\uff09\n     *\/\n    public long nextId(String keyPrefix) {\n        \/\/ 1. \u751f\u6210\u65f6\u95f4\u6233\uff08\u79d2\uff09\n        long timeStamp = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) - BEGIN_TIMESTAMP;\n\n        \/\/ 2. \u751f\u6210\u5e8f\u5217\u53f7\uff1a\u6bcf\u5929\u4e00\u4e2akey\uff0c\u907f\u514d\u6570\u5b57\u65e0\u9650\u589e\u957f\n        String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern(\"yyyy:MM:dd\"));\n        long count = stringRedisTemplate.opsForValue()\n                .increment(\"icr:\" + keyPrefix + \":\" + date);\n\n        \/\/ 3. \u62fc\u63a5\u65f6\u95f4\u6233\u548c\u5e8f\u5217\u53f7\n        return timeStamp &lt;&lt; COUNT_BITS | count;\n    }\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u5173\u952e\u70b9\u89e3\u6790<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 <code>BEGIN_TIMESTAMP<\/code><\/h3>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u57fa\u51c6\u65f6\u95f4\u6233\uff08UTC \u79d2\uff09\u3002\u6211\u4eec\u53ea\u5b58\u50a8\u4ece\u8fd9\u4e2a\u65f6\u95f4\u70b9\u5f00\u59cb\u7684\u79d2\u6570\uff0c\u53ef\u4ee5\u5c11\u5360\u7528\u4f4d\u6570\u3002<br>\u6bd4\u5982\u8fd9\u91cc\u8bbe\u5b9a\u7684\u662f <strong>2022-01-01 00:00:00 UTC<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 <code>COUNT_BITS<\/code><\/h3>\n\n\n\n<p>\u5e8f\u5217\u53f7\u90e8\u5206\u5360\u7528 32 \u4f4d\uff0c\u8868\u793a\u4e00\u5929\u6700\u591a\u53ef\u4ee5\u751f\u6210 <code>2^32<\/code> \u2248 42\u4ebf\u4e2a ID\u3002<br>\u53ef\u4ee5\u6309\u9700\u8c03\u6574\uff0c\u5982\u679c\u4e1a\u52a1\u91cf\u5c0f\uff0c\u4e5f\u53ef\u4ee5\u8c03\u5c0f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.3 \u201c\u5e8f\u5217\u53f7\u56de\u6eda\u4ee5\u5929\u4e3a\u5355\u4f4d\u201d\u662f\u4ec0\u4e48\u610f\u601d\uff1f<\/h3>\n\n\n\n<p>\u8fd9\u91cc\u6bcf\u5929\u751f\u6210\u7684 Redis key \u5e26\u6709\u65e5\u671f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>icr:order:2023:09:10\n<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e8c\u5929\u81ea\u52a8\u7528\u65b0\u7684 key\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>icr:order:2023:09:11\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u6bcf\u5929\u7684\u5e8f\u5217\u53f7\u90fd\u4ece 1 \u5f00\u59cb\u8ba1\u6570\uff08\u201c\u56de\u6eda\u201d\uff09\uff0c\u907f\u514d\u5e8f\u5217\u53f7\u65e0\u9650\u589e\u5927\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.4 \u4f7f\u7528 Redis \u7684\u597d\u5904<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Redis \u5355\u7ebf\u7a0b\u81ea\u589e\u64cd\u4f5c <code>INCR<\/code> \u662f\u539f\u5b50\u6027\u7684\uff1b<\/li>\n\n\n\n<li>\u751f\u6210\u901f\u5ea6\u5feb\uff08\u5fae\u79d2\u7ea7\uff09\uff0c\u652f\u6491\u9ad8\u5e76\u53d1\uff1b<\/li>\n\n\n\n<li>\u5206\u5e03\u5f0f\u591a\u8282\u70b9\u5171\u7528\u4e00\u4e2a Redis\uff0c\u5929\u7136\u5168\u5c40\u552f\u4e00\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u4f7f\u7528\u793a\u4f8b<\/h2>\n\n\n\n<p>\u5728\u4e1a\u52a1\u4ee3\u7801\u4e2d\u76f4\u63a5\u6ce8\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Autowired\nprivate RedisIdWorker redisIdWorker;\n\npublic void createOrder() {\n    long orderId = redisIdWorker.nextId(\"order\");\n    \/\/ \u4fdd\u5b58\u8ba2\u5355\u65f6\u4f7f\u7528 orderId\n}\n<\/code><\/pre>\n\n\n\n<p>Redis \u4e2d\u81ea\u52a8\u751f\u6210\u7c7b\u4f3c\u8fd9\u6837\u7684 key\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>icr:order:2023:09:10 = 10001\n<\/code><\/pre>\n\n\n\n<p>\u6700\u7ec8\u8fd4\u56de\u7684 long \u578b ID \u5728\u6570\u636e\u5e93\u4e2d\u53ef\u5b58\u4e3a bigint\uff0c\u65e2\u77ed\u53c8\u53ef\u6392\u5e8f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. \u5bf9\u6bd4\u5176\u5b83\u65b9\u6848<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6848<\/th><th>\u552f\u4e00\u6027<\/th><th>\u53ef\u6392\u5e8f<\/th><th>\u5b58\u50a8\u6548\u7387<\/th><th>\u5b9e\u73b0\u590d\u6742\u5ea6<\/th><\/tr><\/thead><tbody><tr><td>\u6570\u636e\u5e93\u81ea\u589e<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u7b80\u5355\uff08\u4f46\u4e0d\u9002\u5408\u5206\u5e03\u5f0f\uff09<\/td><\/tr><tr><td>UUID<\/td><td>\u2705<\/td><td>\u274c<\/td><td>\u274c<\/td><td>\u7b80\u5355<\/td><\/tr><tr><td>Snowflake<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u4e2d\u7b49\uff08\u9700\u7ef4\u62a4\u673a\u5668\u53f7\uff09<\/td><\/tr><tr><td>Redis\u81ea\u589e+\u65f6\u95f4\u6233<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u7b80\u5355\u3001\u8f7b\u91cf<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u4e2a\u65b9\u6848\u975e\u5e38\u9002\u5408\u4e2d\u5c0f\u578b\u5206\u5e03\u5f0f\u7cfb\u7edf\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. \u53ef\u80fd\u7684\u4f18\u5316<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u591a Redis \u8282\u70b9\u5bb9\u707e<\/strong>\uff1a\u7528 Redis \u96c6\u7fa4\u6216\u8005\u54e8\u5175\u6a21\u5f0f\u4fdd\u8bc1\u9ad8\u53ef\u7528\u3002<\/li>\n\n\n\n<li><strong>\u6279\u91cf\u9884\u53d6<\/strong>\uff1a\u5982\u679c\u8981\u51cf\u5c11 Redis \u8c03\u7528\uff0c\u53ef\u4ee5\u6279\u91cf\u62ff\u4e00\u6bb5\u5e8f\u5217\u53f7\u7f13\u5b58\u5728\u672c\u5730\u3002<\/li>\n\n\n\n<li><strong>\u6beb\u79d2\u7ea7\u65f6\u95f4\u6233<\/strong>\uff1a\u5982\u679c\u4e1a\u52a1\u9700\u8981\u66f4\u9ad8\u7cbe\u5ea6\uff0c\u53ef\u4ee5\u6539\u6210\u6beb\u79d2\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. \u603b\u7ed3<\/h2>\n\n\n\n<p>\u672c\u6587\u57fa\u4e8e SpringBoot + Redis\uff0c\u7ed3\u5408\u65f6\u95f4\u6233 + \u81ea\u589e\u5e8f\u5217\u53f7\uff0c\u5b9e\u73b0\u4e86\u4e00\u4e2a\u9ad8\u5e76\u53d1\u5168\u5c40\u552f\u4e00 ID \u751f\u6210\u5668\uff0c\u65e2\u4fdd\u8bc1\u552f\u4e00\u6027\u3001\u8d8b\u52bf\u9012\u589e\uff0c\u53c8\u8f7b\u91cf\u597d\u7528\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u628a\u8fd9\u4e2a\u7c7b\u76f4\u63a5\u4e22\u8fdb\u9879\u76ee\u91cc\uff0c\u51e0\u4e4e\u96f6\u6539\u52a8\u5c31\u80fd\u7528\uff0c\u975e\u5e38\u9002\u5408\u8ba2\u5355\u53f7\u3001\u6d41\u6c34\u53f7\u3001\u65e5\u5fd7\u8ffd\u8e2a ID \u7b49\u573a\u666f\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u751f\u6210\u5168\u5c40\u552f\u4e00 ID\uff1a\u8ba2\u5355\u53f7\u3001\u7528\u6237\u53f7\u3001\u65e5\u5fd7\u8ffd\u8e2a ID\u2026\u2026\u5982\u679c\u8fd8\u5728\u7528\u6570\u636e\u5e93\u81ea\u589e ID  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37,66],"tags":[],"class_list":["post-1365","post","type-post","status-publish","format-standard","hentry","category-javaweblist","category-66"],"_links":{"self":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/1365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1365"}],"version-history":[{"count":1,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/1365\/revisions"}],"predecessor-version":[{"id":1366,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/1365\/revisions\/1366"}],"wp:attachment":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1365"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}