{"id":201303,"date":"2025-05-10T11:07:09","date_gmt":"2025-05-10T03:07:09","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201303\/"},"modified":"2025-05-10T11:07:09","modified_gmt":"2025-05-10T03:07:09","slug":"redis%e4%ba%8b%e5%8a%a1%e5%a4%84%e7%90%86%e7%9a%84%e4%bd%bf%e7%94%a8%e6%93%8d%e4%bd%9c%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201303\/","title":{"rendered":"Redis\u4e8b\u52a1\u5904\u7406\u7684\u4f7f\u7528\u64cd\u4f5c\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u4e8b\u52a1\u5904\u7406\u7684\u4f7f\u7528\u64cd\u4f5c\u65b9\u6cd5<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728\u6570\u636e\u5e93\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u5c31\u6574\u7406\u5206\u4eab\u300aRedis\u4e8b\u52a1\u5904\u7406\u7684\u4f7f\u7528\u64cd\u4f5c\u65b9\u6cd5\u300b\uff0c\u804a\u804a\u5904\u7406\u3001Redis\u4e8b\u52a1\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<h2>\u4e00\u3001\u7b80\u4ecb<\/h2>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Redis\u91c7\u7528\u4e86<strong>\u4e50\u89c2\u9501\u65b9\u5f0f\u8fdb\u884c\u4e8b\u52a1\u63a7\u5236<\/strong>\uff0c\u5b83\u4f7f\u7528watch\u547d\u4ee4\u76d1\u89c6\u7ed9\u5b9a\u7684key\uff0c\u5f53exec\uff08\u63d0\u4ea4\u4e8b\u52a1\uff09\u7684\u65f6\u5019,\u5982\u679c\u76d1\u89c6\u7684key\u4ece\u8c03\u7528watch\u540e\u53d1\u751f\u8fc7\u53d8\u5316\uff0c\u5219\u6574\u4e2a\u4e8b\u52a1\u4f1a\u5931\u8d25\u3002\u4e5f\u53ef\u4ee5\u8c03\u7528watch\u591a\u6b21\u76d1\u89c6\u591a\u4e2akey\u3002\u6ce8\u610fwatch\u7684key\u662f\u5bf9\u6574\u4e2a\u8fde\u63a5\u6709\u6548\u7684\uff0c\u5982\u679c\u8fde\u63a5\u65ad\u5f00\uff0c\u76d1\u89c6\u548c\u4e8b\u52a1\u90fd\u4f1a\u88ab\u81ea\u52a8\u6e05\u9664\u3002\u5f53\u7136exec\uff0cdiscard\uff0cunwatch\u547d\u4ee4\u90fd\u4f1a\u6e05\u9664\u8fde\u63a5\u4e2d\u7684\u6240\u6709\u76d1\u89c6\u3002<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Redis\u4fdd\u8bc1\u4e00\u4e2a\u4e8b\u52a1\u4e2d\u7684\u6240\u6709\u547d\u4ee4\u8981\u4e48\u90fd\u6267\u884c\uff0c\u8981\u4e48\u90fd\u4e0d\u6267\u884c(\u539f\u5b50\u6027)\u3002\u5982\u679c\u5728\u53d1\u9001EXEC\u547d\u4ee4\u524d\u5ba2\u6237\u7aef\u65ad\u7ebf\u4e86\uff0c\u5219Redis\u4f1a\u6e05\u7a7a\u4e8b\u52a1\u961f\u5217\uff0c\u4e8b\u52a1\u4e2d\u7684\u6240\u6709\u547d\u4ee4\u90fd\u4e0d\u4f1a\u6267\u884c\u3002\u800c\u4e00\u65e6\u5ba2\u6237\u7aef\u53d1\u9001\u4e86EXEC\u547d\u4ee4\uff0c\u6240\u6709\u7684\u547d\u4ee4\u5c31\u90fd\u4f1a\u88ab\u6267\u884c\uff0c\u5373\u4f7f\u6b64\u540e\u5ba2\u6237\u7aef\u65ad\u7ebf\u4e5f\u6ca1\u5173\u7cfb\uff0c\u56e0\u4e3aRedis\u4e2d\u5df2\u7ecf\u8bb0\u5f55\u4e86\u6240\u6709\u8981\u6267\u884c\u7684\u547d\u4ee4\u3002<\/p>\n<p><strong>\u5e38\u7528\u6307\u4ee4\uff1a<\/strong><\/p>\n<ul>\n<li>multi \u5f00\u542f\u4e8b\u52a1<\/li>\n<li>exec \u63d0\u4ea4\u4e8b\u52a1<\/li>\n<li>discard \u53d6\u6d88\u4e8b\u52a1<\/li>\n<li>watch \u76d1\u63a7\uff0c\u5982\u679c\u76d1\u63a7\u7684\u503c\u53d1\u751f\u53d8\u5316\uff0c\u5219\u63d0\u4ea4\u4e8b\u52a1\u65f6\u4f1a\u5931\u8d25<\/li>\n<li>unwatch \u53bb\u6389\u76d1\u63a7<\/li>\n<\/ul>\n<h2><strong>\u4e8c\u3001\u6a21\u62df\u4f7f\u7528<\/strong><\/h2>\n<h3>\u6a21\u62df\u8f6c\u8d26\u64cd\u4f5c<\/h3>\n<p style=\"text-align: center\"><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20230102\/167261859363b222618a82b.png\" class=\"aligncenter\"><\/p>\n<p>&nbsp;\u5f00\u542f\u4e8b\u52a1\u540e\u6240\u6709\u64cd\u4f5c\u90fd\u4f1a\u8fdb\u5165\u5230\u4e00\u4e2a\u961f\u5217\u4e2d\uff0c\u63d0\u4ea4\u65f6\u4e00\u8d77\u6267\u884c<\/p>\n<h3>\u6a21\u62df\u53d6\u6d88\u4e8b\u52a1<\/h3>\n<p>redis\u4e8b\u52a1\u592a\u7b80\u5355\uff0c\u6ca1\u6709\u56de\u6eda\uff0c\u800c\u53ea\u6709\u53d6\u6d88\u3002<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20230102\/167261859363b22261df0cd.png\" class=\"aligncenter\"><\/p>\n<p>\u5f53\u961f\u5217\u4e2d\u6709\u8bed\u53e5\u51fa\u73b0\u9519\u8bef\u65f6\uff0c\u4e8b\u52a1\u4f1a\u81ea\u52a8\u53d6\u6d88<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20230102\/167261859463b222623bd73.png\" class=\"aligncenter\"><\/p>\n<h3>&nbsp;\u4e50\u89c2\u9501\u4f7f\u7528\u6f14\u793a<\/h3>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u4e50\u89c2\u9501(Optimistic Lock), \u987e\u540d\u601d\u4e49\uff0c\u5c31\u662f\u5f88\u4e50\u89c2\uff0c\u6bcf\u6b21\u53bb\u62ff\u6570\u636e\u7684\u65f6\u5019\u90fd\u8ba4\u4e3a\u522b\u4eba\u4e0d\u4f1a\u4fee\u6539\uff0c\u6240\u4ee5\u4e0d\u4f1a\u4e0a\u9501\uff0c\u671f\u95f4\u8be5\u6570\u636e\u53ef\u4ee5\u968f\u4fbf\u88ab\u5176\u4ed6\u4eba\u8bfb\u53d6\uff0c\u4f46\u662f\u5728\u66f4\u65b0\u7684\u65f6\u5019\u4f1a\u5224\u65ad\u4e00\u4e0b\u5728\u6b64\u671f\u95f4\u522b\u4eba\u6709\u6ca1\u6709\u53bb\u66f4\u65b0\u8fd9\u4e2a\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u7248\u672c\u53f7\u7b49\u673a\u5236\u3002<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u7248\u672c\u53f7\u673a\u5236\u662f\u4e50\u89c2\u9501\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u5c31\u662f\u5728\u8868\u4e2d\u589e\u52a0\u4e00\u4e2a\u7248\u672c\u53f7\u7684\u5b57\u6bb5\uff0c\u66f4\u65b0\u524d\u5148\u67e5\u4e00\u904d\u83b7\u53d6\u7248\u672c\u53f7\uff0c\u518d\u4f5c\u4e3a\u66f4\u65b0\u8bed\u53e5\u7684where\u6761\u4ef6\u8fdb\u884c\u66f4\u65b0\uff0c\u5982\u679c\u6570\u636e\u5728\u83b7\u53d6\u7248\u672c\u53f7\u4e4b\u540e\uff0c\u5728\u66f4\u65b0\u4e4b\u524d\u5df2\u7ecf\u6539\u53d8\u4e86\uff0c\u90a3\u5c31\u4f1a\u66f4\u65b0\u5931\u8d25\uff0c\u56e0\u4e3a\u6700\u540e\u66f4\u65b0\u4e860\u6761\u6570\u636e\uff0cjava\u540e\u53f0\u62ff\u5230\u66f4\u65b0\u6570\u5982\u679c\u4e3a0\uff0c\u5219\u8bf4\u660e\u66f4\u65b0\u5931\u8d25\uff0c\u51fa\u73b0\u4e86\u5e76\u53d1\u95ee\u9898\uff0c\u7136\u540e\u505a\u5177\u4f53\u7684\u5904\u7406\u3002<\/p>\n<h3>1.\u6dfb\u52a0\u6d4b\u8bd5\u6570\u636e\u8bed\u53e5<\/h3>\n<p>\u5f00\u542f\u76d1\u63a7\uff0c\u5f00\u542f\u4e8b\u52a1\uff0c\u6267\u884c\u8bed\u53e5<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20230102\/167261859463b222629731c.png\" class=\"aligncenter\">&nbsp;<\/p>\n<h3>&nbsp;2.\u53e6\u8d77\u4e00\u4e2aredis\u5bf9\u6570\u636e\u8fdb\u884c\u4fee\u6539<\/h3>\n<p style=\"text-align: center\"><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20230102\/167261859463b22262ee8aa.png\" class=\"aligncenter\"><\/p>\n<h3>&nbsp;3.\u8fd4\u56de\u7b2c\u4e00\u4e2a\u6570\u636e\u5e93\u63d0\u4ea4\u4e8b\u52a1<\/h3>\n<p>\u8fd9\u91cc\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u6267\u884c\u4e8b\u52a1\u5931\u8d25\u3002a\u53d8\u6210\u4e86666<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20230102\/167261859563b222633e016.png\" class=\"aligncenter\"><\/p>\n<h2>&nbsp;\u4e09\u3001\u4e50\u89c2\u9501\u79d2\u6740\u62a2\u7968\u7ec3\u4e60<\/h2>\n<pre>\n\/*\u57fa\u4e8ereids\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u591a\u7ebf\u7a0b\u62a2\u7968\u64cd\u4f5c\n* \u91cd\u70b9\u5ef6\u65f6\u4e50\u89c2\u9501\u7684\u5e94\u7528*\/\npublic class SecondsKillDemo02 {\n    \/\/\u5b9a\u4e49\u62a2\u7968\u903b\u8f91\n    public static void KillTicket() {\n        \/\/1.\u8fde\u63a5\n        Jedis jedis = JedisDataSource.getConnection();\n        \/\/2.\u76d1\u63a7reids\u4e2d\u6307\u5b9a\u7684key\n        String a = jedis.get(\"a\");\n        if (a == null || Integer.valueOf(a) == 0)\n            throw new RuntimeException(\"\u6ca1\u7968\u4e86\");\n        jedis.watch(\"a\", \"b\");\n        \/\/3.\u5f00\u542f\u4e8b\u52a1\u6267\u884c\u4e1a\u52a1\n        Transaction multi = jedis.multi();\n        try {\n            multi.decr(\"a\");\n            multi.incrBy(\"b\", 100);\n            \/\/4.\u63d0\u4ea4\u4e8b\u52a1\n             multi.discard();\n            System.out.println(\"ok\");\n        } catch (Exception e) {\n            multi.exec();\n        } finally {\n            \/\/5.\u53d6\u6d88\u76d1\u63a7\n            jedis.unwatch();\n            \/\/6.\u91ca\u653e\n            jedis.close();\n        }\n    }\n \n    public static void main(String[] args) {\n        \/\/1.\u5b9a\u4e49\u521d\u59cb\u6570\u636e\n        Jedis jedis = JedisDataSource.getConnection();\n        jedis.set(\"a\", \"1\");\n        jedis.set(\"b\", \"0\");\n        \/\/2.\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\uff0c\u5728\u7ebf\u7a0b\u4e2d\u6267\u884c\u62a2\u7968\n        Thread t1 = new Thread(new Runnable() {\n            @Override\n            public void run() {\n                KillTicket();\n            }\n        });\n        Thread t2 = new Thread(new Runnable() {\n            @Override\n            public void run() {\n                KillTicket();\n            }\n        });\n        t1.start();\n        t2.start();\n \n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u4e8b\u52a1\u5904\u7406\u7684\u4f7f\u7528\u64cd\u4f5c\u65b9\u6cd5&#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-201303","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201303","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=201303"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201303\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}