{"id":197643,"date":"2024-11-14T09:30:55","date_gmt":"2024-11-14T01:30:55","guid":{"rendered":"https:\/\/server.hk\/cnblog\/197643\/"},"modified":"2024-11-14T09:30:56","modified_gmt":"2024-11-14T01:30:56","slug":"%e5%88%a9%e7%94%a8redis%e7%b7%9a%e7%a8%8b%e5%ae%89%e5%85%a8%e6%80%a7%e9%8e%96%e5%af%a6%e7%8f%be%e5%a2%9e%e9%87%8f%e6%9b%b4%e6%96%b0%e6%a1%88%e4%be%8b%ef%bc%88redis%e7%b7%9a%e7%a8%8b%e5%ae%89%e5%85%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/197643\/","title":{"rendered":"\u5229\u7528Redis\u7dda\u7a0b\u5b89\u5168\u6027\u9396\u5be6\u73fe\u589e\u91cf\u66f4\u65b0\u6848\u4f8b\uff08redis\u7dda\u7a0b\u5b89\u5168\u6848\u4f8b\uff09"},"content":{"rendered":"<h1 id=\"%e5%88%a9%e7%94%a8redis%e7%b7%9a%e7%a8%8b%e5%ae%89%e5%85%a8%e6%80%a7%e9%8e%96%e5%af%a6%e7%8f%be%e5%a2%9e%e9%87%8f%e6%9b%b4%e6%96%b0%e6%a1%88%e4%be%8b%ef%bc%88redis%e7%b7%9a%e7%a8%8b%e5%ae%89%e5%85%a8-yksCWhfNpR\">\u5229\u7528Redis\u7dda\u7a0b\u5b89\u5168\u6027\u9396\u5be6\u73fe\u589e\u91cf\u66f4\u65b0\u6848\u4f8b\uff08redis\u7dda\u7a0b\u5b89\u5168\u6848\u4f8b\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u5206\u4f48\u5f0f\u7cfb\u7d71\u4e2d\uff0c\u6578\u64da\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u6a5f\u5236\u4f86\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u6027\u548c\u4e00\u81f4\u6027\u3002\u5176\u4e2d\uff0c\u7dda\u7a0b\u5b89\u5168\u6027\u9396\uff08Mutex\uff09\u662f\u4e00\u500b\u5e38\u7528\u7684\u6280\u8853\uff0c\u80fd\u5920\u6709\u6548\u5730\u9632\u6b62\u591a\u500b\u7dda\u7a0b\u540c\u6642\u5c0d\u540c\u4e00\u8cc7\u6e90\u9032\u884c\u64cd\u4f5c\uff0c\u5f9e\u800c\u5be6\u73fe\u589e\u91cf\u66f4\u65b0\u7684\u9700\u6c42\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e7%b7%9a%e7%a8%8b%e5%ae%89%e5%85%a8%e6%80%a7%e9%8e%96%ef%bc%9f-yksCWhfNpR\">\u4ec0\u9ebc\u662f\u7dda\u7a0b\u5b89\u5168\u6027\u9396\uff1f<\/h2>\n<p>\u7dda\u7a0b\u5b89\u5168\u6027\u9396\u662f\u4e00\u7a2e\u7528\u65bc\u63a7\u5236\u5c0d\u5171\u4eab\u8cc7\u6e90\u8a2a\u554f\u7684\u6a5f\u5236\u3002\u7576\u4e00\u500b\u7dda\u7a0b\u7372\u5f97\u9396\u6642\uff0c\u5176\u4ed6\u7dda\u7a0b\u5fc5\u9808\u7b49\u5f85\uff0c\u76f4\u5230\u8a72\u9396\u88ab\u91cb\u653e\u3002\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u6578\u64da\u7af6\u722d\u548c\u4e0d\u4e00\u81f4\u7684\u60c5\u6cc1\u767c\u751f\u3002\u5728Redis\u4e2d\uff0c\u5e38\u7528\u7684\u9396\u5be6\u73fe\u65b9\u5f0f\u6709SETNX\u547d\u4ee4\u548cRedisson\u7b49\u5eab\u3002<\/p>\n<h2 id=\"redis%e9%8e%96%e7%9a%84%e5%9f%ba%e6%9c%ac%e5%af%a6%e7%8f%be-yksCWhfNpR\">Redis\u9396\u7684\u57fa\u672c\u5be6\u73fe<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Redis\u5be6\u73fe\u7dda\u7a0b\u5b89\u5168\u6027\u9396\u7684\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code>import redis\nimport time\n\n# \u9023\u63a5\u5230Redis\nr = redis.StrictRedis(host='localhost', port=6379, db=0)\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.001)  # \u7b49\u5f85\u4e00\u6bb5\u6642\u9593\u518d\u91cd\u8a66\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            lock_value = pipe.get(lock_name)\n            if lock_value and lock_value.decode('utf-8') == identifier:\n                pipe.multi()\n                pipe.delete(lock_name)\n                pipe.execute()\n                return True\n            pipe.unwatch()\n            break\n        except redis.WatchError:\n            continue\n    return False\n<\/code><\/pre>\n<h2 id=\"%e5%a2%9e%e9%87%8f%e6%9b%b4%e6%96%b0%e7%9a%84%e6%a1%88%e4%be%8b-yksCWhfNpR\">\u589e\u91cf\u66f4\u65b0\u7684\u6848\u4f8b<\/h2>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u7528\u6236\u7684\u8a2a\u554f\u8a08\u6578\u5668\uff0c\u6211\u5011\u5e0c\u671b\u5728\u591a\u500b\u7dda\u7a0b\u4e2d\u5b89\u5168\u5730\u589e\u91cf\u66f4\u65b0\u9019\u500b\u8a08\u6578\u5668\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u589e\u91cf\u66f4\u65b0\u793a\u4f8b\uff1a<\/p>\n<pre><code>def increment_counter(user_id):\n    lock_name = f'lock:counter:{user_id}'\n    identifier = acquire_lock(lock_name)\n    if identifier:\n        try:\n            # \u7372\u53d6\u7576\u524d\u8a08\u6578\n            current_count = r.get(f'counter:{user_id}')\n            if current_count is None:\n                current_count = 0\n            else:\n                current_count = int(current_count)\n\n            # \u589e\u52a0\u8a08\u6578\n            new_count = current_count + 1\n            r.set(f'counter:{user_id}', new_count)\n        finally:\n            release_lock(lock_name, identifier)\n    else:\n        print(\"\u7121\u6cd5\u7372\u53d6\u9396\uff0c\u8acb\u7a0d\u5f8c\u518d\u8a66\u3002\")\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-yksCWhfNpR\">\u7e3d\u7d50<\/h2>\n<p>\u5229\u7528Redis\u7684\u7dda\u7a0b\u5b89\u5168\u6027\u9396\u53ef\u4ee5\u6709\u6548\u5730\u5be6\u73fe\u589e\u91cf\u66f4\u65b0\uff0c\u78ba\u4fdd\u5728\u591a\u7dda\u7a0b\u74b0\u5883\u4e0b\u6578\u64da\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002\u901a\u904e\u4e0a\u8ff0\u793a\u4f8b\uff0c\u6211\u5011\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u4f7f\u7528Redis\u7684\u9396\u6a5f\u5236\u4f86\u5b89\u5168\u5730\u66f4\u65b0\u7528\u6236\u7684\u8a2a\u554f\u8a08\u6578\u5668\u3002\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u63d0\u9ad8\u4e86\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\uff0c\u9084\u80fd\u5920\u6709\u6548\u5730\u9632\u6b62\u6578\u64da\u7af6\u722d\u554f\u984c\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u7ba1\u7406\u60a8\u7684\u6578\u64da\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u61c9\u7528\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u548c\u512a\u5316\u7684\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5229\u7528Redis\u7684\u7dda\u7a0b\u5b89\u5168\u6027\u9396\u5be6\u73fe\u589e\u91cf\u66f4\u65b0\uff0c\u63d0\u5347\u6578\u64da\u4e00\u81f4\u6027\u8207\u6027\u80fd\u7684\u5be6\u7528\u6848\u4f8b\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-197643","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/197643","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=197643"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/197643\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=197643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=197643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=197643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}