{"id":97508,"date":"2024-10-20T18:36:16","date_gmt":"2024-10-20T10:36:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97508\/"},"modified":"2024-10-20T18:36:17","modified_gmt":"2024-10-20T10:36:17","slug":"redis-%e6%95%b8%e6%93%9a%e5%ba%ab%e5%8f%96%e6%95%b8%e6%9e%81%e9%80%9f%e9%ab%98%e4%b8%a6%e7%99%bc%ef%bc%88redis%e9%ab%98%e4%b8%a6%e7%99%bc%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97508\/","title":{"rendered":"Redis \u6578\u64da\u5eab\u53d6\u6578\u6781\u901f\u9ad8\u4e26\u767c\uff08redis\u9ad8\u4e26\u767c\u53d6\u6578\u64da\u5eab\uff09"},"content":{"rendered":"<h1 id=\"redis-%e6%95%b8%e6%93%9a%e5%ba%ab%e5%8f%96%e6%95%b8%e6%9e%81%e9%80%9f%e9%ab%98%e4%b8%a6%e7%99%bc-PMxpKXAvRH\">Redis \u6578\u64da\u5eab\u53d6\u6578\u6781\u901f\u9ad8\u4e26\u767c<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u97ff\u61c9\u901f\u5ea6\u5c0d\u65bc\u7528\u6236\u9ad4\u9a57\u81f3\u95dc\u91cd\u8981\u3002Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u56e0\u5176\u5353\u8d8a\u7684\u6027\u80fd\u548c\u9ad8\u4e26\u767c\u8655\u7406\u80fd\u529b\u800c\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e Redis \u7684\u9ad8\u4e26\u767c\u53d6\u6578\u64da\u5eab\u7279\u6027\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-redis%ef%bc%9f-PMxpKXAvRH\">\u4ec0\u9ebc\u662f Redis\uff1f<\/h2>\n<p>Redis\uff08Remote Dictionary Server\uff09\u662f\u4e00\u500b\u958b\u6e90\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u4e3b\u8981\u7528\u65bc\u6578\u64da\u7684\u5feb\u901f\u5b58\u53d6\u3002\u5b83\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u548c\u6709\u5e8f\u96c6\u5408\u7b49\u3002\u7531\u65bc\u5176\u5728\u5167\u5b58\u4e2d\u64cd\u4f5c\u6578\u64da\uff0cRedis \u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u6975\u5feb\uff0c\u9019\u4f7f\u5f97\u5b83\u6210\u70ba\u9ad8\u4e26\u767c\u61c9\u7528\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"redis-%e7%9a%84%e9%ab%98%e4%b8%a6%e7%99%bc%e7%89%b9%e6%80%a7-PMxpKXAvRH\">Redis \u7684\u9ad8\u4e26\u767c\u7279\u6027<\/h2>\n<p>Redis \u7684\u9ad8\u4e26\u767c\u6027\u80fd\u4e3b\u8981\u6e90\u65bc\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<ul>\n<li><strong>\u55ae\u7dda\u7a0b\u67b6\u69cb\uff1a<\/strong>Redis \u4f7f\u7528\u55ae\u7dda\u7a0b\u6a21\u578b\u4f86\u8655\u7406\u8acb\u6c42\uff0c\u9019\u610f\u5473\u8457\u5b83\u4e0d\u6703\u56e0\u70ba\u4e0a\u4e0b\u6587\u5207\u63db\u800c\u5c0e\u81f4\u6027\u80fd\u640d\u5931\u3002\u9019\u7a2e\u8a2d\u8a08\u4f7f\u5f97 Redis \u80fd\u5920\u5728\u9ad8\u4e26\u767c\u74b0\u5883\u4e2d\u4fdd\u6301\u7a69\u5b9a\u7684\u6027\u80fd\u3002<\/li>\n<li><strong>\u5167\u5b58\u5b58\u5132\uff1a<\/strong>\u6240\u6709\u6578\u64da\u90fd\u5b58\u5132\u5728\u5167\u5b58\u4e2d\uff0c\u9019\u4f7f\u5f97\u6578\u64da\u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u6975\u5feb\u3002\u6839\u64da\u5b98\u65b9\u6587\u6a94\uff0cRedis \u7684\u6027\u80fd\u53ef\u4ee5\u9054\u5230\u6bcf\u79d2\u6578\u5341\u842c\u6b21\u7684\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u975e\u963b\u585e I\/O\uff1a<\/strong>Redis \u4f7f\u7528\u975e\u963b\u585e I\/O \u6a21\u578b\uff0c\u9019\u4f7f\u5f97\u5b83\u80fd\u5920\u540c\u6642\u8655\u7406\u591a\u500b\u5ba2\u6236\u7aef\u7684\u8acb\u6c42\uff0c\u800c\u4e0d\u6703\u56e0\u70ba\u67d0\u4e00\u500b\u8acb\u6c42\u7684\u5ef6\u9072\u800c\u5f71\u97ff\u5176\u4ed6\u8acb\u6c42\u7684\u8655\u7406\u3002<\/li>\n<\/ul>\n<h2 id=\"%e9%ab%98%e4%b8%a6%e7%99%bc%e5%8f%96%e6%95%b8%e7%9a%84%e5%af%a6%e7%8f%be-PMxpKXAvRH\">\u9ad8\u4e26\u767c\u53d6\u6578\u7684\u5be6\u73fe<\/h2>\n<p>\u5728\u9ad8\u4e26\u767c\u74b0\u5883\u4e2d\uff0c\u5982\u4f55\u6709\u6548\u5730\u5f9e Redis \u4e2d\u53d6\u6578\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5be6\u8e10\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8%e7%ae%a1%e9%81%93%e6%8a%80%e8%a1%93-PMxpKXAvRH\">1. \u4f7f\u7528\u7ba1\u9053\u6280\u8853<\/h3>\n<p>\u7ba1\u9053\u6280\u8853\u5141\u8a31\u5ba2\u6236\u7aef\u5728\u4e00\u6b21\u8acb\u6c42\u4e2d\u767c\u9001\u591a\u500b\u547d\u4ee4\uff0c\u5f9e\u800c\u6e1b\u5c11\u7db2\u7d61\u5ef6\u9072\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 Redis \u7684\u7ba1\u9053\u6280\u8853\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230 Redis\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u4f7f\u7528\u7ba1\u9053\npipe = r.pipeline()\nfor i in range(1000):\n    pipe.get(f'key:{i}')\nresults = pipe.execute()\n<\/code><\/pre>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8%e9%9b%86%e7%be%a4%e6%a8%a1%e5%bc%8f-PMxpKXAvRH\">2. \u4f7f\u7528\u96c6\u7fa4\u6a21\u5f0f<\/h3>\n<p>Redis \u652f\u6301\u96c6\u7fa4\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5c07\u6578\u64da\u5206\u6563\u5230\u591a\u500b\u7bc0\u9ede\u4e0a\uff0c\u5f9e\u800c\u63d0\u9ad8\u4e26\u767c\u8655\u7406\u80fd\u529b\u3002\u9019\u6a23\u53ef\u4ee5\u6709\u6548\u5730\u5206\u64d4\u8b80\u53d6\u548c\u5beb\u5165\u7684\u8ca0\u8f09\u3002<\/p>\n<h3 id=\"3-%e8%a8%ad%e7%bd%ae%e5%90%88%e7%90%86%e7%9a%84%e9%81%8e%e6%9c%9f%e6%99%82%e9%96%93-PMxpKXAvRH\">3. \u8a2d\u7f6e\u5408\u7406\u7684\u904e\u671f\u6642\u9593<\/h3>\n<p>\u5c0d\u65bc\u4e0d\u518d\u9700\u8981\u7684\u6578\u64da\uff0c\u8a2d\u7f6e\u904e\u671f\u6642\u9593\u53ef\u4ee5\u91cb\u653e\u5167\u5b58\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002\u4f7f\u7528 <code>EXPIRE<\/code> \u547d\u4ee4\u53ef\u4ee5\u8f15\u9b06\u5be6\u73fe\uff1a<\/p>\n<pre><code>r.set('key', 'value')\nr.expire('key', 60)  # 60\u79d2\u5f8c\u904e\u671f\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-PMxpKXAvRH\">\u7d50\u8ad6<\/h2>\n<p>Redis \u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u5177\u5099\u4e86\u512a\u79c0\u7684\u9ad8\u4e26\u767c\u8655\u7406\u80fd\u529b\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u8a08\u548c\u6700\u4f73\u5be6\u8e10\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5145\u5206\u767c\u63ee Redis \u7684\u6027\u80fd\uff0c\u6eff\u8db3\u9ad8\u4e26\u767c\u61c9\u7528\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u4f7f\u7528\u7ba1\u9053\u6280\u8853\u3001\u96c6\u7fa4\u6a21\u5f0f\uff0c\u9084\u662f\u8a2d\u7f6e\u5408\u7406\u7684\u904e\u671f\u6642\u9593\uff0c\u9019\u4e9b\u65b9\u6cd5\u90fd\u80fd\u6709\u6548\u63d0\u5347\u6578\u64da\u7684\u53d6\u7528\u6548\u7387\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6027\u80fd\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u7684\u670d\u52d9\u5c07\u662f\u60a8\u7684\u7406\u60f3\u9078\u64c7\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\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Redis\u6578\u64da\u5eab\u7684\u9ad8\u4e26\u767c\u53d6\u6578\u89e3\u6c7a\u65b9\u6848\uff0c\u5be6\u73fe\u5feb\u901f\u6578\u64da\u5b58\u53d6\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u7528\u6236\u9ad4\u9a57\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-97508","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97508","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=97508"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97508\/revisions"}],"predecessor-version":[{"id":97509,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97508\/revisions\/97509"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}