{"id":194867,"date":"2024-11-13T10:14:24","date_gmt":"2024-11-13T02:14:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/194867\/"},"modified":"2024-11-13T10:14:25","modified_gmt":"2024-11-13T02:14:25","slug":"%e7%94%a8redis%e5%af%a6%e7%8f%be%e7%b2%be%e5%bd%a9%e7%b4%9b%e5%91%88%e7%9a%84%e7%b4%85%e5%8c%85%e7%a6%8f%e5%88%a9%ef%bc%88redis%e7%b4%85%e5%8c%85%e5%af%a6%e7%8f%be%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/194867\/","title":{"rendered":"\u7528Redis\u5be6\u73fe\u7cbe\u5f69\u7d1b\u5448\u7684\u7d05\u5305\u798f\u5229\uff08redis\u7d05\u5305\u5be6\u73fe\uff09"},"content":{"rendered":"<h1 id=\"%e7%94%a8redis%e5%af%a6%e7%8f%be%e7%b2%be%e5%bd%a9%e7%b4%9b%e5%91%88%e7%9a%84%e7%b4%85%e5%8c%85%e7%a6%8f%e5%88%a9%ef%bc%88redis%e7%b4%85%e5%8c%85%e5%af%a6%e7%8f%be%ef%bc%89-aHDCGyjoZO\">\u7528Redis\u5be6\u73fe\u7cbe\u5f69\u7d1b\u5448\u7684\u7d05\u5305\u798f\u5229\uff08redis\u7d05\u5305\u5be6\u73fe\uff09<\/h1>\n<p>\u5728\u4e2d\u570b\u6587\u5316\u4e2d\uff0c\u7d05\u5305\u662f\u4e00\u7a2e\u50b3\u7d71\u7684\u795d\u798f\u65b9\u5f0f\uff0c\u901a\u5e38\u5728\u7bc0\u6176\u6216\u7279\u5225\u7684\u5834\u5408\u4e2d\u767c\u653e\u3002\u96a8\u8457\u79d1\u6280\u7684\u9032\u6b65\uff0c\u7d05\u5305\u7684\u5f62\u5f0f\u4e5f\u9010\u6f38\u5f9e\u5be6\u9ad4\u8f49\u5411\u6578\u5b57\u5316\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u6578\u64da\u5eab\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u8f15\u9b06\u5be6\u73fe\u7d05\u5305\u529f\u80fd\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u4f86\u5be6\u73fe\u7d05\u5305\u798f\u5229\u7684\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"redis%e7%b0%a1%e4%bb%8b-aHDCGyjoZO\">Redis\u7c21\u4ecb<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u958b\u6e90\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u56e0\u5176\u9ad8\u6548\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\u800c\u53d7\u5230\u5ee3\u6cdb\u4f7f\u7528\u3002\u5b83\u652f\u6301\u591a\u7a2e\u6578\u64da\u985e\u578b\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\uff0c\u4e26\u4e14\u5177\u5099\u6301\u4e45\u5316\u529f\u80fd\uff0c\u80fd\u5920\u5728\u91cd\u555f\u5f8c\u6062\u5fa9\u6578\u64da\u3002\u9019\u4e9b\u7279\u6027\u4f7f\u5f97Redis\u6210\u70ba\u5be6\u73fe\u7d05\u5305\u529f\u80fd\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"%e7%b4%85%e5%8c%85%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-aHDCGyjoZO\">\u7d05\u5305\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u7d05\u5305\u7684\u57fa\u672c\u6982\u5ff5\u662f\u5c07\u4e00\u7b46\u91d1\u984d\u5206\u6210\u591a\u500b\u5c0f\u984d\uff0c\u7136\u5f8c\u96a8\u6a5f\u767c\u653e\u7d66\u7528\u6236\u3002\u9019\u7a2e\u96a8\u6a5f\u6027\u548c\u4e0d\u78ba\u5b9a\u6027\u589e\u52a0\u4e86\u7528\u6236\u7684\u53c3\u8207\u611f\u548c\u8208\u8da3\u3002\u5be6\u73fe\u7d05\u5305\u529f\u80fd\u7684\u57fa\u672c\u6b65\u9a5f\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u8a2d\u5b9a\u7d05\u5305\u7684\u7e3d\u91d1\u984d\u548c\u6578\u91cf\u3002<\/li>\n<li>\u5c07\u7e3d\u91d1\u984d\u5206\u914d\u5230\u6bcf\u500b\u7d05\u5305\u4e2d\u3002<\/li>\n<li>\u5c07\u7d05\u5305\u4fe1\u606f\u5b58\u5132\u5230Redis\u4e2d\u3002<\/li>\n<li>\u7528\u6236\u8acb\u6c42\u7d05\u5305\u6642\uff0c\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u7d05\u5305\u4e26\u8fd4\u56de\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%94%a8redis%e5%af%a6%e7%8f%be%e7%b4%85%e5%8c%85%e5%8a%9f%e8%83%bd%e7%9a%84%e6%ad%a5%e9%a9%9f-aHDCGyjoZO\">\u7528Redis\u5be6\u73fe\u7d05\u5305\u529f\u80fd\u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e8%a8%ad%e5%ae%9a%e7%b4%85%e5%8c%85%e5%8f%83%e6%95%b8-aHDCGyjoZO\">1. \u8a2d\u5b9a\u7d05\u5305\u53c3\u6578<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u8a2d\u5b9a\u7d05\u5305\u7684\u7e3d\u91d1\u984d\u548c\u6578\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>totalAmount = 1000  # \u7e3d\u91d1\u984d\ntotalCount = 10      # \u7d05\u5305\u6578\u91cf\n<\/code><\/pre>\n<h3 id=\"2-%e9%9a%a8%e6%a9%9f%e5%88%86%e9%85%8d%e7%b4%85%e5%8c%85%e9%87%91%e9%a1%8d-aHDCGyjoZO\">2. \u96a8\u6a5f\u5206\u914d\u7d05\u5305\u91d1\u984d<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5c07\u7e3d\u91d1\u984d\u96a8\u6a5f\u5206\u914d\u5230\u6bcf\u500b\u7d05\u5305\u4e2d\u3002\u9019\u88e1\u53ef\u4ee5\u4f7f\u7528\u4e00\u500b\u7c21\u55ae\u7684\u7b97\u6cd5\u4f86\u5be6\u73fe\uff1a<\/p>\n<pre><code>\nimport random\n\ndef distribute_red_packets(total_amount, total_count):\n    if total_count &lt;= 0 or total_amount &lt;= 0:\n        return []\n    \n    packets = []\n    for i in range(total_count - 1):\n        amount = random.randint(1, total_amount \/\/ (total_count - i))\n        packets.append(amount)\n        total_amount -= amount\n    \n    packets.append(total_amount)  # \u5c07\u5269\u9918\u91d1\u984d\u653e\u5165\u6700\u5f8c\u4e00\u500b\u7d05\u5305\n    random.shuffle(packets)  # \u96a8\u6a5f\u6253\u4e82\u7d05\u5305\u9806\u5e8f\n    return packets\n\nred_packets = distribute_red_packets(totalAmount, totalCount)\n<\/code><\/pre>\n<h3 id=\"3-%e5%ad%98%e5%84%b2%e7%b4%85%e5%8c%85%e4%bf%a1%e6%81%af%e5%88%b0redis-aHDCGyjoZO\">3. \u5b58\u5132\u7d05\u5305\u4fe1\u606f\u5230Redis<\/h3>\n<p>\u5c07\u751f\u6210\u7684\u7d05\u5305\u4fe1\u606f\u5b58\u5132\u5230Redis\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6216\u54c8\u5e0c\u4f86\u5b58\u5132\u7d05\u5305\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Redis\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nimport redis\n\n# \u9023\u63a5\u5230Redis\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u5c07\u7d05\u5305\u5b58\u5132\u5230Redis\nfor index, amount in enumerate(red_packets):\n    r.lpush('red_packets', amount)  # \u4f7f\u7528\u5217\u8868\u5b58\u5132\u7d05\u5305\u91d1\u984d\n<\/code><\/pre>\n<h3 id=\"4-%e7%94%a8%e6%88%b6%e8%ab%8b%e6%b1%82%e7%b4%85%e5%8c%85-aHDCGyjoZO\">4. \u7528\u6236\u8acb\u6c42\u7d05\u5305<\/h3>\n<p>\u7576\u7528\u6236\u8acb\u6c42\u7d05\u5305\u6642\uff0c\u6211\u5011\u53ef\u4ee5\u5f9eRedis\u4e2d\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u7d05\u5305\uff1a<\/p>\n<pre><code>\ndef grab_red_packet():\n    amount = r.rpop('red_packets')  # \u5f9e\u5217\u8868\u4e2d\u96a8\u6a5f\u53d6\u51fa\u4e00\u500b\u7d05\u5305\n    return amount\n\nuser_packet = grab_red_packet()\nif user_packet:\n    print(f\"\u606d\u559c\u4f60\u7372\u5f97\u4e86\u7d05\u5305\uff1a{user_packet}\u5143\")\nelse:\n    print(\"\u7d05\u5305\u5df2\u7d93\u9818\u5b8c\u4e86\uff01\")\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-aHDCGyjoZO\">\u7e3d\u7d50<\/h2>\n<p>\u5229\u7528Redis\u5be6\u73fe\u7d05\u5305\u529f\u80fd\u4e0d\u50c5\u80fd\u5920\u63d0\u5347\u7528\u6236\u7684\u53c3\u8207\u611f\uff0c\u9084\u80fd\u5920\u6709\u6548\u7ba1\u7406\u7d05\u5305\u7684\u767c\u653e\u904e\u7a0b\u3002\u901a\u904e\u7c21\u55ae\u7684\u4ee3\u78bc\u793a\u4f8b\uff0c\u6211\u5011\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u8a2d\u5b9a\u7d05\u5305\u53c3\u6578\u3001\u96a8\u6a5f\u5206\u914d\u91d1\u984d\u3001\u5b58\u5132\u6578\u64da\u4ee5\u53ca\u7528\u6236\u8acb\u6c42\u7d05\u5305\u7684\u6574\u500b\u904e\u7a0b\u3002\u9019\u4e9b\u6280\u8853\u4e0d\u50c5\u9069\u7528\u65bc\u7bc0\u6176\u6d3b\u52d5\uff0c\u4e5f\u53ef\u4ee5\u61c9\u7528\u65bc\u5404\u7a2e\u4fc3\u92b7\u6d3b\u52d5\u4e2d\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u5be6\u73fe\u5404\u7a2e\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528Redis\u5be6\u73fe\u5275\u65b0\u7684\u7d05\u5305\u798f\u5229\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\uff0c\u589e\u5f37\u4e92\u52d5\u6027\uff0c\u8b93\u6bcf\u500b\u7d05\u5305\u90fd\u5145\u6eff\u9a5a\u559c\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-194867","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194867","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=194867"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194867\/revisions"}],"predecessor-version":[{"id":194868,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194867\/revisions\/194868"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=194867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=194867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=194867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}