{"id":169249,"date":"2024-11-06T18:35:17","date_gmt":"2024-11-06T10:35:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/169249\/"},"modified":"2024-11-06T18:35:18","modified_gmt":"2024-11-06T10:35:18","slug":"%e7%a7%92%e6%ae%ba%e7%b5%90%e6%9d%9fredis%e5%a6%82%e4%bd%95%e5%84%aa%e9%9b%85%e5%87%ba%e5%b1%80%ef%bc%88redis%e7%a7%92%e6%ae%ba%e7%b5%90%e6%9d%9f%e5%be%8c%e6%93%8d%e4%bd%9c%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/169249\/","title":{"rendered":"\u79d2\u6bba\u7d50\u675fRedis\u5982\u4f55\u512a\u96c5\u51fa\u5c40\uff08redis\u79d2\u6bba\u7d50\u675f\u5f8c\u64cd\u4f5c\uff09"},"content":{"rendered":"<h1 id=\"%e7%a7%92%e6%ae%ba%e7%b5%90%e6%9d%9fredis%e5%a6%82%e4%bd%95%e5%84%aa%e9%9b%85%e5%87%ba%e5%b1%80%ef%bc%88redis%e7%a7%92%e6%ae%ba%e7%b5%90%e6%9d%9f%e5%be%8c%e6%93%8d%e4%bd%9c%ef%bc%89-fpYBHTTcWe\">\u79d2\u6bba\u7d50\u675fRedis\u5982\u4f55\u512a\u96c5\u51fa\u5c40\uff08redis\u79d2\u6bba\u7d50\u675f\u5f8c\u64cd\u4f5c\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\u7684\u91cd\u8981\u624b\u6bb5\u3002\u9019\u985e\u6d3b\u52d5\u901a\u5e38\u6703\u5728\u77ed\u6642\u9593\u5167\u63d0\u4f9b\u9650\u91cf\u5546\u54c1\uff0c\u5438\u5f15\u5927\u91cf\u7528\u6236\u53c3\u8207\u3002\u7136\u800c\uff0c\u96a8\u8457\u79d2\u6bba\u6d3b\u52d5\u7684\u7d50\u675f\uff0c\u5982\u4f55\u512a\u96c5\u5730\u8655\u7406Redis\u4e2d\u7684\u6578\u64da\uff0c\u78ba\u4fdd\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u6578\u64da\u7684\u4e00\u81f4\u6027\uff0c\u6210\u70ba\u4e86\u958b\u767c\u8005\u9700\u8981\u9762\u5c0d\u7684\u91cd\u8981\u6311\u6230\u3002<\/p>\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%e8%a7%92%e8%89%b2-fpYBHTTcWe\">Redis\u5728\u79d2\u6bba\u6d3b\u52d5\u4e2d\u7684\u89d2\u8272<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u56e0\u5176\u5feb\u901f\u7684\u8b80\u5beb\u6027\u80fd\u800c\u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u79d2\u6bba\u6d3b\u52d5\u4e2d\u3002\u5728\u79d2\u6bba\u6d3b\u52d5\u958b\u59cb\u6642\uff0c\u5546\u54c1\u7684\u5eab\u5b58\u6578\u91cf\u901a\u5e38\u6703\u88ab\u5b58\u5132\u5728Redis\u4e2d\uff0c\u4e26\u901a\u904e\u539f\u5b50\u64cd\u4f5c\u4f86\u78ba\u4fdd\u5eab\u5b58\u7684\u6e96\u78ba\u6027\u3002\u7576\u7528\u6236\u767c\u8d77\u8cfc\u8cb7\u8acb\u6c42\u6642\uff0c\u7cfb\u7d71\u6703\u6aa2\u67e5Redis\u4e2d\u7684\u5eab\u5b58\u6578\u91cf\uff0c\u4e26\u6839\u64da\u5eab\u5b58\u60c5\u6cc1\u9032\u884c\u76f8\u61c9\u7684\u8655\u7406\u3002<\/p>\n<h2 id=\"%e7%a7%92%e6%ae%ba%e7%b5%90%e6%9d%9f%e5%be%8c%e7%9a%84%e6%95%b8%e6%93%9a%e8%99%95%e7%90%86-fpYBHTTcWe\">\u79d2\u6bba\u7d50\u675f\u5f8c\u7684\u6578\u64da\u8655\u7406<\/h2>\n<p>\u7576\u79d2\u6bba\u6d3b\u52d5\u7d50\u675f\u5f8c\uff0c\u958b\u767c\u8005\u9700\u8981\u9032\u884c\u4e00\u7cfb\u5217\u7684\u6578\u64da\u8655\u7406\u64cd\u4f5c\uff0c\u4ee5\u78ba\u4fdd\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u64cd\u4f5c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e6%b8%85%e7%90%86%e9%81%8e%e6%9c%9f%e7%9a%84%e8%ab%8b%e6%b1%82-fpYBHTTcWe\">1. \u6e05\u7406\u904e\u671f\u7684\u8acb\u6c42<\/h3>\n<p>\u5728\u79d2\u6bba\u6d3b\u52d5\u7d50\u675f\u5f8c\uff0c\u7cfb\u7d71\u4e2d\u53ef\u80fd\u6703\u5b58\u5728\u4e00\u4e9b\u672a\u8655\u7406\u7684\u8acb\u6c42\u3002\u9019\u4e9b\u8acb\u6c42\u53ef\u80fd\u662f\u7531\u65bc\u7528\u6236\u5728\u6d3b\u52d5\u7d50\u675f\u5f8c\u4ecd\u7136\u5617\u8a66\u8cfc\u8cb7\u5546\u54c1\u800c\u7522\u751f\u7684\u3002\u958b\u767c\u8005\u53ef\u4ee5\u901a\u904e\u8a2d\u7f6e\u904e\u671f\u6642\u9593\u4f86\u81ea\u52d5\u6e05\u7406\u9019\u4e9b\u8acb\u6c42\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>redis.call(\"DEL\", \"pending_requests\")<\/code><\/pre>\n<h3 id=\"2-%e6%9b%b4%e6%96%b0%e5%ba%ab%e5%ad%98%e6%95%b8%e9%87%8f-fpYBHTTcWe\">2. \u66f4\u65b0\u5eab\u5b58\u6578\u91cf<\/h3>\n<p>\u79d2\u6bba\u6d3b\u52d5\u7d50\u675f\u5f8c\uff0c\u958b\u767c\u8005\u9700\u8981\u6839\u64da\u5be6\u969b\u7684\u92b7\u552e\u60c5\u6cc1\u66f4\u65b0Redis\u4e2d\u7684\u5eab\u5b58\u6578\u91cf\u3002\u9019\u53ef\u4ee5\u901a\u904e\u5c07\u5df2\u552e\u51fa\u7684\u5546\u54c1\u6578\u91cf\u5f9e\u5eab\u5b58\u4e2d\u6e1b\u53bb\u4f86\u5be6\u73fe\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>local sold_count = redis.call(\"GET\", \"sold_count\")\nlocal stock_count = redis.call(\"GET\", \"stock_count\")\nredis.call(\"SET\", \"stock_count\", stock_count - sold_count)<\/code><\/pre>\n<h3 id=\"3-%e9%80%9a%e7%9f%a5%e7%94%a8%e6%88%b6-fpYBHTTcWe\">3. \u901a\u77e5\u7528\u6236<\/h3>\n<p>\u5728\u79d2\u6bba\u6d3b\u52d5\u7d50\u675f\u5f8c\uff0c\u958b\u767c\u8005\u9084\u9700\u8981\u5411\u7528\u6236\u767c\u9001\u901a\u77e5\uff0c\u544a\u77e5\u4ed6\u5011\u8cfc\u8cb7\u7d50\u679c\u3002\u9019\u53ef\u4ee5\u901a\u904e\u6d88\u606f\u968a\u5217\u6216\u63a8\u9001\u670d\u52d9\u4f86\u5be6\u73fe\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Redis\u7684\u767c\u5e03\/\u8a02\u95b1\u529f\u80fd\u4f86\u901a\u77e5\u7528\u6236\uff1a<\/p>\n<pre><code>redis.call(\"PUBLISH\", \"user_notifications\", \"\u79d2\u6bba\u6d3b\u52d5\u5df2\u7d50\u675f\uff0c\u611f\u8b1d\u60a8\u7684\u53c3\u8207\uff01\")<\/code><\/pre>\n<h3 id=\"4-%e6%95%b8%e6%93%9a%e6%8c%81%e4%b9%85%e5%8c%96-fpYBHTTcWe\">4. \u6578\u64da\u6301\u4e45\u5316<\/h3>\n<p>\u70ba\u4e86\u9632\u6b62\u6578\u64da\u4e1f\u5931\uff0c\u958b\u767c\u8005\u61c9\u8a72\u5c07\u79d2\u6bba\u6d3b\u52d5\u7684\u7d50\u679c\u6301\u4e45\u5316\u5230\u6578\u64da\u5eab\u4e2d\u3002\u9019\u53ef\u4ee5\u901a\u904e\u5c07Redis\u4e2d\u7684\u6578\u64da\u540c\u6b65\u5230\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u6216NoSQL\u6578\u64da\u5eab\u4f86\u5be6\u73fe\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>-- \u5c07\u6578\u64da\u5beb\u5165MySQL\nINSERT INTO sales (product_id, sold_count) VALUES (product_id, sold_count)<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-fpYBHTTcWe\">\u7d50\u8ad6<\/h2>\n<p>\u5728\u79d2\u6bba\u6d3b\u52d5\u7d50\u675f\u5f8c\uff0c\u958b\u767c\u8005\u9700\u8981\u9032\u884c\u4e00\u7cfb\u5217\u7684\u6578\u64da\u8655\u7406\u64cd\u4f5c\uff0c\u4ee5\u78ba\u4fdd\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002\u901a\u904e\u6e05\u7406\u904e\u671f\u8acb\u6c42\u3001\u66f4\u65b0\u5eab\u5b58\u6578\u91cf\u3001\u901a\u77e5\u7528\u6236\u4ee5\u53ca\u6578\u64da\u6301\u4e45\u5316\u7b49\u6b65\u9a5f\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406Redis\u4e2d\u7684\u6578\u64da\uff0c\u78ba\u4fdd\u7528\u6236\u9ad4\u9a57\u7684\u6d41\u66a2\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u96fb\u5b50\u5546\u52d9\u5e73\u53f0\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u4ee5\u78ba\u4fdd\u60a8\u7684\u7cfb\u7d71\u5728\u9ad8\u6d41\u91cf\u6642\u671f\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Redis\u79d2\u6bba\u7d50\u675f\u5f8c\u7684\u512a\u96c5\u51fa\u5c40\u7b56\u7565\uff0c\u78ba\u4fdd\u6578\u64da\u4e00\u81f4\u6027\u8207\u7cfb\u7d71\u7a69\u5b9a\u6027\uff0c\u63d0\u5347\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-169249","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/169249","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=169249"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/169249\/revisions"}],"predecessor-version":[{"id":169250,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/169249\/revisions\/169250"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=169249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=169249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=169249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}