{"id":169179,"date":"2024-11-06T18:31:28","date_gmt":"2024-11-06T10:31:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/169179\/"},"modified":"2024-11-06T18:31:29","modified_gmt":"2024-11-06T10:31:29","slug":"%e7%a7%92%e6%ae%ba%e5%a4%a7%e6%88%b0%e7%82%ba%e4%bd%95%e8%a6%81%e7%94%a8redis%ef%bc%88%e7%a7%92%e6%ae%ba%e7%82%ba%e5%95%a5%e8%a6%81%e7%94%a8redis%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/169179\/","title":{"rendered":"\u79d2\u6bba\u5927\u6230\u70ba\u4f55\u8981\u7528Redis\uff08\u79d2\u6bba\u70ba\u5565\u8981\u7528redis\uff09"},"content":{"rendered":"<h1 id=\"%e7%a7%92%e6%ae%ba%e5%a4%a7%e6%88%b0%e7%82%ba%e4%bd%95%e8%a6%81%e7%94%a8redis%ef%bc%88%e7%a7%92%e6%ae%ba%e7%82%ba%e5%95%a5%e8%a6%81%e7%94%a8redis%ef%bc%89-QYNWFwwFwk\">\u79d2\u6bba\u5927\u6230\u70ba\u4f55\u8981\u7528Redis\uff08\u79d2\u6bba\u70ba\u5565\u8981\u7528redis\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u96fb\u5b50\u5546\u52d9\u74b0\u5883\u4e2d\uff0c\u79d2\u6bba\u6d3b\u52d5\u5df2\u6210\u70ba\u5438\u5f15\u9867\u5ba2\u548c\u4fc3\u9032\u92b7\u552e\u7684\u91cd\u8981\u624b\u6bb5\u3002\u7136\u800c\uff0c\u96a8\u8457\u53c3\u8207\u8005\u7684\u589e\u52a0\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u548c\u8655\u7406\u5927\u91cf\u7684\u8acb\u6c42\u6210\u70ba\u4e86\u4e00\u500b\u6311\u6230\u3002\u9019\u6642\uff0cRedis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u4fbf\u6210\u70ba\u4e86\u79d2\u6bba\u6d3b\u52d5\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-QYNWFwwFwk\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u6578\u64da\u5eab\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\u3002\u5b83\u4ee5\u5176\u5feb\u901f\u7684\u8b80\u5beb\u901f\u5ea6\u548c\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\u800c\u805e\u540d\uff0c\u7279\u5225\u9069\u5408\u9700\u8981\u9ad8\u4e26\u767c\u548c\u4f4e\u5ef6\u9072\u7684\u61c9\u7528\u5834\u666f\u3002<\/p>\n<h2 id=\"%e7%a7%92%e6%ae%ba%e6%b4%bb%e5%8b%95%e7%9a%84%e6%8c%91%e6%88%b0-QYNWFwwFwk\">\u79d2\u6bba\u6d3b\u52d5\u7684\u6311\u6230<\/h2>\n<p>\u5728\u79d2\u6bba\u6d3b\u52d5\u4e2d\uff0c\u901a\u5e38\u6703\u6709\u5927\u91cf\u7528\u6236\u540c\u6642\u8a2a\u554f\u7db2\u7ad9\uff0c\u9019\u6703\u5c0e\u81f4\u4f3a\u670d\u5668\u8ca0\u8f09\u904e\u91cd\uff0c\u751a\u81f3\u5d29\u6f70\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u6311\u6230\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u4e26\u767c\u8acb\u6c42\uff1a<\/strong>\u5728\u79d2\u6bba\u6d3b\u52d5\u4e2d\uff0c\u6578\u4ee5\u842c\u8a08\u7684\u7528\u6236\u6703\u5728\u77ed\u6642\u9593\u5167\u767c\u9001\u8acb\u6c42\uff0c\u9019\u5c0d\u4f3a\u670d\u5668\u7684\u8655\u7406\u80fd\u529b\u63d0\u51fa\u4e86\u6975\u9ad8\u7684\u8981\u6c42\u3002<\/li>\n<li><strong>\u6578\u64da\u4e00\u81f4\u6027\uff1a<\/strong>\u5728\u9ad8\u4e26\u767c\u7684\u60c5\u6cc1\u4e0b\uff0c\u5982\u4f55\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u548c\u6e96\u78ba\u6027\u662f\u53e6\u4e00\u500b\u6311\u6230\u3002<\/li>\n<li><strong>\u6027\u80fd\u74f6\u9838\uff1a<\/strong>\u50b3\u7d71\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u5728\u9762\u5c0d\u5927\u91cf\u8acb\u6c42\u6642\uff0c\u53ef\u80fd\u6703\u51fa\u73fe\u6027\u80fd\u74f6\u9838\uff0c\u5c0e\u81f4\u97ff\u61c9\u6642\u9593\u5ef6\u9072\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e5%9c%a8%e7%a7%92%e6%ae%ba%e6%b4%bb%e5%8b%95%e4%b8%ad%e7%9a%84%e5%84%aa%e5%8b%a2-QYNWFwwFwk\">Redis\u5728\u79d2\u6bba\u6d3b\u52d5\u4e2d\u7684\u512a\u52e2<\/h2>\n<p>Redis\u80fd\u5920\u6709\u6548\u89e3\u6c7a\u4e0a\u8ff0\u6311\u6230\uff0c\u4e3b\u8981\u9ad4\u73fe\u5728\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<h3 id=\"1-%e9%ab%98%e6%80%a7%e8%83%bd-QYNWFwwFwk\">1. \u9ad8\u6027\u80fd<\/h3>\n<p>Redis\u7684\u8b80\u5beb\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u901a\u5e38\u5728\u6beb\u79d2\u7d1a\u5225\u3002\u9019\u4f7f\u5f97\u5b83\u80fd\u5920\u5728\u79d2\u6bba\u6d3b\u52d5\u4e2d\u5feb\u901f\u8655\u7406\u5927\u91cf\u8acb\u6c42\uff0c\u6e1b\u5c11\u7528\u6236\u7b49\u5f85\u6642\u9593\u3002<\/p>\n<h3 id=\"2-%e6%94%af%e6%8c%81%e9%ab%98%e4%b8%a6%e7%99%bc-QYNWFwwFwk\">2. \u652f\u6301\u9ad8\u4e26\u767c<\/h3>\n<p>Redis\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u4e26\u4e14\u80fd\u5920\u8655\u7406\u5927\u91cf\u7684\u4e26\u767c\u8acb\u6c42\u3002\u9019\u4f7f\u5f97\u5b83\u6210\u70ba\u79d2\u6bba\u6d3b\u52d5\u4e2d\u7406\u60f3\u7684\u9078\u64c7\uff0c\u80fd\u5920\u540c\u6642\u6eff\u8db3\u591a\u500b\u7528\u6236\u7684\u9700\u6c42\u3002<\/p>\n<h3 id=\"3-%e6%95%b8%e6%93%9a%e6%8c%81%e4%b9%85%e5%8c%96-QYNWFwwFwk\">3. \u6578\u64da\u6301\u4e45\u5316<\/h3>\n<p>\u96d6\u7136Redis\u662f\u4e00\u500b\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4f46\u5b83\u4e5f\u652f\u6301\u6578\u64da\u6301\u4e45\u5316\uff0c\u80fd\u5920\u5c07\u6578\u64da\u5b9a\u671f\u4fdd\u5b58\u5230\u78c1\u789f\u4e2d\uff0c\u9632\u6b62\u6578\u64da\u4e1f\u5931\u3002<\/p>\n<h3 id=\"4-%e7%b0%a1%e5%96%ae%e6%98%93%e7%94%a8-QYNWFwwFwk\">4. \u7c21\u55ae\u6613\u7528<\/h3>\n<p>Redis\u7684API\u7c21\u55ae\u6613\u7528\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\uff0c\u4e26\u4e14\u80fd\u5920\u8f15\u9b06\u96c6\u6210\u5230\u73fe\u6709\u7684\u61c9\u7528\u4e2d\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e6%87%89%e7%94%a8%e7%a4%ba%e4%be%8b-QYNWFwwFwk\">Redis\u7684\u61c9\u7528\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Redis\u9032\u884c\u79d2\u6bba\u6d3b\u52d5\u7684\u7c21\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230Redis\u4f3a\u670d\u5668\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u8a2d\u7f6e\u79d2\u6bba\u5546\u54c1\u7684\u5eab\u5b58\nr.set('product:123:stock', 100)\n\n# \u8655\u7406\u7528\u6236\u8acb\u6c42\ndef handle_request(user_id):\n    # \u6aa2\u67e5\u5eab\u5b58\n    stock = r.get('product:123:stock')\n    if stock and int(stock) &gt; 0:\n        # \u5eab\u5b58\u6e1b\u5c11\n        r.decr('product:123:stock')\n        print(f\"\u7528\u6236 {user_id} \u6210\u529f\u6436\u8cfc\u5546\u54c1\uff01\")\n    else:\n        print(f\"\u7528\u6236 {user_id} \u62b1\u6b49\uff0c\u5546\u54c1\u5df2\u552e\u7f44\u3002\")\n\n# \u6a21\u64ec\u591a\u500b\u7528\u6236\u8acb\u6c42\nfor user_id in range(1, 201):\n    handle_request(user_id)\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-QYNWFwwFwk\">\u7d50\u8ad6<\/h2>\n<p>\u5728\u79d2\u6bba\u6d3b\u52d5\u4e2d\uff0c\u4f7f\u7528Redis\u80fd\u5920\u6709\u6548\u89e3\u6c7a\u9ad8\u4e26\u767c\u8acb\u6c42\u5e36\u4f86\u7684\u6311\u6230\uff0c\u4e26\u4e14\u63d0\u4f9b\u9ad8\u6027\u80fd\u548c\u6578\u64da\u4e00\u81f4\u6027\u3002\u96a8\u8457\u96fb\u5b50\u5546\u52d9\u7684\u767c\u5c55\uff0c\u8d8a\u4f86\u8d8a\u591a\u7684\u4f01\u696d\u9078\u64c7Redis\u4f5c\u70ba\u5176\u5f8c\u7aef\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u4ee5\u61c9\u5c0d\u77ac\u606f\u842c\u8b8a\u7684\u5e02\u5834\u9700\u6c42\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u696d\u52d9\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u61c9\u5c0d\u9ad8\u6d41\u91cf\u7684\u6311\u6230\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u79d2\u6bba\u5927\u6230\u4e2d\u4f7f\u7528Redis\u7684\u539f\u56e0\uff0c\u4e86\u89e3\u5176\u9ad8\u6548\u6027\u80fd\u5982\u4f55\u63d0\u5347\u79d2\u6bba\u6d3b\u52d5\u7684\u97ff\u61c9\u901f\u5ea6\u8207\u7528\u6236\u9ad4\u9a57\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-169179","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/169179","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=169179"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/169179\/revisions"}],"predecessor-version":[{"id":169180,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/169179\/revisions\/169180"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=169179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=169179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=169179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}