{"id":126937,"date":"2024-10-26T08:53:24","date_gmt":"2024-10-26T00:53:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/126937\/"},"modified":"2024-10-26T08:53:25","modified_gmt":"2024-10-26T00:53:25","slug":"redis-%e8%ae%93%e5%bf%ab%e5%8f%96%e6%bf%80%e6%b4%bb%e8%b5%b7%e4%be%86","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/126937\/","title":{"rendered":"Redis \u8b93\u5feb\u53d6\u6fc0\u6d3b\u8d77\u4f86"},"content":{"rendered":"<h1 id=\"redis-%e8%ae%93%e5%bf%ab%e5%8f%96%e6%bf%80%e6%b4%bb%e8%b5%b7%e4%be%86-pxfimRtpzF\">Redis \u8b93\u5feb\u53d6\u6fc0\u6d3b\u8d77\u4f86<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u8def\u74b0\u5883\u4e2d\uff0c\u7db2\u7ad9\u7684\u901f\u5ea6\u548c\u6548\u80fd\u5c0d\u65bc\u7528\u6236\u9ad4\u9a57\u81f3\u95dc\u91cd\u8981\u3002\u96a8\u8457\u6578\u64da\u91cf\u7684\u6fc0\u589e\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u548c\u5b58\u53d6\u9019\u4e9b\u6578\u64da\u6210\u70ba\u4e86\u958b\u767c\u8005\u548c\u7cfb\u7d71\u7ba1\u7406\u54e1\u9762\u81e8\u7684\u4e00\u5927\u6311\u6230\u3002Redis\uff0c\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u8a18\u61b6\u9ad4\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u5feb\u53d6\u529f\u80fd\uff0c\u80fd\u5920\u986f\u8457\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-pxfimRtpzF\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\uff08Remote Dictionary Server\uff09\u662f\u4e00\u500b\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u548c\u6709\u5e8f\u96c6\u5408\u7b49\u3002\u7531\u65bc\u5176\u5728\u8a18\u61b6\u9ad4\u4e2d\u64cd\u4f5c\u6578\u64da\u7684\u7279\u6027\uff0cRedis\u80fd\u5920\u63d0\u4f9b\u6975\u5feb\u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\uff0c\u9019\u4f7f\u5f97\u5b83\u6210\u70ba\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e5%bf%ab%e5%8f%96%e6%a9%9f%e5%88%b6-pxfimRtpzF\">Redis\u7684\u5feb\u53d6\u6a5f\u5236<\/h2>\n<p>Redis\u7684\u5feb\u53d6\u6a5f\u5236\u4e3b\u8981\u4f9d\u8cf4\u65bc\u5176\u9ad8\u6548\u7684\u6578\u64da\u5b58\u53d6\u65b9\u5f0f\u3002\u7576\u61c9\u7528\u7a0b\u5f0f\u9700\u8981\u5b58\u53d6\u67d0\u4e9b\u6578\u64da\u6642\uff0c\u9996\u5148\u6703\u6aa2\u67e5Redis\u5feb\u53d6\u4e2d\u662f\u5426\u5b58\u5728\u8a72\u6578\u64da\u3002\u5982\u679c\u5b58\u5728\uff0c\u5247\u76f4\u63a5\u5f9e\u5feb\u53d6\u4e2d\u8b80\u53d6\uff0c\u9019\u6a23\u53ef\u4ee5\u5927\u5e45\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8acb\u6c42\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002<\/p>\n<h3 id=\"%e5%bf%ab%e5%8f%96%e7%9a%84%e5%b7%a5%e4%bd%9c%e6%b5%81%e7%a8%8b-pxfimRtpzF\">\u5feb\u53d6\u7684\u5de5\u4f5c\u6d41\u7a0b<\/h3>\n<ul>\n<li>\u7528\u6236\u8acb\u6c42\u6578\u64da\u3002<\/li>\n<li>\u61c9\u7528\u7a0b\u5f0f\u6aa2\u67e5Redis\u5feb\u53d6\u3002<\/li>\n<li>\u5982\u679c\u6578\u64da\u5b58\u5728\u65bc\u5feb\u53d6\u4e2d\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u8a72\u6578\u64da\u3002<\/li>\n<li>\u5982\u679c\u6578\u64da\u4e0d\u5728\u5feb\u53d6\u4e2d\uff0c\u5247\u5f9e\u5f8c\u7aef\u6578\u64da\u5eab\u8b80\u53d6\u6578\u64da\uff0c\u4e26\u5c07\u5176\u5b58\u5165Redis\u5feb\u53d6\u4e2d\u4ee5\u5099\u5f8c\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e7%9a%84%e5%84%aa%e5%8b%a2-pxfimRtpzF\">Redis\u7684\u512a\u52e2<\/h2>\n<p>\u4f7f\u7528Redis\u4f5c\u70ba\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\u6709\u591a\u500b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6548\u80fd\uff1a<\/strong>Redis\u80fd\u5920\u5728\u6beb\u79d2\u7d1a\u5225\u5167\u5b8c\u6210\u6578\u64da\u7684\u8b80\u53d6\u548c\u5beb\u5165\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u5feb\u901f\u97ff\u61c9\u7684\u61c9\u7528\u7a0b\u5f0f\u81f3\u95dc\u91cd\u8981\u3002<\/li>\n<li><strong>\u591a\u6a23\u7684\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u6700\u5408\u9069\u7684\u7d50\u69cb\u4f86\u5b58\u5132\u6578\u64da\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>\u96d6\u7136Redis\u4e3b\u8981\u7528\u4f5c\u5feb\u53d6\uff0c\u4f46\u5b83\u4e5f\u63d0\u4f9b\u4e86\u6578\u64da\u6301\u4e45\u5316\u7684\u9078\u9805\uff0c\u78ba\u4fdd\u6578\u64da\u4e0d\u6703\u56e0\u70ba\u7cfb\u7d71\u91cd\u555f\u800c\u4e1f\u5931\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>Redis\u7684API\u8a2d\u8a08\u7c21\u55ae\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u4e0a\u624b\uff0c\u5feb\u901f\u96c6\u6210\u5230\u73fe\u6709\u7684\u61c9\u7528\u7a0b\u5f0f\u4e2d\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e7%9a%84%e4%bd%bf%e7%94%a8%e7%af%84%e4%be%8b-pxfimRtpzF\">Redis\u7684\u4f7f\u7528\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u4f7f\u7528Redis\u9032\u884c\u5feb\u53d6\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230Redis\u4f3a\u670d\u5668\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u5b9a\u7fa9\u4e00\u500b\u51fd\u6578\u4f86\u7372\u53d6\u6578\u64da\ndef get_data(key):\n    # \u5617\u8a66\u5f9e\u5feb\u53d6\u4e2d\u7372\u53d6\u6578\u64da\n    cached_data = r.get(key)\n    if cached_data:\n        return cached_data.decode('utf-8')  # \u8fd4\u56de\u5feb\u53d6\u4e2d\u7684\u6578\u64da\n    else:\n        # \u5982\u679c\u5feb\u53d6\u4e2d\u6c92\u6709\uff0c\u5247\u5f9e\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u6578\u64da\n        data = fetch_from_database(key)  # \u5047\u8a2d\u9019\u662f\u4e00\u500b\u5f9e\u6578\u64da\u5eab\u7372\u53d6\u6578\u64da\u7684\u51fd\u6578\n        r.set(key, data)  # \u5c07\u6578\u64da\u5b58\u5165\u5feb\u53d6\n        return data\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-pxfimRtpzF\">\u7d50\u8ad6<\/h2>\n<p>Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u986f\u8457\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\uff0c\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8acb\u6c42\uff0c\u4e26\u63d0\u4f9b\u5feb\u901f\u7684\u6578\u64da\u5b58\u53d6\u80fd\u529b\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u61c9\u7528\u9084\u662f\u5927\u578b\u7cfb\u7d71\uff0cRedis\u90fd\u80fd\u5920\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u9748\u6d3b\u4e14\u9ad8\u6548\u7684\u6578\u64da\u7ba1\u7406\u65b9\u5f0f\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72Redis\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\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\u7684\u6027\u80fd\u548c\u512a\u8cea\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis \u8b93\u5feb\u53d6\u6fc0\u6d3b\u8d77\u4f86\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\uff0c\u5be6\u73fe\u5feb\u901f\u6578\u64da\u5b58\u53d6\uff0c\u512a\u5316\u7528\u6236\u9ad4\u9a57\uff0c\u52a9\u529b\u696d\u52d9\u589e\u9577\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-126937","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/126937","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=126937"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/126937\/revisions"}],"predecessor-version":[{"id":126938,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/126937\/revisions\/126938"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=126937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=126937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=126937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}