{"id":152315,"date":"2024-11-01T21:55:23","date_gmt":"2024-11-01T13:55:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/152315\/"},"modified":"2024-11-01T21:55:24","modified_gmt":"2024-11-01T13:55:24","slug":"%e6%8e%a1%e7%94%a8redis%e5%bf%ab%e5%8f%96%e5%bf%ab%e9%80%9f%e5%8f%96%e5%be%97%e6%95%b8%e6%93%9a%ef%bc%88redis%e5%bf%ab%e5%8f%96%e5%8f%96%e6%95%b8%e6%93%9a%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/152315\/","title":{"rendered":"\u63a1\u7528Redis\u5feb\u53d6\u5feb\u901f\u53d6\u5f97\u6578\u64da\uff08redis\u5feb\u53d6\u53d6\u6578\u64da\uff09"},"content":{"rendered":"<h1 id=\"%e6%8e%a1%e7%94%a8redis%e5%bf%ab%e5%8f%96%e5%bf%ab%e9%80%9f%e5%8f%96%e5%be%97%e6%95%b8%e6%93%9a%ef%bc%88redis%e5%bf%ab%e5%8f%96%e5%8f%96%e6%95%b8%e6%93%9a%ef%bc%89-sCggqQITKB\">\u63a1\u7528Redis\u5feb\u53d6\u5feb\u901f\u53d6\u5f97\u6578\u64da\uff08redis\u5feb\u53d6\u53d6\u6578\u64da\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6578\u64da\u7684\u5feb\u901f\u5b58\u53d6\u5c0d\u65bc\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u548c\u7cfb\u7d71\u6027\u80fd\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\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\u800c\u53d7\u5230\u5ee3\u6cdb\u61c9\u7528\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u5feb\u53d6\u6280\u8853\u4f86\u5feb\u901f\u53d6\u5f97\u6578\u64da\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%afredis%ef%bc%9f-sCggqQITKB\">\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\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\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=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e4%bd%bf%e7%94%a8redis%e5%bf%ab%e5%8f%96%ef%bc%9f-sCggqQITKB\">\u70ba\u4ec0\u9ebc\u4f7f\u7528Redis\u5feb\u53d6\uff1f<\/h2>\n<ul>\n<li><strong>\u6027\u80fd\u63d0\u5347\uff1a<\/strong>Redis\u80fd\u5920\u5c07\u6578\u64da\u5b58\u5132\u5728\u5167\u5b58\u4e2d\uff0c\u9019\u610f\u5473\u8457\u6578\u64da\u7684\u8b80\u53d6\u901f\u5ea6\u6bd4\u50b3\u7d71\u7684\u78c1\u789f\u5b58\u5132\u5feb\u5f97\u591a\u3002<\/li>\n<li><strong>\u6e1b\u5c11\u6578\u64da\u5eab\u8ca0\u64d4\uff1a<\/strong>\u901a\u904e\u5c07\u983b\u7e41\u8a2a\u554f\u7684\u6578\u64da\u7de9\u5b58\u5728Redis\u4e2d\uff0c\u53ef\u4ee5\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8acb\u6c42\uff0c\u5f9e\u800c\u964d\u4f4e\u6578\u64da\u5eab\u7684\u8ca0\u64d4\u3002<\/li>\n<li><strong>\u9748\u6d3b\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\u6578\u64da\u985e\u578b\u4f86\u5b58\u5132\u548c\u64cd\u4f5c\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8redis%e5%bf%ab%e5%8f%96%e6%95%b8%e6%93%9a-sCggqQITKB\">\u5982\u4f55\u4f7f\u7528Redis\u5feb\u53d6\u6578\u64da<\/h2>\n<p>\u4f7f\u7528Redis\u5feb\u53d6\u6578\u64da\u7684\u57fa\u672c\u6d41\u7a0b\u5305\u62ec\u9023\u63a5Redis\u670d\u52d9\u5668\u3001\u8a2d\u7f6e\u5feb\u53d6\u6578\u64da\u3001\u7372\u53d6\u5feb\u53d6\u6578\u64da\u4ee5\u53ca\u8655\u7406\u5feb\u53d6\u5931\u6548\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"1-%e9%80%a3%e6%8e%a5redis%e6%9c%8d%e5%8b%99%e5%99%a8-sCggqQITKB\">1. \u9023\u63a5Redis\u670d\u52d9\u5668<\/h3>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230Redis\u670d\u52d9\u5668\nclient = redis.StrictRedis(host='localhost', port=6379, db=0)<\/code><\/pre>\n<h3 id=\"2-%e8%a8%ad%e7%bd%ae%e5%bf%ab%e5%8f%96%e6%95%b8%e6%93%9a-sCggqQITKB\">2. \u8a2d\u7f6e\u5feb\u53d6\u6578\u64da<\/h3>\n<pre><code># \u8a2d\u7f6e\u4e00\u500b\u5feb\u53d6\u6578\u64da\uff0c\u4e26\u8a2d\u7f6e\u904e\u671f\u6642\u9593\u70ba60\u79d2\nclient.set('key', 'value', ex=60)<\/code><\/pre>\n<h3 id=\"3-%e7%8d%b2%e5%8f%96%e5%bf%ab%e5%8f%96%e6%95%b8%e6%93%9a-sCggqQITKB\">3. \u7372\u53d6\u5feb\u53d6\u6578\u64da<\/h3>\n<pre><code># \u7372\u53d6\u5feb\u53d6\u6578\u64da\nvalue = client.get('key')\nif value:\n    print(f'\u5feb\u53d6\u6578\u64da: {value.decode(\"utf-8\")}')\nelse:\n    print('\u5feb\u53d6\u6578\u64da\u4e0d\u5b58\u5728\u6216\u5df2\u904e\u671f')<\/code><\/pre>\n<h3 id=\"4-%e8%99%95%e7%90%86%e5%bf%ab%e5%8f%96%e5%a4%b1%e6%95%88-sCggqQITKB\">4. \u8655\u7406\u5feb\u53d6\u5931\u6548<\/h3>\n<p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u5feb\u53d6\u6578\u64da\u53ef\u80fd\u6703\u56e0\u70ba\u904e\u671f\u800c\u5931\u6548\u3002\u958b\u767c\u8005\u9700\u8981\u8003\u616e\u5982\u4f55\u8655\u7406\u9019\u7a2e\u60c5\u6cc1\uff0c\u4f8b\u5982\u5728\u5feb\u53d6\u5931\u6548\u5f8c\u91cd\u65b0\u5f9e\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u6578\u64da\u4e26\u66f4\u65b0\u5feb\u53d6\u3002<\/p>\n<pre><code># \u6aa2\u67e5\u5feb\u53d6\u662f\u5426\u5b58\u5728\uff0c\u82e5\u4e0d\u5b58\u5728\u5247\u5f9e\u6578\u64da\u5eab\u7372\u53d6\nif not value:\n    # \u5047\u8a2d\u5f9e\u6578\u64da\u5eab\u7372\u53d6\u6578\u64da\u7684\u51fd\u6578\n    value = fetch_from_database('key')\n    client.set('key', value, ex=60)<\/code><\/pre>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-sCggqQITKB\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li><strong>\u5408\u7406\u8a2d\u7f6e\u904e\u671f\u6642\u9593\uff1a<\/strong>\u6839\u64da\u6578\u64da\u7684\u7279\u6027\u5408\u7406\u8a2d\u7f6e\u5feb\u53d6\u7684\u904e\u671f\u6642\u9593\uff0c\u4ee5\u5e73\u8861\u6027\u80fd\u548c\u6578\u64da\u7684\u65b0\u9bae\u5ea6\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5408\u9069\u7684\u6578\u64da\u7d50\u69cb\uff1a<\/strong>\u6839\u64da\u5be6\u969b\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684Redis\u6578\u64da\u7d50\u69cb\uff0c\u4ee5\u63d0\u9ad8\u64cd\u4f5c\u6548\u7387\u3002<\/li>\n<li><strong>\u76e3\u63a7\u5feb\u53d6\u547d\u4e2d\u7387\uff1a<\/strong>\u5b9a\u671f\u76e3\u63a7\u5feb\u53d6\u7684\u547d\u4e2d\u7387\uff0c\u4ee5\u8a55\u4f30\u5feb\u53d6\u7b56\u7565\u7684\u6709\u6548\u6027\uff0c\u4e26\u9032\u884c\u76f8\u61c9\u7684\u8abf\u6574\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-sCggqQITKB\">\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\u5347\u6578\u64da\u5b58\u53d6\u7684\u901f\u5ea6\u548c\u7cfb\u7d71\u6027\u80fd\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u7f6e\u548c\u4f7f\u7528\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5145\u5206\u767c\u63eeRedis\u7684\u512a\u52e2\uff0c\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8acb\u6c42\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u61c9\u7528\u7684\u97ff\u61c9\u901f\u5ea6\u548c\u7528\u6236\u9ad4\u9a57\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u7684\u670d\u52d9\u5c07\u662f\u60a8\u7684\u4e0d\u4e8c\u4e4b\u9078\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a1\u7528Redis\u5feb\u53d6\u6280\u8853\uff0c\u5feb\u901f\u53d6\u5f97\u6578\u64da\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\uff0c\u6e1b\u5c11\u5ef6\u9072\uff0c\u5be6\u73fe\u9ad8\u6548\u80fd\u6578\u64da\u5b58\u53d6\u89e3\u6c7a\u65b9\u6848\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-152315","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/152315","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=152315"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/152315\/revisions"}],"predecessor-version":[{"id":152316,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/152315\/revisions\/152316"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=152315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=152315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=152315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}