{"id":43747,"date":"2024-12-01T10:46:50","date_gmt":"2024-12-01T02:46:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43747\/"},"modified":"2024-12-01T10:46:50","modified_gmt":"2024-12-01T02:46:50","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:\/\/fwq.ai\/blog\/43747\/","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><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<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\" title=\"Redis\u751f\u6210\u5168\u5c40\u552f\u4e00ID\u7684\u5b9e\u73b0\u65b9\u6cd5\u63d2\u56fe\" alt=\"Redis\u751f\u6210\u5168\u5c40\u552f\u4e00ID\u7684\u5b9e\u73b0\u65b9\u6cd5\u63d2\u56fe\" \/><\/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<p>\u4eca\u5929\u5173\u4e8e\u300aRedis\u751f\u6210\u5168\u5c40\u552f\u4e00ID\u7684\u5b9e\u73b0\u65b9\u6cd5\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p><\/pre>\n<p>   \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1a\u811a\u672c\u4e4b\u5bb6 \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664  <\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5173\u4e8eRedis\u7684\u4e3b\u4ece\u590d\u5236\u53ca\u54e8\u5175\u95ee\u9898\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u6d45\u8c08Redis\u5e38\u89c1\u5ef6\u8fdf\u95ee\u9898\u5b9a\u4f4d\u4e0e\u5206\u6790\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u751f\u6210\u5168\u5c40\u552f\u4e00ID\u7684\u5b9e\u73b0\u65b9\u6cd5 0\u6d4f\u89c8 \u6536\u85cf \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 \u7b80\u4ecb: \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 \u7279\u6027\uff1a \u552f\u4e00\u6027 \u9ad8\u6027\u80fd \u5b89\u5168\u6027 \u9ad8\u53ef\u7528 \u9012\u589e\u6027 \u751f\u6210\u89c4\u5219\uff1a \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 ID\u7ec4\u6210\u90e8\u5206\uff1a \u7b26\u53f7\u4f4d\uff1a1bit\uff0c\u6c38\u8fdc\u4e3a0 \u65f6\u95f4\u6233\uff1a31bit\uff0c\u4ee5\u79d2\u4e3a\u5355\u4f4d\uff0c\u53ef\u4ee5\u4f7f\u752869\u5e74 \u5e8f\u5217\u53f7\uff1a32bit\uff0c\u79d2\u5185\u7684\u8ba1\u6570\u5668\uff0c\u652f\u6301\u6bcf\u79d2\u4ea7\u751f2^32\u4e2a\u4e0d\u540cID &nbsp;ID\u751f\u6210\u7c7b\uff1a package com.example.util; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; \/** &nbsp;* @Author DaBai &nbsp;* @Date 2022\/3\/17 \u4e0b\u5348 09:25 &nbsp;* @Version 1.0 &nbsp;*\/ @Component public class RedisIDWorker { &nbsp; &nbsp; private static [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-43747","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=43747"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43747\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}