{"id":201595,"date":"2025-05-10T08:37:34","date_gmt":"2025-05-10T00:37:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201595\/"},"modified":"2025-05-10T08:37:34","modified_gmt":"2025-05-10T00:37:34","slug":"redis%e7%9a%84setnx%e5%88%86%e5%b8%83%e5%bc%8f%e9%94%81%e8%b6%85%e6%97%b6%e6%97%b6%e9%97%b4%e5%a4%b1%e6%95%88-1%e9%97%ae%e9%a2%98%e5%8f%8a%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201595\/","title":{"rendered":"Redis\u7684setNX\u5206\u5e03\u5f0f\u9501\u8d85\u65f6\u65f6\u95f4\u5931\u6548-1\u95ee\u9898\u53ca\u89e3\u51b3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u7684setNX\u5206\u5e03\u5f0f\u9501\u8d85\u65f6\u65f6\u95f4\u5931\u6548-1\u95ee\u9898\u53ca\u89e3\u51b3<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6570\u636e\u5e93\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aRedis\u7684setNX\u5206\u5e03\u5f0f\u9501\u8d85\u65f6\u65f6\u95f4\u5931\u6548-1\u95ee\u9898\u53ca\u89e3\u51b3\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ecRedis\u7684setNX\u5206\u5e03\u5f0f\u9501\u8d85\u65f6\u65f6\u95f4\u5931\u6548-1\u95ee\u9898\u53ca\u89e3\u51b3\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<h2>Redis setNX\u5206\u5e03\u5f0f\u9501\u8d85\u65f6\u65f6\u95f4\u5931\u6548 -1<\/h2>\n<h3>\u4f7f\u7528SETNX\u52a0\u9501<\/h3>\n<p>\u52a0\u9501\u7684\u601d\u8def\uff1a<\/p>\n<p>\u5982\u679c key \u4e0d\u5b58\u5728\uff0c\u5c06 key \u8bbe\u7f6e\u4e3a value\uff0c\u5982\u679c key \u5df2\u5b58\u5728\uff0c\u5219 SETNX \u4e0d\u505a\u4efb\u4f55\u52a8\u4f5c\u3002<\/p>\n<h3>\u4f7f\u7528 RedisTemplate \u64cd\u4f5cRedis<\/h3>\n<pre>&nbsp; &nbsp; @Autowired\n&nbsp; &nbsp; private RedisTemplate redisTemplate;\n&nbsp;\n&nbsp; &nbsp; @RequestMapping(\"\/setNx\")\n&nbsp; &nbsp; public String setNx() {\n&nbsp; &nbsp; &nbsp; &nbsp; String key = \"redis_nx_test\";\n&nbsp; &nbsp; &nbsp; &nbsp; String value = \"1\";\n&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; boolean res = redisTemplate.getConnectionFactory().getConnection().setNX(key.getBytes(), value.getBytes());\n&nbsp; &nbsp; &nbsp; &nbsp; log.info(\"setNX:{}\", res);\n&nbsp; &nbsp; &nbsp; &nbsp; if (res) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; redisTemplate.getConnectionFactory().getConnection().expire(key.getBytes(), 50);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.info(\"\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\");\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; return \"success\";\n&nbsp; &nbsp; }<\/pre>\n<h3>\u89e3\u51b3\u6b7b\u9501<\/h3>\n<p>\u8003\u8651\u4e00\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u8fdb\u7a0b\u83b7\u5f97\u9501\u540e\uff0c\u65ad\u5f00\u4e86\u4e0e Redis \u7684\u8fde\u63a5\uff08\u53ef\u80fd\u662f\u8fdb\u7a0b\u6302\u6389\uff0c\u6216\u8005\u7f51\u7edc\u4e2d\u65ad\uff09\uff0c\u5982\u679c\u6ca1\u6709\u6709\u6548\u7684\u91ca\u653e\u9501\u7684\u673a\u5236\uff0c\u90a3\u4e48\u5176\u4ed6\u8fdb\u7a0b\u90fd\u4f1a\u5904\u4e8e\u4e00\u76f4\u7b49\u5f85\u7684\u72b6\u6001\uff0c\u5373\u51fa\u73b0\u201c\u6b7b\u9501\u201d\u3002<\/p>\n<p>\u76ee\u524d\u8003\u8651\u662f\u4f7f\u7528\u5b9a\u65f6\u4efb\u52a1\u8fdb\u884c\u626b\u63cf\u5904\u7406\u8d85\u65f6\u65f6\u95f4\u4e3a -1\u7684key<\/p>\n<pre>@Scheduled(initialDelay = 1000,fixedDelay = 1000*60*30)\n&nbsp; &nbsp; public void deleteExceptionKey(){\n&nbsp; &nbsp; &nbsp; &nbsp; String key = \"redis_nx_test\";\n&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/\u8fd4\u56de -2 &nbsp; \u8868\u793a\u8fd9\u4e2akey\u5df2\u8fc7\u671f\uff0c\u5df2\u4e0d\u5b58\u5728\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/\u8fd4\u56de -1 &nbsp; \u8868\u793a\u8fd9\u4e2akey\u6ca1\u6709\u8bbe\u7f6e\u6709\u6548\u671f\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/\u8fd4\u56de0\u4ee5\u4e0a\u7684\u503c &nbsp; \u8868\u793a\u662f\u8fd9\u4e2akey\u7684\u5269\u4f59\u6709\u6548\u65f6\u95f4\n&nbsp; &nbsp; &nbsp; &nbsp; Long l = redisTemplate.opsForValue().getOperations().getExpire(key);\n&nbsp; &nbsp; &nbsp; &nbsp; log.info(\"Expire:{}\", l);\n&nbsp; &nbsp; &nbsp; &nbsp; if(l == -1){\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Boolean b = &nbsp;redisTemplate.delete(key);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.info(\"\u5220\u9664key:{},{}\", key,b);\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; }<\/pre>\n<h2>Redis\u5206\u5e03\u5f0f\u9501\uff0c\u8d85\u65f6\u95ee\u9898\u7684\u5904\u7406<\/h2>\n<p>\u5206\u5e03\u5f0f\u9501\u8d85\u65f6\u95ee\u9898\u89e3\u51b3\u65b9\u6848\uff0c\u4e3b\u8981\u662f\u57fa\u4e8eredission\u3002Redisson\u662f\u67b6\u8bbe\u5728Redis\u57fa\u7840\u4e0a\u7684\u4e00\u4e2aJava\u9a7b\u5185\u5b58\u6570\u636e\u7f51\u683c\uff08In-Memory Data Grid\uff09\u3002\u5145\u5206\u7684\u5229\u7528\u4e86Redis\u952e\u503c\u6570\u636e\u5e93\u63d0\u4f9b\u7684\u4e00\u7cfb\u5217\u4f18\u52bf\uff0c\u57fa\u4e8eJava\u5b9e\u7528\u5de5\u5177\u5305\u4e2d\u5e38\u7528\u63a5\u53e3\uff0c\u4e3a\u4f7f\u7528\u8005\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5177\u6709\u5206\u5e03\u5f0f\u7279\u6027\u7684\u5e38\u7528\u5de5\u5177\u7c7b\u3002<\/p>\n<p>redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\uff0c\u4e3b\u8981\u662f\u901a\u8fc7SETNX\u547d\u4ee4\uff0c\u8bbe\u7f6e\u4e00\u7ec4 key,value\u3002\u5728\u540c\u4e00\u65f6\u523b\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8bbe\u7f6e\u6210\u529f\uff0c\u8be5\u7ebf\u7a0b\u83b7\u5f97\u4e86\u5206\u5e03\u5f0f\u9501\u3002<\/p>\n<h3>\u5206\u5e03\u5f0f\u53ef\u80fd\u4f1a\u51fa\u73b0\u7684\u8d85\u65f6\u95ee\u9898<\/h3>\n<p><strong>1\u3001\u8bef\u5220\u9664\u522b\u4eba\u7684\u9501<\/strong><\/p>\n<p>A\u83b7\u53d6\u9501\u540e\uff0c\u4e8b\u52a1\u6267\u884c\u8d85\u65f6\u3002\u9501\u5230\u671f\u81ea\u52a8\u91ca\u653e\uff0cB\u7ebf\u7a0b\u83b7\u53d6\u5230\u9501\u3002A\u5b8c\u6210\u4e8b\u52a1\u540e\uff0c\u91ca\u653e\u9501\u65f6\uff0c\u628aB\u7684\u9501\u91ca\u653e\u4e86\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<p>\u6bcf\u4e2a\u7ebf\u7a0b\u5728\u83b7\u53d6\u9501\u5f0f\uff0c\u5728\u5199key-value\u65f6\uff0c\u5c06\u81ea\u5df1\u751f\u6210\u7684\u4e00\u4e2a\u72ec\u4e00\u65e0\u4e8c\u7684id\u5199\u5165value\u3002\u5148\u6821\u9a8cvalue\uff0c\u518d\u5220\u9664\u9501\u3002\u8fd9\u4e24\u4e2a\u64cd\u4f5c\u8981\u4fdd\u8bc1\u539f\u5b50\u6027\uff0c\u53ef\u4ee5\u91c7\u7528Lua\u811a\u672c\u3002<\/p>\n<p>\uff08\u5982\u679c\u6ca1\u6709\u4fdd\u8bc1\u539f\u5b50\u6027\uff0c\u4f1a\u51fa\u73b0A\u6821\u9a8c\u5b8cvalue,\u9501\u5931\u6548\u3002B\u5199\u5165key-value\u3002A\u5220\u9664B\u5199\u5165\u7684\u9501\u3002\uff09<\/p>\n<p><strong>2\u3001A\u5230\u671f\u81ea\u52a8\u91ca\u653e\u9501\u540e\uff0cB\u83b7\u5f97\u9501\u3002\u51fa\u73b0\u4e24\u4e2a\u9501\u5e76\u884c\u7684\u60c5\u51b5<\/strong><\/p>\n<p>A\u83b7\u53d6\u9501\u540e\uff0c\u5f00\u542f\u4e00\u4e2a\u5b88\u62a4\u7ebf\u7a0b\uff0c\u5b9a\u671f\u4e3aA\u5237\u65b0\u9501\u7684\u65f6\u95f4\u3002\u8fd9\u6837A\u5c31\u662f\u4e00\u76f4\u4fdd\u6301\u9501\u7684\u60c5\u51b5\u3002\u5728redission\u6e90\u7801\u4e2d\u6709\u76f8\u5e94\u7684\u7a0b\u5e8f\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u4ee5\u4e0a\u4e3a\u4e2a\u4eba\u7ecf\u9a8c\uff0c\u5e0c\u671b\u80fd\u7ed9\u5927\u5bb6\u4e00\u4e2a\u53c2\u8003\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u591a\u591a\u652f\u6301golang\u5b66\u4e60\u7f51\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u7684setNX\u5206\u5e03\u5f0f\u9501\u8d85&#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-201595","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201595","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=201595"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201595\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}