{"id":190048,"date":"2024-11-12T09:20:21","date_gmt":"2024-11-12T01:20:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/190048\/"},"modified":"2024-11-12T09:20:22","modified_gmt":"2024-11-12T01:20:22","slug":"%e7%8e%a9%e8%bd%89redis%e5%bf%ab%e5%8f%96%e7%b7%a8%e7%a8%8b%e4%b9%8b%e6%97%85%ef%bc%88redis%e5%bf%ab%e5%8f%96%e7%b7%a8%e7%a8%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/190048\/","title":{"rendered":"\u73a9\u8f49Redis\u5feb\u53d6\u7de8\u7a0b\u4e4b\u65c5\uff08redis\u5feb\u53d6\u7de8\u7a0b\uff09"},"content":{"rendered":"<h1 id=\"%e7%8e%a9%e8%bd%89redis%e5%bf%ab%e5%8f%96%e7%b7%a8%e7%a8%8b%e4%b9%8b%e6%97%85%ef%bc%88redis%e5%bf%ab%e5%8f%96%e7%b7%a8%e7%a8%8b%ef%bc%89-odXqafxUPB\">\u73a9\u8f49Redis\u5feb\u53d6\u7de8\u7a0b\u4e4b\u65c5\uff08redis\u5feb\u53d6\u7de8\u7a0b\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6027\u80fd\u548c\u6548\u7387\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u96a8\u8457\u7528\u6236\u9700\u6c42\u7684\u589e\u52a0\uff0c\u5982\u4f55\u5feb\u901f\u97ff\u61c9\u8acb\u6c42\u6210\u70ba\u4e86\u958b\u767c\u8005\u9762\u81e8\u7684\u4e00\u5927\u6311\u6230\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff0c\u56e0\u5176\u5353\u8d8a\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\u800c\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eRedis\u5feb\u53d6\u7de8\u7a0b\u7684\u57fa\u672c\u6982\u5ff5\u3001\u4f7f\u7528\u5834\u666f\u53ca\u5176\u5be6\u73fe\u65b9\u5f0f\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-odXqafxUPB\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\uff08Remote Dictionary Server\uff09\u662f\u4e00\u500b\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u6578\u64da\u5eab\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\u5b83\u901a\u5e38\u7528\u4f5c\u5feb\u53d6\u7cfb\u7d71\uff0c\u4ee5\u63d0\u9ad8\u6578\u64da\u5b58\u53d6\u7684\u901f\u5ea6\u3002Redis\u7684\u7279\u9ede\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u9ad8\u6027\u80fd\uff1aRedis\u80fd\u5920\u6bcf\u79d2\u8655\u7406\u6578\u5341\u842c\u6b21\u7684\u8b80\u53d6\u548c\u5beb\u5165\u64cd\u4f5c\u3002<\/li>\n<li>\u6301\u4e45\u5316\uff1a\u652f\u6301\u5c07\u6578\u64da\u6301\u4e45\u5316\u5230\u78c1\u789f\uff0c\u9632\u6b62\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<li>\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\uff1a\u652f\u6301\u591a\u7a2e\u6578\u64da\u985e\u578b\uff0c\u9069\u5408\u4e0d\u540c\u7684\u61c9\u7528\u5834\u666f\u3002<\/li>\n<li>\u5206\u4f48\u5f0f\uff1a\u652f\u6301\u4e3b\u5f9e\u8907\u88fd\u548c\u5206\u7247\uff0c\u65b9\u4fbf\u64f4\u5c55\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e7%9a%84%e4%bd%bf%e7%94%a8%e5%a0%b4%e6%99%af-odXqafxUPB\">Redis\u7684\u4f7f\u7528\u5834\u666f<\/h2>\n<p>Redis\u7684\u61c9\u7528\u5834\u666f\u975e\u5e38\u5ee3\u6cdb\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u4f7f\u7528\u60c5\u5883\uff1a<\/p>\n<ul>\n<li><strong>\u5feb\u53d6\u7cfb\u7d71\uff1a<\/strong>\u5c07\u983b\u7e41\u8a2a\u554f\u7684\u6578\u64da\u5b58\u5132\u5728Redis\u4e2d\uff0c\u4ee5\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8acb\u6c42\u3002<\/li>\n<li><strong>\u6703\u8a71\u7ba1\u7406\uff1a<\/strong>\u5728Web\u61c9\u7528\u4e2d\uff0c\u4f7f\u7528Redis\u4f86\u5b58\u5132\u7528\u6236\u6703\u8a71\u4fe1\u606f\uff0c\u63d0\u9ad8\u97ff\u61c9\u901f\u5ea6\u3002<\/li>\n<li><strong>\u6392\u884c\u699c\uff1a<\/strong>\u5229\u7528Redis\u7684\u6709\u5e8f\u96c6\u5408\u7279\u6027\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u6392\u884c\u699c\u529f\u80fd\u3002<\/li>\n<li><strong>\u6d88\u606f\u968a\u5217\uff1a<\/strong>\u4f7f\u7528Redis\u7684\u5217\u8868\u7d50\u69cb\u4f86\u5be6\u73fe\u7c21\u55ae\u7684\u6d88\u606f\u968a\u5217\u7cfb\u7d71\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e5%bf%ab%e5%8f%96%e7%b7%a8%e7%a8%8b%e5%af%a6%e7%8f%be-odXqafxUPB\">Redis\u5feb\u53d6\u7de8\u7a0b\u5be6\u73fe<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u901a\u904e\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\u4f86\u5c55\u793a\u5982\u4f55\u5728\u61c9\u7528\u4e2d\u5be6\u73feRedis\u5feb\u53d6\u3002<\/p>\n<h3 id=\"%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-odXqafxUPB\">\u74b0\u5883\u6e96\u5099<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u5df2\u7d93\u5b89\u88dd\u4e86Redis\u670d\u52d9\u5668\u548c\u76f8\u61c9\u7684\u5ba2\u6236\u7aef\u5eab\u3002\u4ee5Python\u70ba\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88ddRedis\u5ba2\u6236\u7aef\uff1a<\/p>\n<pre><code>pip install redis<\/code><\/pre>\n<h3 id=\"%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c%e7%a4%ba%e4%be%8b-odXqafxUPB\">\u57fa\u672c\u64cd\u4f5c\u793a\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684Python\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Redis\u9032\u884c\u5feb\u53d6\u64cd\u4f5c\uff1a<\/p>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230Redis\u670d\u52d9\u5668\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u8a2d\u7f6e\u5feb\u53d6\nr.set('key', 'value')\n\n# \u7372\u53d6\u5feb\u53d6\nvalue = r.get('key')\nprint(value.decode('utf-8'))  # \u8f38\u51fa: value\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u9023\u63a5\u5230\u672c\u5730\u7684Redis\u670d\u52d9\u5668\uff0c\u7136\u5f8c\u8a2d\u7f6e\u4e00\u500b\u9375\u503c\u5c0d\uff0c\u6700\u5f8c\u7372\u53d6\u4e26\u6253\u5370\u8a72\u503c\u3002<\/p>\n<h3 id=\"%e5%bf%ab%e5%8f%96%e7%ad%96%e7%95%a5-odXqafxUPB\">\u5feb\u53d6\u7b56\u7565<\/h3>\n<p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u9078\u64c7\u5408\u9069\u7684\u5feb\u53d6\u7b56\u7565\u81f3\u95dc\u91cd\u8981\u3002\u5e38\u898b\u7684\u5feb\u53d6\u7b56\u7565\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>LRU\uff08Least Recently Used\uff09\uff1a<\/strong>\u7576\u5feb\u53d6\u6eff\u6642\uff0c\u512a\u5148\u6dd8\u6c70\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u6578\u64da\u3002<\/li>\n<li><strong>TTL\uff08Time To Live\uff09\uff1a<\/strong>\u70ba\u6bcf\u500b\u5feb\u53d6\u9805\u8a2d\u7f6e\u904e\u671f\u6642\u9593\uff0c\u81ea\u52d5\u6e05\u9664\u904e\u671f\u6578\u64da\u3002<\/li>\n<li><strong>\u4e3b\u52d5\u6e05\u9664\uff1a<\/strong>\u6839\u64da\u696d\u52d9\u9700\u6c42\uff0c\u4e3b\u52d5\u6e05\u9664\u4e0d\u518d\u9700\u8981\u7684\u5feb\u53d6\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-odXqafxUPB\">\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\u61c9\u7528\u7684\u6027\u80fd\u548c\u97ff\u61c9\u901f\u5ea6\u3002\u901a\u904e\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\u548c\u591a\u6a23\u7684\u4f7f\u7528\u5834\u666f\uff0cRedis\u5df2\u6210\u70ba\u958b\u767c\u8005\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u5728\u9078\u64c7\u5408\u9069\u7684\u5feb\u53d6\u7b56\u7565\u548c\u5be6\u73fe\u65b9\u5f0f\u5f8c\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5145\u5206\u767c\u63eeRedis\u7684\u6f5b\u529b\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684Redis\u670d\u52d9\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\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\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Redis\u5feb\u53d6\u7de8\u7a0b\u7684\u6280\u5de7\u8207\u5be6\u8e10\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\uff0c\u512a\u5316\u6578\u64da\u5b58\u53d6\uff0c\u8b93\u4f60\u7684\u958b\u767c\u4e4b\u65c5\u66f4\u9ad8\u6548\uff01<\/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-190048","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/190048","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=190048"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/190048\/revisions"}],"predecessor-version":[{"id":190049,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/190048\/revisions\/190049"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=190048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=190048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=190048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}