{"id":185140,"date":"2024-11-10T19:04:28","date_gmt":"2024-11-10T11:04:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/185140\/"},"modified":"2024-11-10T19:04:29","modified_gmt":"2024-11-10T11:04:29","slug":"%e4%bd%bf%e7%94%a8redis%e5%84%aa%e5%8c%96%e5%ad%98%e5%84%b2%e5%b0%8d%e8%b1%a1%ef%bc%88%e4%bd%bf%e7%94%a8redis%e5%ad%98%e5%84%b2%e5%b0%8d%e8%b1%a1%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/185140\/","title":{"rendered":"\u4f7f\u7528Redis\u512a\u5316\u5b58\u5132\u5c0d\u8c61\uff08\u4f7f\u7528redis\u5b58\u5132\u5c0d\u8c61\uff09"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8redis%e5%84%aa%e5%8c%96%e5%ad%98%e5%84%b2%e5%b0%8d%e8%b1%a1%ef%bc%88%e4%bd%bf%e7%94%a8redis%e5%ad%98%e5%84%b2%e5%b0%8d%e8%b1%a1%ef%bc%89-TmPhWaLfCQ\">\u4f7f\u7528Redis\u512a\u5316\u5b58\u5132\u5c0d\u8c61\uff08\u4f7f\u7528redis\u5b58\u5132\u5c0d\u8c61\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0c\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u6548\u7387\u81f3\u95dc\u91cd\u8981\u3002\u96a8\u8457\u7528\u6236\u9700\u6c42\u7684\u589e\u52a0\uff0c\u958b\u767c\u8005\u9700\u8981\u5c0b\u627e\u66f4\u5feb\u3001\u66f4\u53ef\u9760\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u7d50\u69cb\u5b58\u5132\u7cfb\u7d71\uff0c\u5df2\u7d93\u6210\u70ba\u8a31\u591a\u958b\u767c\u8005\u7684\u9996\u9078\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Redis\u4f86\u512a\u5316\u5b58\u5132\u5c0d\u8c61\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-TmPhWaLfCQ\">\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\u3002\u7531\u65bc\u5176\u5728\u5167\u5b58\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\u975e\u5e38\u9069\u5408\u7528\u65bc\u9700\u8981\u9ad8\u6027\u80fd\u7684\u61c9\u7528\u5834\u666f\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%e4%be%86%e5%ad%98%e5%84%b2%e5%b0%8d%e8%b1%a1%ef%bc%9f-TmPhWaLfCQ\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\u4f86\u5b58\u5132\u5c0d\u8c61\uff1f<\/h2>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u7684\u5167\u5b58\u5b58\u5132\u7279\u6027\u4f7f\u5176\u80fd\u5920\u4ee5\u6beb\u79d2\u7d1a\u7684\u901f\u5ea6\u8655\u7406\u8acb\u6c42\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u5feb\u901f\u97ff\u61c9\u7684\u61c9\u7528\u7a0b\u5e8f\u81f3\u95dc\u91cd\u8981\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\u5b58\u5132\u65b9\u5f0f\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>\u96d6\u7136Redis\u4e3b\u8981\u662f\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4f46\u5b83\u4e5f\u63d0\u4f9b\u4e86\u6301\u4e45\u5316\u9078\u9805\uff0c\u5141\u8a31\u6578\u64da\u5728\u91cd\u555f\u5f8c\u4ecd\u7136\u53ef\u7528\u3002<\/li>\n<li><strong>\u5206\u4f48\u5f0f\u652f\u6301\uff1a<\/strong>Redis\u652f\u6301\u4e3b\u5f9e\u8907\u88fd\u548c\u5206\u7247\uff0c\u9019\u4f7f\u5f97\u5b83\u80fd\u5920\u8f15\u9b06\u64f4\u5c55\u4ee5\u8655\u7406\u66f4\u5927\u7684\u6578\u64da\u96c6\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8redis%e5%ad%98%e5%84%b2%e5%b0%8d%e8%b1%a1-TmPhWaLfCQ\">\u5982\u4f55\u4f7f\u7528Redis\u5b58\u5132\u5c0d\u8c61<\/h2>\n<p>\u5728\u4f7f\u7528Redis\u5b58\u5132\u5c0d\u8c61\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88ddRedis\u670d\u52d9\u5668\u4e26\u914d\u7f6e\u597d\u74b0\u5883\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Redis\u5b58\u5132\u5c0d\u8c61\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9dredis-TmPhWaLfCQ\">1. \u5b89\u88ddRedis<\/h3>\n<p>\u5728\u5927\u591a\u6578Linux\u7cfb\u7d71\u4e0a\uff0c\u53ef\u4ee5\u901a\u904e\u5305\u7ba1\u7406\u5668\u8f15\u9b06\u5b89\u88ddRedis\u3002\u4f8b\u5982\uff0c\u5728Ubuntu\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>sudo apt-get update\nsudo apt-get install redis-server<\/code><\/pre>\n<h3 id=\"2-%e9%80%a3%e6%8e%a5%e5%88%b0redis-TmPhWaLfCQ\">2. \u9023\u63a5\u5230Redis<\/h3>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528Redis\u7684\u547d\u4ee4\u884c\u754c\u9762\uff08CLI\uff09\u6216\u901a\u904e\u7de8\u7a0b\u8a9e\u8a00\u7684\u5ba2\u6236\u7aef\u5eab\u4f86\u9023\u63a5Redis\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230Redis\nr = redis.Redis(host='localhost', port=6379, db=0)<\/code><\/pre>\n<h3 id=\"3-%e5%ad%98%e5%84%b2%e5%b0%8d%e8%b1%a1-TmPhWaLfCQ\">3. \u5b58\u5132\u5c0d\u8c61<\/h3>\n<p>\u5728Redis\u4e2d\uff0c\u5c0d\u8c61\u901a\u5e38\u4ee5JSON\u683c\u5f0f\u5b58\u5132\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684json\u5eab\u5c07\u5c0d\u8c61\u8f49\u63db\u70baJSON\u5b57\u7b26\u4e32\uff0c\u7136\u5f8c\u5c07\u5176\u5b58\u5132\u5728Redis\u4e2d\uff1a<\/p>\n<pre><code>import json\n\n# \u5b9a\u7fa9\u4e00\u500b\u5c0d\u8c61\nobj = {'name': 'Alice', 'age': 30, 'city': 'Hong Kong'}\n\n# \u5c07\u5c0d\u8c61\u8f49\u63db\u70baJSON\u5b57\u7b26\u4e32\nobj_json = json.dumps(obj)\n\n# \u5b58\u5132\u5c0d\u8c61\nr.set('user:1000', obj_json)<\/code><\/pre>\n<h3 id=\"4-%e7%8d%b2%e5%8f%96%e5%b0%8d%e8%b1%a1-TmPhWaLfCQ\">4. \u7372\u53d6\u5c0d\u8c61<\/h3>\n<p>\u8981\u5f9eRedis\u4e2d\u7372\u53d6\u5c0d\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\uff1a<\/p>\n<pre><code># \u7372\u53d6\u5c0d\u8c61\nobj_json = r.get('user:1000')\n\n# \u5c07JSON\u5b57\u7b26\u4e32\u8f49\u63db\u56de\u5c0d\u8c61\nobj = json.loads(obj_json)\nprint(obj)<\/code><\/pre>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-TmPhWaLfCQ\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li><strong>\u4f7f\u7528\u5408\u9069\u7684\u6578\u64da\u7d50\u69cb\uff1a<\/strong>\u6839\u64da\u61c9\u7528\u9700\u6c42\u9078\u64c7\u6700\u5408\u9069\u7684Redis\u6578\u64da\u7d50\u69cb\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u548c\u53ef\u8b80\u6027\u3002<\/li>\n<li><strong>\u8a2d\u7f6e\u904e\u671f\u6642\u9593\uff1a<\/strong>\u5c0d\u65bc\u4e0d\u9700\u8981\u6c38\u4e45\u5b58\u5132\u7684\u6578\u64da\uff0c\u53ef\u4ee5\u8a2d\u7f6e\u904e\u671f\u6642\u9593\u4ee5\u91cb\u653e\u5167\u5b58\u3002<\/li>\n<li><strong>\u76e3\u63a7\u6027\u80fd\uff1a<\/strong>\u5b9a\u671f\u76e3\u63a7Redis\u7684\u6027\u80fd\u6307\u6a19\uff0c\u4ee5\u78ba\u4fdd\u7cfb\u7d71\u904b\u884c\u5e73\u7a69\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-TmPhWaLfCQ\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Redis\u4f86\u512a\u5316\u5b58\u5132\u5c0d\u8c61\u662f\u4e00\u7a2e\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u97ff\u61c9\u901f\u5ea6\u3002\u901a\u904e\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\u548c\u9ad8\u6548\u7684\u5167\u5b58\u64cd\u4f5c\uff0cRedis\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u5de5\u5177\u4f86\u8655\u7406\u5404\u7a2e\u6578\u64da\u5b58\u5132\u9700\u6c42\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884cRedis\uff0cServer.HK\u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u7684\u696d\u52d9\u9700\u6c42\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\u90fd\u80fd\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528Redis\u512a\u5316\u5b58\u5132\u5c0d\u8c61\uff0c\u63d0\u9ad8\u6578\u64da\u5b58\u53d6\u6548\u7387\uff0c\u5be6\u73fe\u5feb\u901f\u97ff\u61c9\u548c\u9ad8\u6027\u80fd\u61c9\u7528\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-185140","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/185140","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=185140"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/185140\/revisions"}],"predecessor-version":[{"id":185141,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/185140\/revisions\/185141"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=185140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=185140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=185140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}