{"id":165963,"date":"2024-11-05T14:19:23","date_gmt":"2024-11-05T06:19:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/165963\/"},"modified":"2024-11-05T14:19:24","modified_gmt":"2024-11-05T06:19:24","slug":"%e5%bf%ab%e9%80%9f%e9%ab%98%e6%95%88%e7%9a%84redis%e7%b7%9a%e7%a8%8b%e9%80%9a%e4%bf%a1%e6%96%b9%e5%bc%8f%ef%bc%88redis%e7%b7%9a%e7%a8%8b%e9%80%9a%e4%bf%a1%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/165963\/","title":{"rendered":"\u5feb\u901f\u9ad8\u6548\u7684Redis\u7dda\u7a0b\u901a\u4fe1\u65b9\u5f0f\uff08redis\u7dda\u7a0b\u901a\u4fe1\uff09"},"content":{"rendered":"<h1 id=\"%e5%bf%ab%e9%80%9f%e9%ab%98%e6%95%88%e7%9a%84redis%e7%b7%9a%e7%a8%8b%e9%80%9a%e4%bf%a1%e6%96%b9%e5%bc%8f%ef%bc%88redis%e7%b7%9a%e7%a8%8b%e9%80%9a%e4%bf%a1%ef%bc%89-udXtGZIhgC\">\u5feb\u901f\u9ad8\u6548\u7684Redis\u7dda\u7a0b\u901a\u4fe1\u65b9\u5f0f\uff08redis\u7dda\u7a0b\u901a\u4fe1\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\uff0c\u6027\u80fd\u548c\u6548\u7387\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u5834\u666f\u4e2d\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u5feb\u901f\u6578\u64da\u5b58\u53d6\u7684\u60c5\u6cc1\u4e0b\u3002\u672c\u6587\u5c07\u63a2\u8a0eRedis\u7684\u7dda\u7a0b\u901a\u4fe1\u65b9\u5f0f\uff0c\u4e26\u4ecb\u7d39\u5982\u4f55\u5229\u7528\u9019\u4e9b\u65b9\u5f0f\u4f86\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%e7%b7%9a%e7%a8%8b%e9%80%9a%e4%bf%a1%ef%bc%9f-udXtGZIhgC\">\u4ec0\u9ebc\u662fRedis\u7dda\u7a0b\u901a\u4fe1\uff1f<\/h2>\n<p>Redis\u7dda\u7a0b\u901a\u4fe1\u662f\u6307\u5728\u591a\u7dda\u7a0b\u74b0\u5883\u4e2d\uff0c\u5982\u4f55\u6709\u6548\u5730\u4f7f\u7528Redis\u4f86\u9032\u884c\u6578\u64da\u7684\u8b80\u53d6\u548c\u5beb\u5165\u3002\u7531\u65bcRedis\u662f\u55ae\u7dda\u7a0b\u7684\uff0c\u9019\u610f\u5473\u8457\u5b83\u5728\u8655\u7406\u8acb\u6c42\u6642\u4e0d\u6703\u540c\u6642\u57f7\u884c\u591a\u500b\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u958b\u767c\u8005\u9700\u8981\u8003\u616e\u5982\u4f55\u5728\u591a\u7dda\u7a0b\u61c9\u7528\u4e2d\u6709\u6548\u5730\u8207Redis\u9032\u884c\u4ea4\u4e92\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e5%9f%ba%e6%9c%ac%e7%89%b9%e6%80%a7-udXtGZIhgC\">Redis\u7684\u57fa\u672c\u7279\u6027<\/h2>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u80fd\u5920\u4ee5\u6bcf\u79d2\u6578\u5341\u842c\u6b21\u7684\u901f\u5ea6\u8655\u7406\u8acb\u6c42\uff0c\u9019\u4f7f\u5f97\u5b83\u6210\u70ba\u9ad8\u6027\u80fd\u61c9\u7528\u7684\u7406\u60f3\u9078\u64c7\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\uff1a<\/strong>Redis\u652f\u6301\u591a\u7a2e\u6301\u4e45\u5316\u65b9\u5f0f\uff0c\u5305\u62ecRDB\u5feb\u7167\u548cAOF\u65e5\u8a8c\uff0c\u9019\u4f7f\u5f97\u6578\u64da\u5728\u91cd\u555f\u5f8c\u4ecd\u7136\u53ef\u7528\u3002<\/li>\n<li><strong>\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis\u652f\u6301\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u9019\u4f7f\u5f97\u5b83\u5728\u4e0d\u540c\u5834\u666f\u4e0b\u90fd\u80fd\u9748\u6d3b\u61c9\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b7%9a%e7%a8%8b%e9%80%9a%e4%bf%a1%e7%9a%84%e6%8c%91%e6%88%b0-udXtGZIhgC\">\u7dda\u7a0b\u901a\u4fe1\u7684\u6311\u6230<\/h2>\n<p>\u5728\u591a\u7dda\u7a0b\u74b0\u5883\u4e2d\uff0c\u4f7f\u7528Redis\u9032\u884c\u901a\u4fe1\u6642\uff0c\u958b\u767c\u8005\u9762\u81e8\u5e7e\u500b\u6311\u6230\uff1a<\/p>\n<ul>\n<li><strong>\u7af6\u722d\u689d\u4ef6\uff1a<\/strong>\u591a\u500b\u7dda\u7a0b\u540c\u6642\u8a2a\u554fRedis\u53ef\u80fd\u5c0e\u81f4\u6578\u64da\u4e0d\u4e00\u81f4\u7684\u554f\u984c\u3002<\/li>\n<li><strong>\u9023\u63a5\u7ba1\u7406\uff1a<\/strong>\u6bcf\u500b\u7dda\u7a0b\u90fd\u9700\u8981\u7ba1\u7406\u81ea\u5df1\u7684Redis\u9023\u63a5\uff0c\u9019\u53ef\u80fd\u5c0e\u81f4\u8cc7\u6e90\u6d6a\u8cbb\u3002<\/li>\n<li><strong>\u6027\u80fd\u74f6\u9838\uff1a<\/strong>\u5982\u679c\u4e0d\u5408\u7406\u5730\u4f7f\u7528Redis\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u6027\u80fd\u4e0b\u964d\u3002<\/li>\n<\/ul>\n<h2 id=\"%e9%ab%98%e6%95%88%e7%9a%84redis%e7%b7%9a%e7%a8%8b%e9%80%9a%e4%bf%a1%e6%96%b9%e5%bc%8f-udXtGZIhgC\">\u9ad8\u6548\u7684Redis\u7dda\u7a0b\u901a\u4fe1\u65b9\u5f0f<\/h2>\n<p>\u70ba\u4e86\u514b\u670d\u4e0a\u8ff0\u6311\u6230\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u63a1\u53d6\u4ee5\u4e0b\u5e7e\u7a2e\u9ad8\u6548\u7684Redis\u7dda\u7a0b\u901a\u4fe1\u65b9\u5f0f\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8%e9%80%a3%e6%8e%a5%e6%b1%a0-udXtGZIhgC\">1. \u4f7f\u7528\u9023\u63a5\u6c60<\/h3>\n<p>\u9023\u63a5\u6c60\u662f\u4e00\u7a2e\u7ba1\u7406Redis\u9023\u63a5\u7684\u6280\u8853\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u6e1b\u5c11\u9023\u63a5\u7684\u958b\u92b7\u3002\u901a\u904e\u4f7f\u7528\u9023\u63a5\u6c60\uff0c\u6bcf\u500b\u7dda\u7a0b\u53ef\u4ee5\u5f9e\u6c60\u4e2d\u7372\u53d6\u4e00\u500b\u53ef\u7528\u7684\u9023\u63a5\uff0c\u4f7f\u7528\u5b8c\u7562\u5f8c\u518d\u5c07\u5176\u6b78\u9084\u3002\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u6bcf\u6b21\u8acb\u6c42\u90fd\u5275\u5efa\u65b0\u9023\u63a5\u7684\u958b\u92b7\u3002<\/p>\n<pre><code>import redis\nfrom redis import ConnectionPool\n\n# \u5275\u5efa\u9023\u63a5\u6c60\npool = ConnectionPool(host='localhost', port=6379, db=0)\n\n# \u7372\u53d6\u9023\u63a5\nr = redis.Redis(connection_pool=pool)\nr.set('key', 'value')\nprint(r.get('key'))\n<\/code><\/pre>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8%e6%b6%88%e6%81%af%e9%9a%8a%e5%88%97-udXtGZIhgC\">2. \u4f7f\u7528\u6d88\u606f\u968a\u5217<\/h3>\n<p>\u5728\u591a\u7dda\u7a0b\u74b0\u5883\u4e2d\uff0c\u4f7f\u7528\u6d88\u606f\u968a\u5217\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u8026\u7dda\u7a0b\u4e4b\u9593\u7684\u901a\u4fe1\u3002\u958b\u767c\u8005\u53ef\u4ee5\u5c07\u9700\u8981\u8655\u7406\u7684\u4efb\u52d9\u653e\u5165Redis\u7684\u5217\u8868\u4e2d\uff0c\u7136\u5f8c\u7531\u5de5\u4f5c\u7dda\u7a0b\u5f9e\u5217\u8868\u4e2d\u53d6\u51fa\u4efb\u52d9\u9032\u884c\u8655\u7406\u3002\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u7dda\u7a0b\u4e4b\u9593\u7684\u76f4\u63a5\u4ea4\u4e92\uff0c\u63d0\u9ad8\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u64f4\u5c55\u6027\u3002<\/p>\n<pre><code>import redis\n\nr = redis.Redis()\n\n# \u5c07\u4efb\u52d9\u653e\u5165\u968a\u5217\nr.lpush('task_queue', 'task1')\n\n# \u7372\u53d6\u4efb\u52d9\ntask = r.rpop('task_queue')\nprint(task)\n<\/code><\/pre>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8redis%e7%9a%84%e7%99%bc%e5%b8%83-%e8%a8%82%e9%96%b1%e6%a8%a1%e5%bc%8f-udXtGZIhgC\">3. \u4f7f\u7528Redis\u7684\u767c\u5e03\/\u8a02\u95b1\u6a21\u5f0f<\/h3>\n<p>Redis\u7684\u767c\u5e03\/\u8a02\u95b1\u6a21\u5f0f\u5141\u8a31\u7dda\u7a0b\u4e4b\u9593\u9032\u884c\u975e\u540c\u6b65\u901a\u4fe1\u3002\u7576\u4e00\u500b\u7dda\u7a0b\u767c\u4f48\u6d88\u606f\u6642\uff0c\u6240\u6709\u8a02\u95b1\u8a72\u6d88\u606f\u7684\u7dda\u7a0b\u90fd\u6703\u6536\u5230\u901a\u77e5\u3002\u9019\u7a2e\u65b9\u5f0f\u9069\u5408\u65bc\u9700\u8981\u5373\u6642\u66f4\u65b0\u7684\u5834\u666f\uff0c\u5982\u804a\u5929\u61c9\u7528\u6216\u5373\u6642\u901a\u77e5\u7cfb\u7d71\u3002<\/p>\n<pre><code>import redis\n\nr = redis.Redis()\n\n# \u8a02\u95b1\u983b\u9053\npubsub = r.pubsub()\npubsub.subscribe('channel')\n\n# \u767c\u4f48\u6d88\u606f\nr.publish('channel', 'Hello, World!')\n\n# \u63a5\u6536\u6d88\u606f\nfor message in pubsub.listen():\n    print(message)\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-udXtGZIhgC\">\u7e3d\u7d50<\/h2>\n<p>Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u5728\u591a\u7dda\u7a0b\u74b0\u5883\u4e2d\u63d0\u4f9b\u4e86\u591a\u7a2e\u901a\u4fe1\u65b9\u5f0f\u3002\u901a\u904e\u4f7f\u7528\u9023\u63a5\u6c60\u3001\u6d88\u606f\u968a\u5217\u548c\u767c\u5e03\/\u8a02\u95b1\u6a21\u5f0f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u7dda\u7a0b\u4e4b\u9593\u7684\u901a\u4fe1\uff0c\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684Redis\u7dda\u7a0b\u901a\u4fe1\u65b9\u5f0f\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u53ef\u9760\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5feb\u901f\u9ad8\u6548\u7684Redis\u7dda\u7a0b\u901a\u4fe1\u65b9\u5f0f\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\uff0c\u5be6\u73fe\u9ad8\u6548\u6578\u64da\u8655\u7406\u8207\u5be6\u6642\u97ff\u61c9\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-165963","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/165963","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=165963"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/165963\/revisions"}],"predecessor-version":[{"id":165964,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/165963\/revisions\/165964"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=165963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=165963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=165963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}