{"id":193323,"date":"2024-11-12T21:12:29","date_gmt":"2024-11-12T13:12:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/193323\/"},"modified":"2024-11-12T21:12:31","modified_gmt":"2024-11-12T13:12:31","slug":"%e5%ba%8f%e5%88%97%e5%8c%96%e5%ad%98%e5%84%b2redis%e7%9a%84%e5%bf%85%e8%a6%81%e6%ad%a5%e9%a9%9f%ef%bc%88%e5%ad%98redis%e6%99%82%e9%9c%80%e8%a6%81%e5%ba%8f%e5%88%97%e5%8c%96%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/193323\/","title":{"rendered":"\u5e8f\u5217\u5316\u5b58\u5132Redis\u7684\u5fc5\u8981\u6b65\u9a5f\uff08\u5b58redis\u6642\u9700\u8981\u5e8f\u5217\u5316\uff09"},"content":{"rendered":"<h1 id=\"%e5%ba%8f%e5%88%97%e5%8c%96%e5%ad%98%e5%84%b2redis%e7%9a%84%e5%bf%85%e8%a6%81%e6%ad%a5%e9%a9%9f%ef%bc%88%e5%ad%98redis%e6%99%82%e9%9c%80%e8%a6%81%e5%ba%8f%e5%88%97%e5%8c%96%ef%bc%89-VLGgRqoFWu\">\u5e8f\u5217\u5316\u5b58\u5132Redis\u7684\u5fc5\u8981\u6b65\u9a5f\uff08\u5b58redis\u6642\u9700\u8981\u5e8f\u5217\u5316\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0cRedis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u5834\u666f\uff0c\u5305\u62ec\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u5be6\u6642\u6578\u64da\u8655\u7406\u7b49\u3002\u7136\u800c\uff0c\u7576\u6211\u5011\u9700\u8981\u5c07\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\u5b58\u5132\u5230Redis\u4e2d\u6642\uff0c\u5e8f\u5217\u5316\u6210\u70ba\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684\u6b65\u9a5f\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5728\u4f7f\u7528Redis\u6642\u9032\u884c\u5e8f\u5217\u5316\u7684\u5fc5\u8981\u6027\u53ca\u5176\u5177\u9ad4\u6b65\u9a5f\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e5%ba%8f%e5%88%97%e5%8c%96%ef%bc%9f-VLGgRqoFWu\">\u4ec0\u9ebc\u662f\u5e8f\u5217\u5316\uff1f<\/h2>\n<p>\u5e8f\u5217\u5316\u662f\u5c07\u6578\u64da\u7d50\u69cb\u6216\u5c0d\u8c61\u8f49\u63db\u70ba\u53ef\u5b58\u5132\u6216\u50b3\u8f38\u7684\u683c\u5f0f\u7684\u904e\u7a0b\u3002\u9019\u4e00\u904e\u7a0b\u4f7f\u5f97\u6578\u64da\u80fd\u5920\u5728\u4e0d\u540c\u7684\u7cfb\u7d71\u4e4b\u9593\u9032\u884c\u50b3\u905e\uff0c\u6216\u5728\u5b58\u5132\u4ecb\u8cea\u4e2d\u6301\u4e45\u5316\u3002\u53cd\u5e8f\u5217\u5316\u5247\u662f\u5c07\u5e8f\u5217\u5316\u5f8c\u7684\u6578\u64da\u8f49\u63db\u56de\u539f\u59cb\u6578\u64da\u7d50\u69cb\u7684\u904e\u7a0b\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%9c%80%e8%a6%81%e5%ba%8f%e5%88%97%e5%8c%96%ef%bc%9f-VLGgRqoFWu\">\u70ba\u4ec0\u9ebc\u9700\u8981\u5e8f\u5217\u5316\uff1f<\/h2>\n<p>\u5728\u4f7f\u7528Redis\u5b58\u5132\u6578\u64da\u6642\uff0c\u5e8f\u5217\u5316\u7684\u5fc5\u8981\u6027\u4e3b\u8981\u9ad4\u73fe\u5728\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<ul>\n<li><strong>\u6578\u64da\u7d50\u69cb\u7684\u517c\u5bb9\u6027\uff1a<\/strong>Redis\u652f\u6301\u7684\u6578\u64da\u985e\u578b\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u548c\u6709\u5e8f\u96c6\u5408\u7b49\u3002\u5982\u679c\u6211\u5011\u9700\u8981\u5b58\u5132\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5c0d\u8c61\u6216\u6578\u7d44\uff0c\u5247\u5fc5\u9808\u5c07\u5176\u5e8f\u5217\u5316\u70ba\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<\/li>\n<li><strong>\u6578\u64da\u7684\u6301\u4e45\u5316\uff1a<\/strong>\u5e8f\u5217\u5316\u5f8c\u7684\u6578\u64da\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b58\u5132\u5230Redis\u4e2d\uff0c\u4e26\u5728\u9700\u8981\u6642\u9032\u884c\u53cd\u5e8f\u5217\u5316\u4ee5\u6062\u5fa9\u539f\u59cb\u6578\u64da\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u6027\u80fd\uff1a<\/strong>\u5e8f\u5217\u5316\u53ef\u4ee5\u6e1b\u5c11\u6578\u64da\u7684\u5927\u5c0f\uff0c\u5f9e\u800c\u63d0\u9ad8\u5b58\u5132\u548c\u50b3\u8f38\u7684\u6548\u7387\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%ba%8f%e5%88%97%e5%8c%96%e7%9a%84%e6%ad%a5%e9%a9%9f-VLGgRqoFWu\">\u5e8f\u5217\u5316\u7684\u6b65\u9a5f<\/h2>\n<p>\u5728\u5c07\u6578\u64da\u5b58\u5132\u5230Redis\u4e4b\u524d\uff0c\u9032\u884c\u5e8f\u5217\u5316\u7684\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<h3 id=\"1-%e9%81%b8%e6%93%87%e5%ba%8f%e5%88%97%e5%8c%96%e6%a0%bc%e5%bc%8f-VLGgRqoFWu\">1. \u9078\u64c7\u5e8f\u5217\u5316\u683c\u5f0f<\/h3>\n<p>\u5e38\u898b\u7684\u5e8f\u5217\u5316\u683c\u5f0f\u5305\u62ecJSON\u3001XML\u3001Protocol Buffers\u7b49\u3002\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff0cJSON\u683c\u5f0f\u6613\u65bc\u95b1\u8b80\u548c\u7de8\u8f2f\uff0c\u9069\u5408\u7528\u65bcWeb\u61c9\u7528\uff1b\u800cProtocol Buffers\u5247\u5728\u6027\u80fd\u4e0a\u66f4\u5177\u512a\u52e2\uff0c\u9069\u5408\u7528\u65bc\u9ad8\u6548\u7684\u6578\u64da\u50b3\u8f38\u3002<\/p>\n<h3 id=\"2-%e5%af%a6%e7%8f%be%e5%ba%8f%e5%88%97%e5%8c%96-VLGgRqoFWu\">2. \u5be6\u73fe\u5e8f\u5217\u5316<\/h3>\n<p>\u6839\u64da\u9078\u64c7\u7684\u5e8f\u5217\u5316\u683c\u5f0f\uff0c\u4f7f\u7528\u76f8\u61c9\u7684\u5eab\u4f86\u5be6\u73fe\u5e8f\u5217\u5316\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u9032\u884cJSON\u5e8f\u5217\u5316\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import json\n\n# \u5b9a\u7fa9\u4e00\u500b\u5b57\u5178\ndata = {\n    'name': 'Server.HK',\n    'type': 'VPS Hosting',\n    'location': 'Hong Kong'\n}\n\n# \u5c07\u5b57\u5178\u5e8f\u5217\u5316\u70baJSON\u5b57\u7b26\u4e32\njson_data = json.dumps(data)\nprint(json_data)  # \u8f38\u51fa: {\"name\": \"Server.HK\", \"type\": \"VPS Hosting\", \"location\": \"Hong Kong\"}\n<\/code><\/pre>\n<h3 id=\"3-%e5%ad%98%e5%84%b2%e5%88%b0redis-VLGgRqoFWu\">3. \u5b58\u5132\u5230Redis<\/h3>\n<p>\u4f7f\u7528Redis\u7684\u5ba2\u6236\u7aef\u5eab\u5c07\u5e8f\u5217\u5316\u5f8c\u7684\u6578\u64da\u5b58\u5132\u5230Redis\u4e2d\u3002\u4ee5\u4e0b\u662f\u5c07JSON\u5b57\u7b26\u4e32\u5b58\u5132\u5230Redis\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230Redis\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u5c07\u5e8f\u5217\u5316\u7684\u6578\u64da\u5b58\u5132\u5230Redis\nr.set('server_info', json_data)\n<\/code><\/pre>\n<h3 id=\"4-%e5%8f%8d%e5%ba%8f%e5%88%97%e5%8c%96-VLGgRqoFWu\">4. \u53cd\u5e8f\u5217\u5316<\/h3>\n<p>\u7576\u9700\u8981\u4f7f\u7528\u5b58\u5132\u7684\u6578\u64da\u6642\uff0c\u9996\u5148\u5f9eRedis\u4e2d\u7372\u53d6\u6578\u64da\uff0c\u7136\u5f8c\u9032\u884c\u53cd\u5e8f\u5217\u5316\u3002\u4ee5\u4e0b\u662f\u53cd\u5e8f\u5217\u5316\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code># \u5f9eRedis\u7372\u53d6\u6578\u64da\nstored_data = r.get('server_info')\n\n# \u5c07JSON\u5b57\u7b26\u4e32\u53cd\u5e8f\u5217\u5316\u70ba\u5b57\u5178\ndata_dict = json.loads(stored_data)\nprint(data_dict)  # \u8f38\u51fa: {'name': 'Server.HK', 'type': 'VPS Hosting', 'location': 'Hong Kong'}\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-VLGgRqoFWu\">\u7e3d\u7d50<\/h2>\n<p>\u5e8f\u5217\u5316\u5728\u4f7f\u7528Redis\u5b58\u5132\u6578\u64da\u6642\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u6b65\u9a5f\u3002\u5b83\u4e0d\u50c5\u80fd\u5920\u63d0\u9ad8\u6578\u64da\u7684\u517c\u5bb9\u6027\u548c\u6301\u4e45\u6027\uff0c\u9084\u80fd\u63d0\u5347\u6027\u80fd\u3002\u901a\u904e\u9078\u64c7\u5408\u9069\u7684\u5e8f\u5217\u5316\u683c\u5f0f\u4e26\u6b63\u78ba\u5be6\u73fe\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u904e\u7a0b\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u5229\u7528Redis\u9032\u884c\u6578\u64da\u5b58\u5132\u548c\u7ba1\u7406\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\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\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\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u53ef\u9760\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5728Redis\u4e2d\u5b58\u50a8\u6570\u636e\u65f6\u5e8f\u5217\u5316\u7684\u5fc5\u8981\u6b65\u9aa4\uff0c\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u4e0e\u9ad8\u6548\u5b58\u53d6\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-193323","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/193323","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=193323"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/193323\/revisions"}],"predecessor-version":[{"id":193324,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/193323\/revisions\/193324"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=193323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=193323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=193323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}