{"id":197639,"date":"2024-11-14T09:28:54","date_gmt":"2024-11-14T01:28:54","guid":{"rendered":"https:\/\/server.hk\/cnblog\/197639\/"},"modified":"2024-11-14T09:28:55","modified_gmt":"2024-11-14T01:28:55","slug":"%e5%88%a9%e7%94%a8redis%e7%aa%81%e7%a0%b4%e6%90%9c%e7%b4%a2%e7%93%b6%e9%a0%b8%ef%bc%88redis%e8%a7%a3%e6%b1%ba%e6%90%9c%e7%b4%a2%e5%95%8f%e9%a1%8c%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/197639\/","title":{"rendered":"\u5229\u7528Redis\u7a81\u7834\u641c\u7d22\u74f6\u9838\uff08redis\u89e3\u6c7a\u641c\u7d22\u554f\u984c\uff09"},"content":{"rendered":"<h1 id=\"%e5%88%a9%e7%94%a8redis%e7%aa%81%e7%a0%b4%e6%90%9c%e7%b4%a2%e7%93%b6%e9%a0%b8%ef%bc%88redis%e8%a7%a3%e6%b1%ba%e6%90%9c%e7%b4%a2%e5%95%8f%e9%a1%8c%ef%bc%89-EvGmVgwRSQ\">\u5229\u7528Redis\u7a81\u7834\u641c\u7d22\u74f6\u9838\uff08Redis\u89e3\u6c7a\u641c\u7d22\u554f\u984c\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u5feb\u901f\u800c\u6e96\u78ba\u7684\u641c\u7d22\u529f\u80fd\u5c0d\u65bc\u4efb\u4f55\u61c9\u7528\u7a0b\u5e8f\u6216\u7db2\u7ad9\u7684\u6210\u529f\u81f3\u95dc\u91cd\u8981\u3002\u96a8\u8457\u6578\u64da\u91cf\u7684\u6fc0\u589e\uff0c\u50b3\u7d71\u7684\u641c\u7d22\u89e3\u6c7a\u65b9\u6848\u5f80\u5f80\u7121\u6cd5\u6eff\u8db3\u7528\u6236\u7684\u9700\u6c42\uff0c\u9019\u6642\u5019\uff0cRedis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u6578\u64da\u7d50\u69cb\u5b58\u5132\u7cfb\u7d71\uff0c\u4fbf\u6210\u70ba\u4e86\u89e3\u6c7a\u641c\u7d22\u74f6\u9838\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"redis%e7%b0%a1%e4%bb%8b-EvGmVgwRSQ\">Redis\u7c21\u4ecb<\/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\u548c\u6709\u5e8f\u96c6\u5408\u7b49\u3002\u7531\u65bc\u5176\u9ad8\u6027\u80fd\u548c\u9748\u6d3b\u6027\uff0cRedis\u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u5be6\u6642\u6578\u64da\u5206\u6790\u7b49\u5834\u666f\u3002\u5176\u5feb\u901f\u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u4f7f\u5f97Redis\u6210\u70ba\u89e3\u6c7a\u641c\u7d22\u554f\u984c\u7684\u6709\u6548\u5de5\u5177\u3002<\/p>\n<h2 id=\"%e6%90%9c%e7%b4%a2%e7%93%b6%e9%a0%b8%e7%9a%84%e6%88%90%e5%9b%a0-EvGmVgwRSQ\">\u641c\u7d22\u74f6\u9838\u7684\u6210\u56e0<\/h2>\n<p>\u641c\u7d22\u74f6\u9838\u901a\u5e38\u6e90\u65bc\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<ul>\n<li><strong>\u6578\u64da\u91cf\u9f90\u5927\uff1a<\/strong>\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u52a0\uff0c\u50b3\u7d71\u7684\u641c\u7d22\u5f15\u64ce\u53ef\u80fd\u7121\u6cd5\u5feb\u901f\u6aa2\u7d22\u6240\u9700\u7684\u4fe1\u606f\u3002<\/li>\n<li><strong>\u67e5\u8a62\u6548\u7387\u4f4e\uff1a<\/strong>\u8907\u96dc\u7684\u67e5\u8a62\u53ef\u80fd\u5c0e\u81f4\u6027\u80fd\u4e0b\u964d\uff0c\u7279\u5225\u662f\u5728\u9ad8\u4e26\u767c\u7684\u60c5\u6cc1\u4e0b\u3002<\/li>\n<li><strong>\u7d22\u5f15\u66f4\u65b0\u5ef6\u9072\uff1a<\/strong>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u7d22\u5f15\u7684\u66f4\u65b0\u53ef\u80fd\u7121\u6cd5\u53ca\u6642\u53cd\u6620\u6700\u65b0\u7684\u6578\u64da\uff0c\u5f71\u97ff\u641c\u7d22\u7d50\u679c\u7684\u6e96\u78ba\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e5%a6%82%e4%bd%95%e8%a7%a3%e6%b1%ba%e6%90%9c%e7%b4%a2%e5%95%8f%e9%a1%8c-EvGmVgwRSQ\">Redis\u5982\u4f55\u89e3\u6c7a\u641c\u7d22\u554f\u984c<\/h2>\n<p>Redis\u901a\u904e\u5176\u9ad8\u6548\u7684\u6578\u64da\u7d50\u69cb\u548c\u64cd\u4f5c\uff0c\u80fd\u5920\u6709\u6548\u5730\u89e3\u6c7a\u4e0a\u8ff0\u641c\u7d22\u74f6\u9838\u3002\u4ee5\u4e0b\u662f\u5e7e\u7a2e\u5e38\u898b\u7684\u61c9\u7528\u5834\u666f\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8redis%e4%bd%9c%e7%82%ba%e7%b7%a9%e5%ad%98%e5%b1%a4-EvGmVgwRSQ\">1. \u4f7f\u7528Redis\u4f5c\u70ba\u7de9\u5b58\u5c64<\/h3>\n<p>\u5c07\u641c\u7d22\u7d50\u679c\u7de9\u5b58\u5728Redis\u4e2d\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u67e5\u8a62\u901f\u5ea6\u3002\u7576\u7528\u6236\u767c\u51fa\u641c\u7d22\u8acb\u6c42\u6642\uff0c\u7cfb\u7d71\u9996\u5148\u6aa2\u67e5Redis\u4e2d\u662f\u5426\u5b58\u5728\u76f8\u61c9\u7684\u7d50\u679c\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\uff0c\u5426\u5247\u518d\u67e5\u8a62\u5f8c\u7aef\u6578\u64da\u5eab\u4e26\u5c07\u7d50\u679c\u7de9\u5b58\u5230Redis\u4e2d\u3002<\/p>\n<pre><code>\n# \u5047\u8a2d\u4f7f\u7528Python\u7684Redis\u5eab\nimport redis\n\n# \u9023\u63a5\u5230Redis\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u5b9a\u7fa9\u641c\u7d22\u51fd\u6578\ndef search(query):\n    # \u6aa2\u67e5\u7de9\u5b58\n    cached_result = r.get(query)\n    if cached_result:\n        return cached_result\n    else:\n        # \u5047\u8a2d\u5f9e\u6578\u64da\u5eab\u7372\u53d6\u7d50\u679c\n        result = fetch_from_database(query)\n        # \u5c07\u7d50\u679c\u7de9\u5b58\u5230Redis\n        r.set(query, result)\n        return result\n<\/code><\/pre>\n<h3 id=\"2-%e5%88%a9%e7%94%a8redis%e7%9a%84%e6%95%b8%e6%93%9a%e7%b5%90%e6%a7%8b%e9%80%b2%e8%a1%8c%e9%ab%98%e6%95%88%e6%9f%a5%e8%a9%a2-EvGmVgwRSQ\">2. \u5229\u7528Redis\u7684\u6578\u64da\u7d50\u69cb\u9032\u884c\u9ad8\u6548\u67e5\u8a62<\/h3>\n<p>Redis\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u53ef\u4ee5\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u7d50\u69cb\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u6709\u5e8f\u96c6\u5408\u4f86\u5b58\u5132\u641c\u7d22\u7d50\u679c\uff0c\u4e26\u6839\u64da\u76f8\u95dc\u6027\u9032\u884c\u6392\u5e8f\uff0c\u9019\u6a23\u7528\u6236\u53ef\u4ee5\u5feb\u901f\u7372\u5f97\u6700\u76f8\u95dc\u7684\u7d50\u679c\u3002<\/p>\n<pre><code>\n# \u4f7f\u7528\u6709\u5e8f\u96c6\u5408\u5b58\u5132\u641c\u7d22\u7d50\u679c\nr.zadd('search_results', {'result1': 1, 'result2': 2, 'result3': 3})\n\n# \u7372\u53d6\u524d\u5169\u500b\u6700\u76f8\u95dc\u7684\u7d50\u679c\ntop_results = r.zrevrange('search_results', 0, 1)\n<\/code><\/pre>\n<h3 id=\"3-%e5%af%a6%e6%99%82%e6%9b%b4%e6%96%b0%e7%b4%a2%e5%bc%95-EvGmVgwRSQ\">3. \u5be6\u6642\u66f4\u65b0\u7d22\u5f15<\/h3>\n<p>Redis\u7684\u9ad8\u6027\u80fd\u7279\u6027\u4f7f\u5f97\u5b83\u80fd\u5920\u5be6\u6642\u66f4\u65b0\u7d22\u5f15\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u983b\u7e41\u66f4\u65b0\u6578\u64da\u7684\u61c9\u7528\u5c24\u70ba\u91cd\u8981\u3002\u901a\u904e\u5c07\u6578\u64da\u7684\u589e\u522a\u6539\u64cd\u4f5c\u540c\u6b65\u5230Redis\uff0c\u53ef\u4ee5\u78ba\u4fdd\u641c\u7d22\u7d50\u679c\u7684\u5373\u6642\u6027\u548c\u6e96\u78ba\u6027\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-EvGmVgwRSQ\">\u7d50\u8ad6<\/h2>\n<p>Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u6709\u6548\u5730\u89e3\u6c7a\u641c\u7d22\u74f6\u9838\u554f\u984c\u3002\u901a\u904e\u5c07Redis\u7528\u4f5c\u7de9\u5b58\u5c64\u3001\u5229\u7528\u5176\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\u4ee5\u53ca\u5be6\u6642\u66f4\u65b0\u7d22\u5f15\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u641c\u7d22\u6027\u80fd\u548c\u7528\u6236\u9ad4\u9a57\u3002\u5728\u9078\u64c7\u5408\u9069\u7684\u670d\u52d9\u5668\u67b6\u69cb\u6642\uff0c\u8003\u616e\u4f7f\u7528Redis\u4f86\u512a\u5316\u641c\u7d22\u529f\u80fd\u5c07\u662f\u4e00\u500b\u660e\u667a\u7684\u9078\u64c7\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<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>\u63a2\u7d22\u5982\u4f55\u5229\u7528Redis\u6280\u8853\u7a81\u7834\u641c\u7d22\u74f6\u9838\uff0c\u63d0\u5347\u6578\u64da\u6aa2\u7d22\u6548\u7387\uff0c\u89e3\u6c7a\u641c\u7d22\u554f\u984c\uff0c\u5be6\u73fe\u5feb\u901f\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-197639","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/197639","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=197639"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/197639\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=197639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=197639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=197639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}