{"id":169257,"date":"2024-11-06T18:39:20","date_gmt":"2024-11-06T10:39:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/169257\/"},"modified":"2024-11-06T18:39:20","modified_gmt":"2024-11-06T10:39:20","slug":"%e7%a7%92%e6%ae%ba%e6%90%b6%e7%b4%85%e5%8c%85%ef%bc%8credis%e5%8a%a9%e5%8a%9b%e8%b6%85%e7%b4%9a%e5%bf%ab%e6%a8%82%e9%ab%94%e9%a9%97%ef%bc%88redis%e7%a7%92%e6%ae%ba-%e6%90%b6%e7%b4%85%e5%8c%85%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/169257\/","title":{"rendered":"\u79d2\u6bba\u6436\u7d05\u5305\uff0cRedis\u52a9\u529b\u8d85\u7d1a\u5feb\u6a02\u9ad4\u9a57\uff08redis\u79d2\u6bba \u6436\u7d05\u5305\uff09"},"content":{"rendered":"<h1 id=\"%e7%a7%92%e6%ae%ba%e6%90%b6%e7%b4%85%e5%8c%85%ef%bc%8credis%e5%8a%a9%e5%8a%9b%e8%b6%85%e7%b4%9a%e5%bf%ab%e6%a8%82%e9%ab%94%e9%a9%97-NFPamfgppS\">\u79d2\u6bba\u6436\u7d05\u5305\uff0cRedis\u52a9\u529b\u8d85\u7d1a\u5feb\u6a02\u9ad4\u9a57<\/h1>\n<p>\u5728\u4e2d\u570b\u7684\u50b3\u7d71\u7bc0\u65e5\u4e2d\uff0c\u6436\u7d05\u5305\u662f\u4e00\u9805\u5099\u53d7\u6b61\u8fce\u7684\u6d3b\u52d5\u3002\u96a8\u8457\u79d1\u6280\u7684\u9032\u6b65\uff0c\u9019\u4e00\u6d3b\u52d5\u4e5f\u9010\u6f38\u8f49\u5411\u7dda\u4e0a\uff0c\u6210\u70ba\u4e86\u4eba\u5011\u5728\u7bc0\u65e5\u4e2d\u4e92\u76f8\u795d\u798f\u548c\u5206\u4eab\u7684\u65b9\u5f0f\u3002\u7136\u800c\uff0c\u96a8\u8457\u53c3\u8207\u4eba\u6578\u7684\u589e\u52a0\uff0c\u5982\u4f55\u5728\u77ed\u6642\u9593\u5167\u8655\u7406\u5927\u91cf\u7684\u8acb\u6c42\uff0c\u6210\u70ba\u4e86\u6280\u8853\u4e0a\u7684\u4e00\u5927\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\u6436\u7d05\u5305\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-NFPamfgppS\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u958b\u6e90\u7684\u5167\u5b58\u6578\u64da\u7d50\u69cb\u5b58\u5132\u7cfb\u7d71\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\u7531\u65bc\u5176\u9ad8\u6548\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\uff0cRedis\u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u5834\u666f\uff0c\u5305\u62ec\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u5be6\u6642\u6578\u64da\u8655\u7406\u7b49\u3002<\/p>\n<h2 id=\"%e6%90%b6%e7%b4%85%e5%8c%85%e7%9a%84%e6%8a%80%e8%a1%93%e6%8c%91%e6%88%b0-NFPamfgppS\">\u6436\u7d05\u5305\u7684\u6280\u8853\u6311\u6230<\/h2>\n<p>\u5728\u6436\u7d05\u5305\u6d3b\u52d5\u4e2d\uff0c\u901a\u5e38\u6703\u6709\u5927\u91cf\u7528\u6236\u540c\u6642\u767c\u8d77\u8acb\u6c42\uff0c\u9019\u5c0d\u65bc\u5f8c\u7aef\u7cfb\u7d71\u7684\u6027\u80fd\u63d0\u51fa\u4e86\u6975\u9ad8\u7684\u8981\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4e3b\u8981\u7684\u6280\u8853\u6311\u6230\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u4f75\u767c\u8acb\u6c42\uff1a<\/strong>\u5728\u77ed\u6642\u9593\u5167\uff0c\u7cfb\u7d71\u9700\u8981\u8655\u7406\u6210\u5343\u4e0a\u842c\u7684\u8acb\u6c42\uff0c\u9019\u5c0d\u65bc\u6578\u64da\u5eab\u7684\u8b80\u5beb\u6027\u80fd\u662f\u4e00\u500b\u5de8\u5927\u7684\u8003\u9a57\u3002<\/li>\n<li><strong>\u6578\u64da\u4e00\u81f4\u6027\uff1a<\/strong>\u5728\u6436\u7d05\u5305\u7684\u904e\u7a0b\u4e2d\uff0c\u5982\u4f55\u78ba\u4fdd\u6bcf\u500b\u7528\u6236\u7372\u5f97\u7684\u7d05\u5305\u6578\u91cf\u6b63\u78ba\u4e14\u4e0d\u91cd\u8907\uff0c\u662f\u4e00\u500b\u91cd\u8981\u7684\u554f\u984c\u3002<\/li>\n<li><strong>\u5ef6\u9072\u554f\u984c\uff1a<\/strong>\u7528\u6236\u5e0c\u671b\u5728\u6700\u77ed\u7684\u6642\u9593\u5167\u7372\u5f97\u7d50\u679c\uff0c\u4efb\u4f55\u5ef6\u9072\u90fd\u53ef\u80fd\u5f71\u97ff\u7528\u6236\u9ad4\u9a57\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e5%9c%a8%e6%90%b6%e7%b4%85%e5%8c%85%e4%b8%ad%e7%9a%84%e6%87%89%e7%94%a8-NFPamfgppS\">Redis\u5728\u6436\u7d05\u5305\u4e2d\u7684\u61c9\u7528<\/h2>\n<p>Redis\u7684\u9ad8\u6027\u80fd\u548c\u4f4e\u5ef6\u9072\u7279\u6027\uff0c\u4f7f\u5176\u6210\u70ba\u89e3\u6c7a\u4e0a\u8ff0\u6311\u6230\u7684\u7406\u60f3\u9078\u64c7\u3002\u4ee5\u4e0b\u662fRedis\u5728\u6436\u7d05\u5305\u6d3b\u52d5\u4e2d\u7684\u5177\u9ad4\u61c9\u7528\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8redis%e4%bd%9c%e7%82%ba%e7%b7%a9%e5%ad%98-NFPamfgppS\">1. \u4f7f\u7528Redis\u4f5c\u70ba\u7de9\u5b58<\/h3>\n<p>\u5728\u6436\u7d05\u5305\u6d3b\u52d5\u4e2d\uff0c\u7cfb\u7d71\u53ef\u4ee5\u5c07\u7d05\u5305\u7684\u6578\u91cf\u548c\u7528\u6236\u7684\u8acb\u6c42\u7de9\u5b58\u5728Redis\u4e2d\u3002\u9019\u6a23\u53ef\u4ee5\u5927\u5e45\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u76f4\u63a5\u8a2a\u554f\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002<\/p>\n<pre><code>\n# \u5047\u8a2d\u7d05\u5305\u6578\u91cf\u70ba100\nredis.set(\"red_packet_count\", 100)\n<\/code><\/pre>\n<h3 id=\"2-%e5%af%a6%e7%8f%be%e5%88%86%e4%bd%88%e5%bc%8f%e9%8e%96-NFPamfgppS\">2. \u5be6\u73fe\u5206\u4f48\u5f0f\u9396<\/h3>\n<p>\u70ba\u4e86\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528Redis\u7684\u5206\u4f48\u5f0f\u9396\u4f86\u63a7\u5236\u5c0d\u7d05\u5305\u6578\u91cf\u7684\u8a2a\u554f\u3002\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u591a\u500b\u7528\u6236\u540c\u6642\u6436\u5230\u540c\u4e00\u500b\u7d05\u5305\u7684\u60c5\u6cc1\u3002<\/p>\n<pre><code>\n# \u7372\u53d6\u9396\nif redis.set(\"lock\", \"locked\", nx=True, ex=5):\n    # \u57f7\u884c\u6436\u7d05\u5305\u908f\u8f2f\n    ...\n    # \u91cb\u653e\u9396\n    redis.delete(\"lock\")\n<\/code><\/pre>\n<h3 id=\"3-%e5%af%a6%e6%99%82%e7%b5%b1%e8%a8%88-NFPamfgppS\">3. \u5be6\u6642\u7d71\u8a08<\/h3>\n<p>\u5229\u7528Redis\u7684\u6578\u64da\u7d50\u69cb\uff0c\u53ef\u4ee5\u5be6\u6642\u7d71\u8a08\u6bcf\u500b\u7528\u6236\u7684\u6436\u7d05\u5305\u7d50\u679c\uff0c\u4e26\u5373\u6642\u66f4\u65b0\u7528\u6236\u754c\u9762\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<pre><code>\n# \u66f4\u65b0\u7528\u6236\u6436\u5230\u7684\u7d05\u5305\u6578\u91cf\nredis.hincrby(\"user:123\", \"red_packets\", 1)\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-NFPamfgppS\">\u7d50\u8ad6<\/h2>\n<p>\u96a8\u8457\u7dda\u4e0a\u6436\u7d05\u5305\u6d3b\u52d5\u7684\u666e\u53ca\uff0c\u5982\u4f55\u9ad8\u6548\u5730\u8655\u7406\u5927\u91cf\u8acb\u6c42\u6210\u70ba\u4e86\u6280\u8853\u4e0a\u7684\u4e00\u5927\u6311\u6230\u3002Redis\u6191\u85c9\u5176\u9ad8\u6027\u80fd\u548c\u9748\u6d3b\u6027\uff0c\u70ba\u6436\u7d05\u5305\u6d3b\u52d5\u63d0\u4f9b\u4e86\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u901a\u904e\u5408\u7406\u5730\u4f7f\u7528Redis\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u78ba\u4fdd\u7528\u6236\u5728\u53c3\u8207\u6d3b\u52d5\u6642\u7372\u5f97\u6d41\u66a2\u7684\u9ad4\u9a57\uff0c\u5f9e\u800c\u63d0\u5347\u6574\u9ad4\u7684\u6eff\u610f\u5ea6\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u5982\u4f55\u5728\u60a8\u7684\u61c9\u7528\u4e2d\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u6709\u8208\u8da3\uff0c\u6216\u8005\u60f3\u8981\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\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u79d2\u6bba\u6436\u7d05\u5305\uff0cRedis\u52a9\u529b\u8d85\u7d1a\u5feb\u6a02\u9ad4\u9a57\uff0c\u5be6\u73fe\u9ad8\u6548\u6436\u8cfc\uff0c\u8b93\u4f60\u8f15\u9b06\u4eab\u53d7\u7d05\u5305\u7684\u6a02\u8da3\u8207\u523a\u6fc0\uff01<\/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-169257","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/169257","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=169257"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/169257\/revisions"}],"predecessor-version":[{"id":169258,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/169257\/revisions\/169258"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=169257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=169257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=169257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}