{"id":158977,"date":"2024-11-03T10:25:44","date_gmt":"2024-11-03T02:25:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/158977\/"},"modified":"2024-11-03T10:25:45","modified_gmt":"2024-11-03T02:25:45","slug":"%e7%b4%85%e5%8c%85%e6%af%94%e7%89%b9%e5%b9%a3redis%e9%a9%85%e5%8b%95%e7%9a%84%e6%90%b6%e8%b3%bc%e5%88%86%e5%b9%a3%e7%b4%85%e5%8c%85%ef%bc%88redis%e6%af%94%e7%89%b9%e5%b9%a3%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/158977\/","title":{"rendered":"\u7d05\u5305\u6bd4\u7279\u5e63Redis\u9a45\u52d5\u7684\u6436\u8cfc\u5206\u5e63\u7d05\u5305\uff08redis\u6bd4\u7279\u5e63\uff09"},"content":{"rendered":"<h1 id=\"%e7%b4%85%e5%8c%85%e6%af%94%e7%89%b9%e5%b9%a3redis%e9%a9%85%e5%8b%95%e7%9a%84%e6%90%b6%e8%b3%bc%e5%88%86%e5%b9%a3%e7%b4%85%e5%8c%85%ef%bc%88redis%e6%af%94%e7%89%b9%e5%b9%a3%ef%bc%89-oeyccBxNye\">\u7d05\u5305\u6bd4\u7279\u5e63Redis\u9a45\u52d5\u7684\u6436\u8cfc\u5206\u5e63\u7d05\u5305\uff08redis\u6bd4\u7279\u5e63\uff09<\/h1>\n<p>\u96a8\u8457\u6578\u5b57\u8ca8\u5e63\u7684\u8208\u8d77\uff0c\u6bd4\u7279\u5e63\u4f5c\u70ba\u6700\u53d7\u6b61\u8fce\u7684\u52a0\u5bc6\u8ca8\u5e63\u4e4b\u4e00\uff0c\u5df2\u7d93\u6210\u70ba\u8a31\u591a\u5546\u696d\u6a21\u5f0f\u7684\u6838\u5fc3\u3002\u7279\u5225\u662f\u5728\u4e2d\u570b\u6587\u5316\u4e2d\uff0c\u7d05\u5305\u8c61\u5fb5\u8457\u795d\u798f\u548c\u8ca1\u5bcc\uff0c\u5c07\u6bd4\u7279\u5e63\u8207\u7d05\u5305\u7d50\u5408\u8d77\u4f86\uff0c\u5f62\u6210\u4e86\u4e00\u7a2e\u65b0\u7a4e\u7684\u5546\u696d\u6a21\u5f0f\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u6280\u8853\u9a45\u52d5\u7d05\u5305\u6bd4\u7279\u5e63\u7684\u6436\u8cfc\u5206\u5e63\u7d05\u5305\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u6280\u8853\u7d30\u7bc0\u548c\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e7%b4%85%e5%8c%85%e8%88%87%e6%af%94%e7%89%b9%e5%b9%a3%e7%9a%84%e7%b5%90%e5%90%88-oeyccBxNye\">\u7d05\u5305\u8207\u6bd4\u7279\u5e63\u7684\u7d50\u5408<\/h2>\n<p>\u7d05\u5305\u5728\u4e2d\u570b\u6587\u5316\u4e2d\u6709\u8457\u60a0\u4e45\u7684\u6b77\u53f2\uff0c\u901a\u5e38\u5728\u7bc0\u6176\u6216\u7279\u5225\u7684\u65e5\u5b50\u88e1\u767c\u653e\uff0c\u8c61\u5fb5\u8457\u795d\u798f\u548c\u597d\u904b\u3002\u96a8\u8457\u6578\u5b57\u8ca8\u5e63\u7684\u666e\u53ca\uff0c\u8d8a\u4f86\u8d8a\u591a\u7684\u4f01\u696d\u958b\u59cb\u63a2\u7d22\u5c07\u6bd4\u7279\u5e63\u4f5c\u70ba\u7d05\u5305\u7684\u5f62\u5f0f\u3002\u9019\u4e0d\u50c5\u80fd\u5438\u5f15\u5e74\u8f15\u6d88\u8cbb\u8005\uff0c\u9084\u80fd\u63d0\u5347\u54c1\u724c\u7684\u73fe\u4ee3\u611f\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e8%a7%92%e8%89%b2-oeyccBxNye\">Redis\u7684\u89d2\u8272<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u9700\u8981\u5feb\u901f\u8b80\u5beb\u7684\u5834\u666f\u3002\u5728\u7d05\u5305\u6bd4\u7279\u5e63\u7684\u61c9\u7528\u4e2d\uff0cRedis\u53ef\u4ee5\u7528\u4f86\u7ba1\u7406\u7d05\u5305\u7684\u767c\u653e\u3001\u6436\u8cfc\u548c\u5206\u914d\u3002\u5176\u9ad8\u6027\u80fd\u548c\u4f4e\u5ef6\u9072\u7684\u7279\u6027\u4f7f\u5f97Redis\u6210\u70ba\u8655\u7406\u5927\u91cf\u4e26\u767c\u8acb\u6c42\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h3 id=\"%e5%a6%82%e4%bd%95%e5%af%a6%e7%8f%be%e7%b4%85%e5%8c%85%e6%af%94%e7%89%b9%e5%b9%a3%e7%9a%84%e6%90%b6%e8%b3%bc%e7%b3%bb%e7%b5%b1-oeyccBxNye\">\u5982\u4f55\u5be6\u73fe\u7d05\u5305\u6bd4\u7279\u5e63\u7684\u6436\u8cfc\u7cfb\u7d71<\/h3>\n<p>\u4ee5\u4e0b\u662f\u5be6\u73fe\u7d05\u5305\u6bd4\u7279\u5e63\u6436\u8cfc\u7cfb\u7d71\u7684\u4e00\u500b\u57fa\u672c\u601d\u8def\uff1a<\/p>\n<ul>\n<li><strong>\u7d05\u5305\u751f\u6210\uff1a<\/strong>\u4f7f\u7528\u6bd4\u7279\u5e63\u751f\u6210\u7d05\u5305\uff0c\u4e26\u5c07\u5176\u5b58\u5132\u5728Redis\u4e2d\u3002<\/li>\n<li><strong>\u6436\u8cfc\u908f\u8f2f\uff1a<\/strong>\u7576\u7528\u6236\u767c\u8d77\u6436\u8cfc\u8acb\u6c42\u6642\uff0c\u7cfb\u7d71\u9700\u8981\u6aa2\u67e5\u7d05\u5305\u7684\u53ef\u7528\u6027\uff0c\u4e26\u5728Redis\u4e2d\u9032\u884c\u539f\u5b50\u64cd\u4f5c\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/li>\n<li><strong>\u5206\u914d\u7d05\u5305\uff1a<\/strong>\u6210\u529f\u6436\u8cfc\u5f8c\uff0c\u5c07\u7d05\u5305\u7684\u6bd4\u7279\u5e63\u8f49\u79fb\u5230\u7528\u6236\u7684\u8cec\u6236\u4e2d\u3002<\/li>\n<\/ul>\n<h4 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-oeyccBxNye\">\u793a\u4f8b\u4ee3\u78bc<\/h4>\n<pre><code>\nimport redis\n\n# \u9023\u63a5\u5230Redis\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u751f\u6210\u7d05\u5305\ndef create_red_packet(amount, count):\n    r.hset('red_packet', 'total_amount', amount)\n    r.hset('red_packet', 'count', count)\n\n# \u6436\u8cfc\u7d05\u5305\ndef grab_red_packet(user_id):\n    if r.hget('red_packet', 'count') &gt; 0:\n        r.hincrby('red_packet', 'count', -1)\n        # \u5047\u8a2d\u6bcf\u500b\u7d05\u5305\u7684\u91d1\u984d\u70ba\u7e3d\u91d1\u984d\u9664\u4ee5\u7d05\u5305\u6578\u91cf\n        total_amount = int(r.hget('red_packet', 'total_amount'))\n        count = int(r.hget('red_packet', 'count'))\n        amount_per_packet = total_amount \/ count\n        # \u5c07\u91d1\u984d\u8f49\u79fb\u5230\u7528\u6236\u8cec\u6236\n        transfer_to_user(user_id, amount_per_packet)\n        return True\n    return False\n<\/code><\/pre>\n<h2 id=\"%e6%8c%91%e6%88%b0%e8%88%87%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-oeyccBxNye\">\u6311\u6230\u8207\u89e3\u6c7a\u65b9\u6848<\/h2>\n<p>\u5728\u5be6\u73fe\u7d05\u5305\u6bd4\u7279\u5e63\u6436\u8cfc\u7cfb\u7d71\u7684\u904e\u7a0b\u4e2d\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u6311\u6230\uff0c\u4f8b\u5982\u9ad8\u4e26\u767c\u8acb\u6c42\u5c0e\u81f4\u7684\u6578\u64da\u7af6\u722d\u554f\u984c\u3002\u70ba\u4e86\u89e3\u6c7a\u9019\u4e9b\u554f\u984c\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528Redis\u7684\u5206\u5e03\u5f0f\u9396\u4f86\u78ba\u4fdd\u5728\u6436\u8cfc\u904e\u7a0b\u4e2d\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<h3 id=\"%e5%88%86%e5%b8%83%e5%bc%8f%e9%8e%96%e7%9a%84%e5%af%a6%e7%8f%be-oeyccBxNye\">\u5206\u5e03\u5f0f\u9396\u7684\u5be6\u73fe<\/h3>\n<p>\u4f7f\u7528Redis\u7684SETNX\u547d\u4ee4\u53ef\u4ee5\u5be6\u73fe\u7c21\u55ae\u7684\u5206\u5e03\u5f0f\u9396\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u57fa\u672c\u7684\u5be6\u73fe\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nimport time\n\ndef acquire_lock(lock_name, acquire_time=10):\n    identifier = str(uuid.uuid4())\n    end = time.time() + acquire_time\n    while time.time() &lt; end:\n        if r.set(lock_name, identifier, nx=True, ex=acquire_time):\n            return identifier\n        time.sleep(0.01)\n    return False\n\ndef release_lock(lock_name, identifier):\n    pipe = r.pipeline(True)\n    while True:\n        try:\n            pipe.watch(lock_name)\n            if pipe.get(lock_name) == identifier:\n                pipe.multi()\n                pipe.delete(lock_name)\n                pipe.execute()\n                return True\n            pipe.unwatch()\n            break\n        except redis.exceptions.WatchError:\n            pass\n    return False\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-oeyccBxNye\">\u7d50\u8ad6<\/h2>\n<p>\u7d05\u5305\u6bd4\u7279\u5e63Redis\u9a45\u52d5\u7684\u6436\u8cfc\u5206\u5e63\u7d05\u5305\u662f\u4e00\u500b\u5275\u65b0\u7684\u5546\u696d\u6a21\u5f0f\uff0c\u7d50\u5408\u4e86\u50b3\u7d71\u6587\u5316\u8207\u73fe\u4ee3\u79d1\u6280\u3002\u901a\u904e\u4f7f\u7528Redis\u4f86\u7ba1\u7406\u7d05\u5305\u7684\u767c\u653e\u548c\u6436\u8cfc\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8655\u7406\u9ad8\u4e26\u767c\u8acb\u6c42\uff0c\u78ba\u4fdd\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002\u96a8\u8457\u6578\u5b57\u8ca8\u5e63\u7684\u666e\u53ca\uff0c\u9019\u7a2e\u6a21\u5f0f\u6709\u671b\u5728\u672a\u4f86\u7372\u5f97\u66f4\u5ee3\u6cdb\u7684\u61c9\u7528\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u6709\u95dc\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u548c\u5176\u4ed6\u96f2\u670d\u52d9\u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u7d05\u5305\u6bd4\u7279\u5e63Redis\u9a45\u52d5\u7684\u6436\u8cfc\u5206\u5e63\u7d05\u5305\uff0c\u9ad4\u9a57\u5feb\u901f\u3001\u5b89\u5168\u7684\u6578\u5b57\u8ca8\u5e63\u4ea4\u6613\uff0c\u8b93\u60a8\u7684\u8cc7\u7522\u589e\u503c\u66f4\u8f15\u9b06\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-158977","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/158977","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=158977"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/158977\/revisions"}],"predecessor-version":[{"id":158978,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/158977\/revisions\/158978"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=158977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=158977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=158977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}