{"id":134569,"date":"2024-10-31T01:15:21","date_gmt":"2024-10-30T17:15:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/134569\/"},"modified":"2024-10-31T01:15:21","modified_gmt":"2024-10-30T17:15:21","slug":"redis%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e6%94%b9%e8%ae%8a%e5%82%b3%e7%b5%b1%e5%ad%98%e5%84%b2%e6%96%b9%e5%bc%8f%ef%bc%88%e5%85%88%e5%af%abredis%e5%be%8c%e5%af%ab%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/134569\/","title":{"rendered":"Redis\u8207\u6578\u64da\u5eab\u6539\u8b8a\u50b3\u7d71\u5b58\u5132\u65b9\u5f0f\uff08\u5148\u5bebredis\u5f8c\u5beb\u6578\u64da\u5eab\uff09"},"content":{"rendered":"<h1 id=\"redis%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e6%94%b9%e8%ae%8a%e5%82%b3%e7%b5%b1%e5%ad%98%e5%84%b2%e6%96%b9%e5%bc%8f-rMOozlHqgI\">Redis\u8207\u6578\u64da\u5eab\u6539\u8b8a\u50b3\u7d71\u5b58\u5132\u65b9\u5f0f<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5b58\u5132\u548c\u7ba1\u7406\u7684\u65b9\u5f0f\u6b63\u5728\u8fc5\u901f\u6f14\u8b8a\u3002\u96a8\u8457\u61c9\u7528\u7a0b\u5e8f\u9700\u6c42\u7684\u589e\u52a0\uff0c\u50b3\u7d71\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u9762\u81e8\u8457\u6027\u80fd\u548c\u64f4\u5c55\u6027\u7684\u6311\u6230\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u6578\u64da\u7d50\u69cb\u5b58\u5132\u7cfb\u7d71\uff0c\u6b63\u5728\u6539\u8b8a\u6211\u5011\u5c0d\u6578\u64da\u5b58\u5132\u7684\u7406\u89e3\u548c\u4f7f\u7528\u65b9\u5f0f\u3002\u672c\u6587\u5c07\u63a2\u8a0eRedis\u7684\u7279\u6027\u53ca\u5176\u5982\u4f55\u8207\u50b3\u7d71\u6578\u64da\u5eab\u76f8\u8f14\u76f8\u6210\uff0c\u9032\u800c\u6539\u8b8a\u6211\u5011\u7684\u5b58\u5132\u65b9\u5f0f\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e7%89%b9%e6%80%a7-rMOozlHqgI\">Redis\u7684\u7279\u6027<\/h2>\n<p>Redis\u662f\u4e00\u500b\u958b\u6e90\u7684\u5167\u5b58\u6578\u64da\u7d50\u69cb\u5b58\u5132\u7cfb\u7d71\uff0c\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\u5176\u4e3b\u8981\u7279\u9ede\u5305\u62ec\uff1a<\/p>\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\u8b80\u5beb\u8acb\u6c42\uff0c\u9019\u4f7f\u5f97\u5b83\u975e\u5e38\u9069\u5408\u9700\u8981\u5feb\u901f\u97ff\u61c9\u7684\u61c9\u7528\u5834\u666f\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\uff1a<\/strong>\u96d6\u7136Redis\u4e3b\u8981\u662f\u5167\u5b58\u5b58\u5132\uff0c\u4f46\u5b83\u652f\u6301\u5c07\u6578\u64da\u6301\u4e45\u5316\u5230\u78c1\u789f\uff0c\u9019\u6a23\u5373\u4f7f\u5728\u7cfb\u7d71\u91cd\u555f\u5f8c\u4e5f\u80fd\u6062\u5fa9\u6578\u64da\u3002<\/li>\n<li><strong>\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u6700\u5408\u9069\u7684\u5b58\u5132\u65b9\u5f0f\u3002<\/li>\n<li><strong>\u5206\u4f48\u5f0f\u652f\u6301\uff1a<\/strong>Redis Cluster\u5141\u8a31\u7528\u6236\u5728\u591a\u500b\u7bc0\u9ede\u4e4b\u9593\u5206\u914d\u6578\u64da\uff0c\u5f9e\u800c\u5be6\u73fe\u9ad8\u53ef\u7528\u6027\u548c\u64f4\u5c55\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e7%9a%84%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af-rMOozlHqgI\">Redis\u7684\u61c9\u7528\u5834\u666f<\/h2>\n<p>Redis\u7684\u9ad8\u6027\u80fd\u548c\u9748\u6d3b\u6027\u4f7f\u5176\u5728\u591a\u7a2e\u61c9\u7528\u5834\u666f\u4e2d\u5f97\u5230\u4e86\u5ee3\u6cdb\u61c9\u7528\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u7de9\u5b58\u7cfb\u7d71\uff1a<\/strong>Redis\u5e38\u7528\u4f5c\u7de9\u5b58\u5c64\uff0c\u4ee5\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8acb\u6c42\uff0c\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u97ff\u61c9\u901f\u5ea6\u3002<\/li>\n<li><strong>\u5be6\u6642\u6578\u64da\u5206\u6790\uff1a<\/strong>\u7531\u65bc\u5176\u5feb\u901f\u7684\u6578\u64da\u8b80\u53d6\u80fd\u529b\uff0cRedis\u975e\u5e38\u9069\u5408\u7528\u65bc\u5be6\u6642\u6578\u64da\u5206\u6790\u548c\u5831\u544a\u3002<\/li>\n<li><strong>\u6d88\u606f\u968a\u5217\uff1a<\/strong>Redis\u7684\u5217\u8868\u548c\u767c\u5e03\/\u8a02\u95b1\u529f\u80fd\u4f7f\u5176\u6210\u70ba\u4e00\u500b\u6709\u6548\u7684\u6d88\u606f\u968a\u5217\u7cfb\u7d71\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%82%b3%e7%b5%b1%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%8c%91%e6%88%b0-rMOozlHqgI\">\u50b3\u7d71\u6578\u64da\u5eab\u7684\u6311\u6230<\/h2>\n<p>\u50b3\u7d71\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff08\u5982MySQL\u3001PostgreSQL\u7b49\uff09\u5728\u6578\u64da\u5b58\u5132\u548c\u7ba1\u7406\u65b9\u9762\u6709\u8457\u60a0\u4e45\u7684\u6b77\u53f2\uff0c\u4f46\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u9577\u548c\u61c9\u7528\u9700\u6c42\u7684\u8b8a\u5316\uff0c\u5b83\u5011\u4e5f\u9762\u81e8\u8457\u4e00\u4e9b\u6311\u6230\uff1a<\/p>\n<ul>\n<li><strong>\u6027\u80fd\u74f6\u9838\uff1a<\/strong>\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u52a0\uff0c\u50b3\u7d71\u6578\u64da\u5eab\u5728\u67e5\u8a62\u548c\u5beb\u5165\u64cd\u4f5c\u4e0a\u7684\u6027\u80fd\u53ef\u80fd\u6703\u4e0b\u964d\u3002<\/li>\n<li><strong>\u64f4\u5c55\u6027\u554f\u984c\uff1a<\/strong>\u50b3\u7d71\u6578\u64da\u5eab\u901a\u5e38\u96e3\u4ee5\u6a6b\u5411\u64f4\u5c55\uff0c\u9019\u4f7f\u5f97\u5728\u9ad8\u6d41\u91cf\u74b0\u5883\u4e2d\u7dad\u6301\u6027\u80fd\u8b8a\u5f97\u56f0\u96e3\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\u4e0d\u8db3\uff1a<\/strong>\u5728\u9762\u5c0d\u591a\u6a23\u5316\u7684\u6578\u64da\u9700\u6c42\u6642\uff0c\u50b3\u7d71\u6578\u64da\u5eab\u7684\u7d50\u69cb\u5316\u6578\u64da\u6a21\u578b\u53ef\u80fd\u7121\u6cd5\u6eff\u8db3\u9700\u6c42\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e8%88%87%e5%82%b3%e7%b5%b1%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e7%b5%90%e5%90%88-rMOozlHqgI\">Redis\u8207\u50b3\u7d71\u6578\u64da\u5eab\u7684\u7d50\u5408<\/h2>\n<p>\u5118\u7ba1Redis\u548c\u50b3\u7d71\u6578\u64da\u5eab\u5404\u6709\u512a\u7f3a\u9ede\uff0c\u4f46\u5b83\u5011\u4e26\u4e0d\u662f\u76f8\u4e92\u6392\u65a5\u7684\u3002\u5be6\u969b\u4e0a\uff0c\u8a31\u591a\u73fe\u4ee3\u61c9\u7528\u7a0b\u5e8f\u9078\u64c7\u5c07\u5169\u8005\u7d50\u5408\u4f7f\u7528\uff0c\u4ee5\u767c\u63ee\u5404\u81ea\u7684\u512a\u52e2\u3002\u4f8b\u5982\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528Redis\u4f5c\u70ba\u7de9\u5b58\u5c64\uff0c\u4f86\u52a0\u901f\u5c0d\u5f8c\u7aef\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7684\u67e5\u8a62\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002<\/p>\n<pre><code>\n# \u4f7f\u7528Redis\u4f5c\u70ba\u7de9\u5b58\u7684\u793a\u4f8b\nimport redis\n\n# \u9023\u63a5\u5230Redis\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u67e5\u8a62\u6578\u64da\ndef get_data(key):\n    # \u9996\u5148\u6aa2\u67e5\u7de9\u5b58\n    cached_data = r.get(key)\n    if cached_data:\n        return cached_data\n    else:\n        # \u5982\u679c\u7de9\u5b58\u4e2d\u6c92\u6709\uff0c\u5247\u67e5\u8a62\u6578\u64da\u5eab\n        data = query_database(key)\n        # \u5c07\u6578\u64da\u5b58\u5165\u7de9\u5b58\n        r.set(key, data)\n        return data\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-rMOozlHqgI\">\u7d50\u8ad6<\/h2>\n<p>Redis\u548c\u50b3\u7d71\u6578\u64da\u5eab\u7684\u7d50\u5408\u70ba\u6578\u64da\u5b58\u5132\u548c\u7ba1\u7406\u63d0\u4f9b\u4e86\u65b0\u7684\u601d\u8def\u3002\u901a\u904e\u5229\u7528Redis\u7684\u9ad8\u6027\u80fd\u548c\u9748\u6d3b\u6027\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u6c7a\u50b3\u7d71\u6578\u64da\u5eab\u9762\u81e8\u7684\u6311\u6230\u3002\u96a8\u8457\u6280\u8853\u7684\u9032\u6b65\uff0c\u672a\u4f86\u7684\u6578\u64da\u5b58\u5132\u65b9\u5f0f\u5c07\u66f4\u52a0\u591a\u6a23\u5316\uff0c\u4e26\u4e14\u80fd\u5920\u66f4\u597d\u5730\u6eff\u8db3\u4e0d\u65b7\u8b8a\u5316\u7684\u696d\u52d9\u9700\u6c42\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u6709\u95dc<code><a href=\"https:\/\/server.hk\">VPS<\/a><\/code>\u548c<code><a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a><\/code>\u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u8207\u6578\u64da\u5eab\u9769\u65b0\u50b3\u7d71\u5b58\u5132\u65b9\u5f0f\uff0c\u63d0\u4f9b\u9ad8\u6548\u80fd\u3001\u4f4e\u5ef6\u9072\u7684\u6578\u64da\u8655\u7406\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-134569","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/134569","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=134569"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/134569\/revisions"}],"predecessor-version":[{"id":134570,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/134569\/revisions\/134570"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=134569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=134569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=134569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}