{"id":120904,"date":"2024-10-25T10:24:20","date_gmt":"2024-10-25T02:24:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/120904\/"},"modified":"2024-10-25T10:24:20","modified_gmt":"2024-10-25T02:24:20","slug":"redis-%e5%bf%ab%e5%8f%96%e6%94%af%e6%8c%81%e8%a9%95%e8%ab%96%e6%95%b8%e6%93%9a%e5%bf%ab%e9%80%9f%e9%9f%bf%e6%87%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/120904\/","title":{"rendered":"Redis \u5feb\u53d6\u652f\u6301\u8a55\u8ad6\u6578\u64da\u5feb\u901f\u97ff\u61c9"},"content":{"rendered":"<h1 id=\"redis-%e5%bf%ab%e5%8f%96%e6%94%af%e6%8c%81%e8%a9%95%e8%ab%96%e6%95%b8%e6%93%9a%e5%bf%ab%e9%80%9f%e9%9f%bf%e6%87%89-bJwaeUveNp\">Redis \u5feb\u53d6\u652f\u6301\u8a55\u8ad6\u6578\u64da\u5feb\u901f\u97ff\u61c9<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u7db2\u7ad9\u7684\u6027\u80fd\u548c\u7528\u6236\u9ad4\u9a57\u81f3\u95dc\u91cd\u8981\u3002\u7279\u5225\u662f\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u7528\u6236\u8a55\u8ad6\u7684\u7db2\u7ad9\uff0c\u5982\u4f55\u5feb\u901f\u97ff\u61c9\u7528\u6236\u8acb\u6c42\u6210\u70ba\u4e86\u4e00\u500b\u6311\u6230\u3002Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u8a55\u8ad6\u6578\u64da\u7684\u97ff\u61c9\u901f\u5ea6\uff0c\u5f9e\u800c\u6539\u5584\u6574\u9ad4\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-redis%ef%bc%9f-bJwaeUveNp\">\u4ec0\u9ebc\u662f Redis\uff1f<\/h2>\n<p>Redis \u662f\u4e00\u7a2e\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\u7b49\u3002\u7531\u65bc\u5176\u9ad8\u6548\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\uff0cRedis \u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u5feb\u53d6\u3001\u6d88\u606f\u968a\u5217\u548c\u5be6\u6642\u6578\u64da\u5206\u6790\u7b49\u5834\u666f\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87-redis-%e4%bd%9c%e7%82%ba%e5%bf%ab%e5%8f%96%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88%ef%bc%9f-bJwaeUveNp\">\u70ba\u4ec0\u9ebc\u9078\u64c7 Redis \u4f5c\u70ba\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff1f<\/h2>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis \u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u901a\u5e38\u5728\u6beb\u79d2\u7d1a\u5225\uff0c\u9019\u4f7f\u5f97\u5b83\u975e\u5e38\u9069\u5408\u7528\u65bc\u9700\u8981\u5feb\u901f\u97ff\u61c9\u7684\u61c9\u7528\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis \u4e0d\u50c5\u652f\u6301\u7c21\u55ae\u7684\u9375\u503c\u5c0d\uff0c\u9084\u652f\u6301\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\uff0c\u9019\u4f7f\u5f97\u5b83\u80fd\u5920\u9748\u6d3b\u5730\u8655\u7406\u5404\u7a2e\u6578\u64da\u9700\u6c42\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>\u96d6\u7136 Redis \u662f\u4e00\u500b\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4f46\u5b83\u4e5f\u63d0\u4f9b\u4e86\u6301\u4e45\u5316\u9078\u9805\uff0c\u53ef\u4ee5\u5c07\u6578\u64da\u4fdd\u5b58\u5230\u78c1\u789f\u4e2d\uff0c\u9632\u6b62\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-redis-%e5%bf%ab%e5%8f%96%e8%a9%95%e8%ab%96%e6%95%b8%e6%93%9a-bJwaeUveNp\">\u5982\u4f55\u4f7f\u7528 Redis \u5feb\u53d6\u8a55\u8ad6\u6578\u64da<\/h2>\n<p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u4f7f\u7528 Redis \u5feb\u53d6\u8a55\u8ad6\u6578\u64da\u7684\u904e\u7a0b\u53ef\u4ee5\u5206\u70ba\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9d-redis-bJwaeUveNp\">1. \u5b89\u88dd Redis<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5728\u4f3a\u670d\u5668\u4e0a\u5b89\u88dd Redis\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5728 Ubuntu \u7cfb\u7d71\u4e0a\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install redis-server<\/code><\/pre>\n<h3 id=\"2-%e9%85%8d%e7%bd%ae-redis-bJwaeUveNp\">2. \u914d\u7f6e Redis<\/h3>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u901a\u904e\u7de8\u8f2f\u914d\u7f6e\u6587\u4ef6\u4f86\u8abf\u6574 Redis \u7684\u8a2d\u7f6e\u3002\u914d\u7f6e\u6587\u4ef6\u901a\u5e38\u4f4d\u65bc \/etc\/redis\/redis.conf\u3002\u53ef\u4ee5\u6839\u64da\u9700\u8981\u8abf\u6574\u5167\u5b58\u9650\u5236\u3001\u6301\u4e45\u5316\u9078\u9805\u7b49\u3002<\/p>\n<h3 id=\"3-%e5%bf%ab%e5%8f%96%e8%a9%95%e8%ab%96%e6%95%b8%e6%93%9a-bJwaeUveNp\">3. \u5feb\u53d6\u8a55\u8ad6\u6578\u64da<\/h3>\n<p>\u5728\u61c9\u7528\u4e2d\uff0c\u53ef\u4ee5\u5c07\u8a55\u8ad6\u6578\u64da\u5b58\u5132\u5230 Redis \u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5c07\u8a55\u8ad6\u6578\u64da\u5b58\u5132\u5230 Redis\uff1a<\/p>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230 Redis\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u5047\u8a2d\u6709\u4e00\u500b\u8a55\u8ad6\u5217\u8868\ncomments = [\n    {\"id\": 1, \"content\": \"\u9019\u662f\u4e00\u689d\u8a55\u8ad6\"},\n    {\"id\": 2, \"content\": \"\u9019\u662f\u53e6\u4e00\u689d\u8a55\u8ad6\"}\n]\n\n# \u5c07\u8a55\u8ad6\u5b58\u5132\u5230 Redis\nfor comment in comments:\n    r.hset(f\"comment:{comment['id']}\", mapping=comment)<\/code><\/pre>\n<h3 id=\"4-%e7%8d%b2%e5%8f%96%e5%bf%ab%e5%8f%96%e7%9a%84%e8%a9%95%e8%ab%96%e6%95%b8%e6%93%9a-bJwaeUveNp\">4. \u7372\u53d6\u5feb\u53d6\u7684\u8a55\u8ad6\u6578\u64da<\/h3>\n<p>\u7576\u7528\u6236\u8acb\u6c42\u8a55\u8ad6\u6642\uff0c\u53ef\u4ee5\u9996\u5148\u6aa2\u67e5 Redis \u4e2d\u662f\u5426\u5b58\u5728\u8a72\u6578\u64da\uff0c\u5982\u679c\u5b58\u5728\u5247\u76f4\u63a5\u8fd4\u56de\uff0c\u5426\u5247\u5f9e\u6578\u64da\u5eab\u4e2d\u67e5\u8a62\u4e26\u5b58\u5132\u5230 Redis \u4e2d\uff1a<\/p>\n<pre><code>def get_comment(comment_id):\n    # \u5617\u8a66\u5f9e Redis \u7372\u53d6\u8a55\u8ad6\n    comment = r.hgetall(f\"comment:{comment_id}\")\n    if comment:\n        return comment\n    else:\n        # \u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5f9e\u6578\u64da\u5eab\u4e2d\u67e5\u8a62\n        comment = query_database(comment_id)\n        # \u4e26\u5c07\u5176\u5b58\u5132\u5230 Redis\n        r.hset(f\"comment:{comment_id}\", mapping=comment)\n        return comment<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-bJwaeUveNp\">\u7d50\u8ad6<\/h2>\n<p>\u4f7f\u7528 Redis \u5feb\u53d6\u8a55\u8ad6\u6578\u64da\u4e0d\u50c5\u80fd\u5920\u63d0\u9ad8\u97ff\u61c9\u901f\u5ea6\uff0c\u9084\u80fd\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u58d3\u529b\u3002\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0c\u9019\u7a2e\u65b9\u6cd5\u5c07\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u901a\u904e\u5408\u7406\u914d\u7f6e\u548c\u4f7f\u7528 Redis\uff0c\u7db2\u7ad9\u53ef\u4ee5\u5728\u9ad8\u6d41\u91cf\u60c5\u6cc1\u4e0b\u4fdd\u6301\u826f\u597d\u7684\u6027\u80fd\uff0c\u5f9e\u800c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-bJwaeUveNp\">\u7e3d\u7d50<\/h2>\n<p>Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u8a55\u8ad6\u6578\u64da\u7684\u97ff\u61c9\u901f\u5ea6\u3002\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u7528\u6236\u8a55\u8ad6\u7684\u7db2\u7ad9\uff0c\u9078\u64c7\u5408\u9069\u7684\u5feb\u53d6\u6280\u8853\u81f3\u95dc\u91cd\u8981\u3002\u82e5\u60a8\u5c0d\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u6216\u5176\u4ed6\u4f3a\u670d\u5668\u89e3\u6c7a\u65b9\u6848\u611f\u8208\u8da3\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis \u5feb\u53d6\u63d0\u5347\u8a55\u8ad6\u6578\u64da\u97ff\u61c9\u901f\u5ea6\uff0c\u78ba\u4fdd\u7528\u6236\u7372\u5f97\u5373\u6642\u66f4\u65b0\u548c\u6d41\u66a2\u9ad4\u9a57\uff0c\u512a\u5316\u7db2\u7ad9\u6027\u80fd\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-120904","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/120904","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=120904"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/120904\/revisions"}],"predecessor-version":[{"id":120905,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/120904\/revisions\/120905"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=120904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=120904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=120904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}