{"id":128935,"date":"2024-10-26T17:51:21","date_gmt":"2024-10-26T09:51:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/128935\/"},"modified":"2024-10-26T17:51:22","modified_gmt":"2024-10-26T09:51:22","slug":"redis%e5%af%a6%e7%8f%be%e9%99%90%e6%99%82%e7%a7%92%e6%ae%ba%ef%bc%8c%e4%bb%a4%e6%90%b6%e8%b3%bc%e6%9b%b4%e7%81%ab%e7%86%b1%ef%bc%88redis%e9%99%90%e6%99%82%e7%a7%92%e6%ae%ba%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/128935\/","title":{"rendered":"Redis\u5be6\u73fe\u9650\u6642\u79d2\u6bba\uff0c\u4ee4\u6436\u8cfc\u66f4\u706b\u71b1\uff08redis\u9650\u6642\u79d2\u6bba\uff09"},"content":{"rendered":"<h1 id=\"redis%e5%af%a6%e7%8f%be%e9%99%90%e6%99%82%e7%a7%92%e6%ae%ba%ef%bc%8c%e4%bb%a4%e6%90%b6%e8%b3%bc%e6%9b%b4%e7%81%ab%e7%86%b1-OmXAkLzohv\">Redis\u5be6\u73fe\u9650\u6642\u79d2\u6bba\uff0c\u4ee4\u6436\u8cfc\u66f4\u706b\u71b1<\/h1>\n<p>\u5728\u96fb\u5b50\u5546\u52d9\u7684\u4e16\u754c\u4e2d\uff0c\u9650\u6642\u79d2\u6bba\u6d3b\u52d5\u5df2\u6210\u70ba\u5438\u5f15\u9867\u5ba2\u7684\u91cd\u8981\u624b\u6bb5\u3002\u9019\u985e\u6d3b\u52d5\u901a\u5e38\u6703\u5728\u77ed\u6642\u9593\u5167\u63d0\u4f9b\u7279\u5b9a\u5546\u54c1\u7684\u512a\u60e0\uff0c\u5438\u5f15\u5927\u91cf\u6d88\u8cbb\u8005\u53c3\u8207\u3002\u7136\u800c\uff0c\u96a8\u8457\u53c3\u8207\u4eba\u6578\u7684\u589e\u52a0\uff0c\u5982\u4f55\u6709\u6548\u7ba1\u7406\u9ad8\u4e26\u767c\u7684\u8acb\u6c42\u6210\u70ba\u4e86\u4e00\u500b\u6311\u6230\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u7406\u60f3\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u5be6\u73fe\u9650\u6642\u79d2\u6bba\u6d3b\u52d5\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e7%89%b9%e6%80%a7-OmXAkLzohv\">Redis\u7684\u7279\u6027<\/h2>\n<p>Redis\u662f\u4e00\u500b\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u6578\u64da\u5eab\uff0c\u5177\u6709\u4ee5\u4e0b\u5e7e\u500b\u7279\u9ede\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6548\u80fd\uff1a<\/strong>Redis\u7684\u6578\u64da\u5b58\u5132\u5728\u5167\u5b58\u4e2d\uff0c\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u6975\u5feb\uff0c\u80fd\u5920\u652f\u6301\u6bcf\u79d2\u6578\u5341\u842c\u6b21\u7684\u8acb\u6c42\u3002<\/li>\n<li><strong>\u6578\u64da\u7d50\u69cb\u591a\u6a23\uff1a<\/strong>Redis\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\uff0c\u80fd\u5920\u9748\u6d3b\u61c9\u5c0d\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>\u96d6\u7136Redis\u4e3b\u8981\u662f\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4f46\u5b83\u4e5f\u63d0\u4f9b\u4e86\u6301\u4e45\u5316\u7684\u9078\u9805\uff0c\u78ba\u4fdd\u6578\u64da\u4e0d\u6703\u56e0\u70ba\u670d\u52d9\u5668\u91cd\u555f\u800c\u4e1f\u5931\u3002<\/li>\n<\/ul>\n<h2 id=\"%e9%99%90%e6%99%82%e7%a7%92%e6%ae%ba%e7%9a%84%e5%af%a6%e7%8f%be%e5%8e%9f%e7%90%86-OmXAkLzohv\">\u9650\u6642\u79d2\u6bba\u7684\u5be6\u73fe\u539f\u7406<\/h2>\n<p>\u5728\u9650\u6642\u79d2\u6bba\u6d3b\u52d5\u4e2d\uff0c\u901a\u5e38\u6703\u8a2d\u7f6e\u4e00\u500b\u5546\u54c1\u7684\u5eab\u5b58\u6578\u91cf\uff0c\u4e26\u5728\u7279\u5b9a\u6642\u9593\u5167\u958b\u653e\u6436\u8cfc\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Redis\u5be6\u73fe\u9650\u6642\u79d2\u6bba\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e8%a8%ad%e7%bd%ae%e5%ba%ab%e5%ad%98-OmXAkLzohv\">1. \u8a2d\u7f6e\u5eab\u5b58<\/h3>\n<p>\u9996\u5148\uff0c\u5c07\u5546\u54c1\u7684\u5eab\u5b58\u6578\u91cf\u5b58\u5132\u5728Redis\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528Redis\u7684\u5b57\u7b26\u4e32\u985e\u578b\u4f86\u8a2d\u7f6e\u5eab\u5b58\uff1a<\/p>\n<pre><code>SET product:stock:123 100<\/code><\/pre>\n<p>\u9019\u88e1\uff0c`product:stock:123`\u662f\u5546\u54c1\u7684\u552f\u4e00\u6a19\u8b58\u7b26\uff0c`100`\u662f\u5eab\u5b58\u6578\u91cf\u3002<\/p>\n<h3 id=\"2-%e9%96%8b%e5%a7%8b%e6%90%b6%e8%b3%bc-OmXAkLzohv\">2. \u958b\u59cb\u6436\u8cfc<\/h3>\n<p>\u7576\u79d2\u6bba\u6d3b\u52d5\u958b\u59cb\u6642\uff0c\u5ba2\u6236\u7aef\u6703\u767c\u9001\u8acb\u6c42\u5230\u670d\u52d9\u5668\u3002\u670d\u52d9\u5668\u9700\u8981\u6aa2\u67e5\u5eab\u5b58\u4e26\u9032\u884c\u6263\u6e1b\u3002\u9019\u88e1\u53ef\u4ee5\u4f7f\u7528Redis\u7684\u539f\u5b50\u64cd\u4f5c\u4f86\u78ba\u4fdd\u5eab\u5b58\u7684\u6b63\u78ba\u6027\uff1a<\/p>\n<pre><code>DECR product:stock:123<\/code><\/pre>\n<p>\u9019\u500b\u547d\u4ee4\u6703\u5c07\u5eab\u5b58\u6e1b\u5c111\u3002\u5982\u679c\u8fd4\u56de\u7684\u5eab\u5b58\u6578\u91cf\u5c0f\u65bc0\uff0c\u5247\u8868\u793a\u6436\u8cfc\u5931\u6557\u3002<\/p>\n<h3 id=\"3-%e8%99%95%e7%90%86%e9%ab%98%e4%b8%a6%e7%99%bc%e8%ab%8b%e6%b1%82-OmXAkLzohv\">3. \u8655\u7406\u9ad8\u4e26\u767c\u8acb\u6c42<\/h3>\n<p>\u5728\u9ad8\u4e26\u767c\u7684\u60c5\u6cc1\u4e0b\uff0c\u53ef\u80fd\u6703\u51fa\u73fe\u8d85\u8ce3\u7684\u60c5\u6cc1\u3002\u70ba\u4e86\u907f\u514d\u9019\u7a2e\u60c5\u6cc1\uff0c\u53ef\u4ee5\u4f7f\u7528Redis\u7684\u4e8b\u52d9\u529f\u80fd\u6216Lua\u8173\u672c\u4f86\u78ba\u4fdd\u64cd\u4f5c\u7684\u539f\u5b50\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>\nEVAL \"if redis.call('get', KEYS[1]) &gt; 0 then redis.call('decr', KEYS[1]) return 1 else return 0 end\" 1 product:stock:123\n<\/code><\/pre>\n<p>\u9019\u6bb5Lua\u8173\u672c\u6703\u6aa2\u67e5\u5eab\u5b58\u4e26\u5728\u5eab\u5b58\u5927\u65bc0\u7684\u60c5\u6cc1\u4e0b\u9032\u884c\u6263\u6e1b\uff0c\u5f9e\u800c\u907f\u514d\u8d85\u8ce3\u3002<\/p>\n<h2 id=\"%e5%af%a6%e9%9a%9b%e6%a1%88%e4%be%8b-OmXAkLzohv\">\u5be6\u969b\u6848\u4f8b<\/h2>\n<p>\u8a31\u591a\u77e5\u540d\u96fb\u5546\u5e73\u53f0\u90fd\u4f7f\u7528Redis\u4f86\u5be6\u73fe\u9650\u6642\u79d2\u6bba\u6d3b\u52d5\u3002\u4f8b\u5982\uff0c\u67d0\u5927\u578b\u96fb\u5546\u5728\u96d9\u5341\u4e00\u671f\u9593\uff0c\u4f7f\u7528Redis\u4f86\u7ba1\u7406\u79d2\u6bba\u5546\u54c1\u7684\u5eab\u5b58\uff0c\u6210\u529f\u8655\u7406\u4e86\u6578\u767e\u842c\u7684\u4e26\u767c\u8acb\u6c42\uff0c\u4e26\u4e14\u5728\u6d3b\u52d5\u7d50\u675f\u5f8c\uff0c\u5eab\u5b58\u6578\u64da\u6e96\u78ba\u7121\u8aa4\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-OmXAkLzohv\">\u7e3d\u7d50<\/h2>\n<p>Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u70ba\u9650\u6642\u79d2\u6bba\u6d3b\u52d5\u63d0\u4f9b\u4e86\u5f37\u6709\u529b\u7684\u652f\u6301\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u8a08\u548c\u5be6\u73fe\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u9ad8\u4e26\u767c\u8acb\u6c42\uff0c\u78ba\u4fdd\u6d3b\u52d5\u7684\u9806\u5229\u9032\u884c\u3002\u5c0d\u65bc\u5e0c\u671b\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\u7684\u4f01\u696d\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684\u670d\u52d9\u5668\u67b6\u69cb\u81f3\u95dc\u91cd\u8981\u3002\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u548c<a href=\"https:\/\/server.hk\">\u96f2\u4f3a\u670d\u5668<\/a>\u7684\u8cc7\u8a0a\uff0c\u5c07\u6709\u52a9\u65bc\u60a8\u5728\u96fb\u5b50\u5546\u52d9\u9818\u57df\u53d6\u5f97\u6210\u529f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5229\u7528Redis\u5be6\u73fe\u9650\u6642\u79d2\u6bba\uff0c\u63d0\u5347\u6436\u8cfc\u71b1\u5ea6\uff0c\u8b93\u60a8\u7684\u6d3b\u52d5\u66f4\u5177\u5438\u5f15\u529b\u8207\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-128935","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/128935","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=128935"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/128935\/revisions"}],"predecessor-version":[{"id":128936,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/128935\/revisions\/128936"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=128935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=128935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=128935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}