{"id":201243,"date":"2025-05-10T09:32:28","date_gmt":"2025-05-10T01:32:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201243\/"},"modified":"2025-05-10T09:32:28","modified_gmt":"2025-05-10T01:32:28","slug":"redis%e7%94%9f%e6%88%90%e5%85%a8%e5%b1%80%e5%94%af%e4%b8%80id%e7%9a%84%e5%ae%9e%e7%8e%b0%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201243\/","title":{"rendered":"Redis\u751f\u6210\u5168\u5c40\u552f\u4e00ID\u7684\u5b9e\u73b0\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u751f\u6210\u5168\u5c40\u552f\u4e00ID\u7684\u5b9e\u73b0\u65b9\u6cd5<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u600e\u4e48\u5165\u95e8\u6570\u636e\u5e93\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762golang\u5b66\u4e60\u7f51\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aRedis\u751f\u6210\u5168\u5c40\u552f\u4e00ID\u7684\u5b9e\u73b0\u65b9\u6cd5\u300b\uff0c\u6d89\u53ca\u5230Redis\u5168\u5c40\u552f\u4e00ID\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<h2>\u7b80\u4ecb:<\/h2>\n<p>\u5168\u5c40\u552f\u4e00ID\u751f\u6210\u5668\u662f\u4e00\u79cd\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e0b\u7528\u6765\u751f\u6210\u5168\u5c40\u552f\u4e00ID\u7684\u5de5\u5177<\/p>\n<h2>\u7279\u6027\uff1a<\/h2>\n<ul>\n<li>\u552f\u4e00\u6027<\/li>\n<li>\u9ad8\u6027\u80fd<\/li>\n<li>\u5b89\u5168\u6027<\/li>\n<li>\u9ad8\u53ef\u7528<\/li>\n<li>\u9012\u589e\u6027<\/li>\n<\/ul>\n<h2>\u751f\u6210\u89c4\u5219\uff1a<\/h2>\n<p>\u6709\u65f6\u4e3a\u4e86\u589e\u52a0ID\u7684\u5b89\u5168\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0d\u76f4\u63a5\u4f7f\u7528Redis\u81ea\u589e\u7684\u6570\u503c\uff0c\u800c\u662f\u62fc\u63a5\u4e00\u4e9b\u5176\u4ed6\u4fe1\u606f<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167249342063b0396ce0096.png\" class=\"aligncenter\"><\/p>\n<p>ID\u7ec4\u6210\u90e8\u5206\uff1a<\/p>\n<ul>\n<li>\u7b26\u53f7\u4f4d\uff1a1bit\uff0c\u6c38\u8fdc\u4e3a0<\/li>\n<li>\u65f6\u95f4\u6233\uff1a31bit\uff0c\u4ee5\u79d2\u4e3a\u5355\u4f4d\uff0c\u53ef\u4ee5\u4f7f\u752869\u5e74<\/li>\n<li>\u5e8f\u5217\u53f7\uff1a32bit\uff0c\u79d2\u5185\u7684\u8ba1\u6570\u5668\uff0c\u652f\u6301\u6bcf\u79d2\u4ea7\u751f2^32\u4e2a\u4e0d\u540cID<\/li>\n<\/ul>\n<h2>&nbsp;ID\u751f\u6210\u7c7b\uff1a<\/h2>\n<pre>package com.example.util;\n\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.data.redis.core.StringRedisTemplate;\nimport org.springframework.stereotype.Component;\n\nimport java.time.LocalDateTime;\nimport java.time.ZoneOffset;\nimport java.time.format.DateTimeFormatter;\n\n\/**\n&nbsp;* @Author DaBai\n&nbsp;* @Date 2022\/3\/17 \u4e0b\u5348 09:25\n&nbsp;* @Version 1.0\n&nbsp;*\/\n\n@Component\npublic class RedisIDWorker {\n&nbsp; &nbsp; private static final long BEGIN_TIMESTAMP = 1640995200L;\n\n&nbsp; &nbsp; \/**\n&nbsp; &nbsp; &nbsp;* \u5e8f\u5217\u53f7\u4f4d\u6570\n&nbsp; &nbsp; &nbsp;*\/\n&nbsp; &nbsp; private static final int COUNT_BITS = 32;\n\n&nbsp; &nbsp; private StringRedisTemplate stringRedisTemplate;\n\n&nbsp; &nbsp; public RedisIDWorker(StringRedisTemplate stringRedisTemplate) {\n&nbsp; &nbsp; &nbsp; &nbsp; this.stringRedisTemplate = stringRedisTemplate;\n&nbsp; &nbsp; }\n\n\n&nbsp; &nbsp; public long nextID(String keyPrefix) {\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/1\u3001\u751f\u6210\u65f6\u95f4\u6233\n&nbsp; &nbsp; &nbsp; &nbsp; LocalDateTime now = LocalDateTime.now();\n&nbsp; &nbsp; &nbsp; &nbsp; long nowScond = now.toEpochSecond(ZoneOffset.UTC);\n&nbsp; &nbsp; &nbsp; &nbsp; long timestamp = nowScond - BEGIN_TIMESTAMP;\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/2\u3001\u751f\u6210\u5e8f\u5217\u53f7\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ 2.1 \u83b7\u53d6\u5f53\u524d\u65e5\u671f\uff0c\u7cbe\u786e\u5230\u5929\n&nbsp; &nbsp; &nbsp; &nbsp; String date = now.format(DateTimeFormatter.ofPattern(\"yyyy:MM:dd\"));\n&nbsp; &nbsp; &nbsp; &nbsp; long count = stringRedisTemplate.opsForValue().increment(\"icr\" + keyPrefix + \":\" + date);\n\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/3\u3001\u62fc\u63a5\u5b57\u7b26\u4e32\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ \u65f6\u95f4\u6233\u5de6\u79fb32\u4f4d\uff0c\u7136\u540e \u6216 \u5e8f\u5217\u53f7\uff0c\u67091\u4e3a1\n&nbsp; &nbsp; &nbsp; &nbsp; long ids = timestamp \n<h2>\u6d4b\u8bd5\u7c7b\uff1a<\/h2>\n<pre>@Resource\n&nbsp; &nbsp; RedisIDWorker redisIDWorker;\n\n&nbsp; &nbsp; private ExecutorService es = Executors.newFixedThreadPool(500);\n\n&nbsp; &nbsp; @Test\n&nbsp; &nbsp; public void ShowID() throws InterruptedException {\n&nbsp; &nbsp; &nbsp; &nbsp; CountDownLatch countDownLatch = new CountDownLatch(300);\n&nbsp; &nbsp; &nbsp; &nbsp; Runnable task = () -&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i \n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u751f\u6210\u5168\u5c40\u552f\u4e00ID\u7684\u5b9e\u73b0&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201243","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201243"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201243\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}