{"id":202257,"date":"2025-05-10T14:10:57","date_gmt":"2025-05-10T06:10:57","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202257\/"},"modified":"2025-05-10T14:10:57","modified_gmt":"2025-05-10T06:10:57","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8redis%e5%ae%9e%e7%8e%b0%e5%88%86%e5%b8%83%e5%bc%8f%e6%95%b0%e6%8d%ae%e4%b8%80%e8%87%b4%e6%80%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202257\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u6570\u636e\u4e00\u81f4\u6027"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u6570\u636e\u4e00\u81f4\u6027<\/h1>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u6570\u636e\u4e00\u81f4\u6027\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p>\u5982\u4f55\u4f7f\u7528Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u6570\u636e\u4e00\u81f4\u6027<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u968f\u7740\u4e92\u8054\u7f51\u7684\u5feb\u901f\u53d1\u5c55\uff0c\u5206\u5e03\u5f0f\u7cfb\u7edf\u5df2\u6210\u4e3a\u8bb8\u591a\u4f01\u4e1a\u7684\u9996\u9009\u67b6\u6784\u3002\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u6570\u636e\u7684\u4e00\u81f4\u6027\u662f\u975e\u5e38\u5173\u952e\u7684\u3002Redis\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u6027\u80fd\u3001\u53ef\u6269\u5c55\u7684\u952e\u503c\u5b58\u50a8\u7cfb\u7edf\uff0c\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u4e0b\u9762\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u6570\u636e\u4e00\u81f4\u6027\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u7406\u89e3\u6570\u636e\u4e00\u81f4\u6027<br \/>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u6570\u636e\u4e00\u81f4\u6027\u662f\u6307\u7cfb\u7edf\u4e2d\u7684\u6240\u6709\u8282\u70b9\u5728\u540c\u4e00\u65f6\u523b\u770b\u5230\u7684\u6570\u636e\u662f\u4e00\u6837\u7684\u3002\u5e38\u89c1\u7684\u6570\u636e\u4e00\u81f4\u6027\u95ee\u9898\u5305\u62ec\uff1a\u6570\u636e\u4e22\u5931\u3001\u8bfb\u5199\u51b2\u7a81\u3001\u810f\u8bfb\u7b49\u3002\u4e3a\u4e86\u4fdd\u8bc1\u6570\u636e\u4e00\u81f4\u6027\uff0c\u53ef\u4ee5\u91c7\u7528\u5404\u79cd\u6570\u636e\u540c\u6b65\u3001\u6570\u636e\u590d\u5236\u548c\u8c03\u5ea6\u7b97\u6cd5\u7b49\u6280\u672f\u3002<\/p>\n<p>\u4e8c\u3001\u4f7f\u7528Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u6570\u636e\u4e00\u81f4\u6027<\/p>\n<ol>\n<li>\u5206\u5e03\u5f0f\u9501<br \/>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528\u5206\u5e03\u5f0f\u9501\u53ef\u4ee5\u4fdd\u8bc1\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u4ece\u800c\u907f\u514d\u4e86\u6570\u636e\u51b2\u7a81\u3002Redis\u63d0\u4f9b\u4e86setnx\u548csetex\u7b49\u547d\u4ee4\u6765\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\uff0c\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre>import redis\nimport time\n\ndef acquire_lock(redis_conn, lock_name, expire_time=10):\n    lock = None\n    try:\n        while not lock:\n            lock = redis_conn.setnx(lock_name, int(time.time()) + expire_time)\n            if lock:\n                redis_conn.expire(lock_name, expire_time)\n        return True\n    except Exception as e:\n        return False\n\ndef release_lock(redis_conn, lock_name):\n    redis_conn.delete(lock_name)\n\n# \u4f7f\u7528\u793a\u4f8b\nredis_conn = redis.Redis(host='localhost', port=6379, db=0)\nlock_name = 'my_lock'\nacquired = acquire_lock(redis_conn, lock_name)\nif acquired:\n    try:\n        # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n    finally:\n        release_lock(redis_conn, lock_name)<\/pre>\n<ol>\n<li>\u53d1\u5e03\/\u8ba2\u9605\u6a21\u5f0f<br \/>Redis\u7684\u53d1\u5e03\/\u8ba2\u9605\u6a21\u5f0f\u5141\u8bb8\u591a\u4e2a\u5ba2\u6237\u7aef\u901a\u8fc7\u8ba2\u9605\u9891\u9053\u6765\u63a5\u6536\u53d1\u5e03\u8005\u53d1\u9001\u7684\u6d88\u606f\u3002\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u53ef\u4ee5\u5229\u7528\u53d1\u5e03\/\u8ba2\u9605\u6a21\u5f0f\u5b9e\u73b0\u6570\u636e\u540c\u6b65\uff0c\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre>import redis\n\ndef publish_data(redis_conn, channel, data):\n    redis_conn.publish(channel, data)\n\ndef subscribe_data(redis_conn, channel):\n    pubsub = redis_conn.pubsub()\n    pubsub.subscribe(channel)\n    for message in pubsub.listen():\n        # \u5904\u7406\u63a5\u6536\u5230\u7684\u6570\u636e\n        print(message)\n\n# \u4f7f\u7528\u793a\u4f8b\nredis_conn = redis.Redis(host='localhost', port=6379, db=0)\nchannel = 'data_sync'\ndata = 'hello-world'\npublish_data(redis_conn, channel, data)\nsubscribe_data(redis_conn, channel)<\/pre>\n<p>\u4e09\u3001\u603b\u7ed3<br \/>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u6570\u636e\u4e00\u81f4\u6027\uff0c\u5e76\u63d0\u4f9b\u4e86\u5206\u5e03\u5f0f\u9501\u548c\u53d1\u5e03\/\u8ba2\u9605\u6a21\u5f0f\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u5f53\u7136\uff0cRedis\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u89e3\u51b3\u65b9\u6848\u6765\u4fdd\u8bc1\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u6570\u636e\u4e00\u81f4\u6027\uff0c\u5982\u4e8b\u52a1\u3001\u4e50\u89c2\u9501\u3001\u5206\u5e03\u5f0f\u961f\u5217\u7b49\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6848\u3002<\/p>\n<p>\u6700\u540e\uff0c\u8981\u6ce8\u610f\u7684\u662f\uff0c\u867d\u7136Redis\u63d0\u4f9b\u4e86\u4e00\u4e9b\u673a\u5236\u6765\u5b9e\u73b0\u5206\u5e03\u5f0f\u6570\u636e\u4e00\u81f4\u6027\uff0c\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u8fd8\u9700\u8981\u8003\u8651\u7f51\u7edc\u5ef6\u8fdf\u3001\u6545\u969c\u6062\u590d\u7b49\u56e0\u7d20\uff0c\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u7684\u53ef\u9760\u6027\u548c\u6027\u80fd\u3002\u56e0\u6b64\uff0c\u5728\u8bbe\u8ba1\u548c\u5b9e\u65bd\u5206\u5e03\u5f0f\u7cfb\u7edf\u65f6\uff0c\u9700\u8981\u7efc\u5408\u8003\u8651\u591a\u4e2a\u56e0\u7d20\u6765\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Redis\u5982\u4f55\u5b9e\u73b0\u5206\u5e03\u5f0f\u7f13\u5b58\u4e00\u81f4\u6027\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u4f7f\u7528Memcache\u4f18\u5316\u4f60\u7684PHP\u5e94\u7528\u4e2d\u7684\u6570\u636e\u5e93\u8bbf\u95ee\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u6570\u636e&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-202257","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202257","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202257"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202257\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}