{"id":163667,"date":"2024-11-05T01:25:20","date_gmt":"2024-11-04T17:25:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/163667\/"},"modified":"2024-11-05T01:25:21","modified_gmt":"2024-11-04T17:25:21","slug":"%e8%a7%a3%e6%b1%baredis%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e5%95%8f%e9%a1%8c%ef%bc%88%e5%ad%98%e5%85%a5redis%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/163667\/","title":{"rendered":"\u89e3\u6c7aRedis\u4e2d\u6587\u4e82\u78bc\u554f\u984c\uff08\u5b58\u5165redis\u4e2d\u6587\u4e82\u78bc\uff09"},"content":{"rendered":"<h1 id=\"%e8%a7%a3%e6%b1%baredis%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e5%95%8f%e9%a1%8c%ef%bc%88%e5%ad%98%e5%85%a5redis%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%ef%bc%89-wXutfvwmgB\">\u89e3\u6c7aRedis\u4e2d\u6587\u4e82\u78bc\u554f\u984c\uff08\u5b58\u5165redis\u4e2d\u6587\u4e82\u78bc\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\uff0cRedis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u6578\u64da\u7de9\u5b58\u548c\u5373\u6642\u6578\u64da\u8655\u7406\u3002\u7136\u800c\uff0c\u7576\u6d89\u53ca\u5230\u4e2d\u6587\u5b57\u7b26\u7684\u5b58\u5132\u6642\uff0c\u958b\u767c\u8005\u7d93\u5e38\u6703\u9047\u5230\u4e82\u78bc\u554f\u984c\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u6709\u6548\u89e3\u6c7aRedis\u4e2d\u7684\u4e2d\u6587\u4e82\u78bc\u554f\u984c\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u89e3\u6c7a\u65b9\u6848\u548c\u793a\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e6%9c%83%e5%87%ba%e7%8f%be%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc-wXutfvwmgB\">\u70ba\u4ec0\u9ebc\u6703\u51fa\u73fe\u4e2d\u6587\u4e82\u78bc<\/h2>\n<p>\u4e2d\u6587\u4e82\u78bc\u7684\u51fa\u73fe\u901a\u5e38\u8207\u5b57\u7b26\u7de8\u78bc\u6709\u95dc\u3002Redis\u672c\u8eab\u4f7f\u7528UTF-8\u7de8\u78bc\u4f86\u5b58\u5132\u6578\u64da\uff0c\u4f46\u5982\u679c\u5728\u5b58\u5165\u6216\u53d6\u51fa\u6578\u64da\u6642\uff0c\u4f7f\u7528\u4e86\u4e0d\u6b63\u78ba\u7684\u7de8\u78bc\u65b9\u5f0f\uff0c\u5c31\u6703\u5c0e\u81f4\u4e82\u78bc\u73fe\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u539f\u56e0\uff1a<\/p>\n<ul>\n<li><strong>\u7de8\u78bc\u4e0d\u4e00\u81f4\uff1a<\/strong>\u5982\u679c\u5728\u5b58\u5132\u6578\u64da\u6642\u4f7f\u7528\u4e86\u4e0d\u540c\u65bcUTF-8\u7684\u7de8\u78bc\uff0c\u5247\u5728\u53d6\u51fa\u6642\u6703\u51fa\u73fe\u4e82\u78bc\u3002<\/li>\n<li><strong>\u6578\u64da\u8f49\u63db\u554f\u984c\uff1a<\/strong>\u5728\u5c07\u6578\u64da\u5f9e\u61c9\u7528\u7a0b\u5f0f\u8f49\u63db\u5230Redis\u6642\uff0c\u53ef\u80fd\u6703\u56e0\u70ba\u8f49\u63db\u4e0d\u7576\u800c\u5c0e\u81f4\u4e82\u78bc\u3002<\/li>\n<li><strong>\u5ba2\u6236\u7aef\u914d\u7f6e\u554f\u984c\uff1a<\/strong>\u67d0\u4e9bRedis\u5ba2\u6236\u7aef\u53ef\u80fd\u672a\u6b63\u78ba\u914d\u7f6e\u5b57\u7b26\u7de8\u78bc\uff0c\u9019\u4e5f\u6703\u5f71\u97ff\u6578\u64da\u7684\u6b63\u78ba\u5b58\u53d6\u3002<\/li>\n<\/ul>\n<h2 id=\"%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-wXutfvwmgB\">\u89e3\u6c7a\u65b9\u6848<\/h2>\n<p>\u70ba\u4e86\u89e3\u6c7aRedis\u4e2d\u7684\u4e2d\u6587\u4e82\u78bc\u554f\u984c\uff0c\u53ef\u4ee5\u63a1\u53d6\u4ee5\u4e0b\u5e7e\u7a2e\u65b9\u6cd5\uff1a<\/p>\n<h3 id=\"1-%e7%a2%ba%e4%bf%9d%e4%bd%bf%e7%94%a8utf-8%e7%b7%a8%e7%a2%bc-wXutfvwmgB\">1. \u78ba\u4fdd\u4f7f\u7528UTF-8\u7de8\u78bc<\/h3>\n<p>\u5728\u5b58\u5132\u4e2d\u6587\u6578\u64da\u4e4b\u524d\uff0c\u9996\u5148\u8981\u78ba\u4fdd\u6578\u64da\u662f\u4ee5UTF-8\u7de8\u78bc\u7684\u3002\u4ee5\u4e0b\u662fPython\u7684\u793a\u4f8b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230Redis\nr = redis.StrictRedis(host='localhost', port=6379, db=0)\n\n# \u78ba\u4fdd\u6578\u64da\u662fUTF-8\u7de8\u78bc\nchinese_text = \"\u4f60\u597d\uff0c\u4e16\u754c\uff01\"\nr.set('greeting', chinese_text.encode('utf-8'))\n<\/code><\/pre>\n<h3 id=\"2-%e6%aa%a2%e6%9f%a5%e5%ae%a2%e6%88%b6%e7%ab%af%e7%9a%84%e7%b7%a8%e7%a2%bc%e8%a8%ad%e7%bd%ae-wXutfvwmgB\">2. \u6aa2\u67e5\u5ba2\u6236\u7aef\u7684\u7de8\u78bc\u8a2d\u7f6e<\/h3>\n<p>\u5728\u4f7f\u7528Redis\u5ba2\u6236\u7aef\u6642\uff0c\u78ba\u4fdd\u5ba2\u6236\u7aef\u7684\u7de8\u78bc\u8a2d\u7f6e\u70baUTF-8\u3002\u4f8b\u5982\uff0c\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u9019\u6a23\u8a2d\u7f6e\uff1a<\/p>\n<pre><code>import redis.clients.jedis.Jedis;\n\nJedis jedis = new Jedis(\"localhost\");\njedis.set(\"greeting\", \"\u4f60\u597d\uff0c\u4e16\u754c\uff01\".getBytes(\"UTF-8\"));\n<\/code><\/pre>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8%e6%ad%a3%e7%a2%ba%e7%9a%84%e6%95%b8%e6%93%9a%e9%a1%9e%e5%9e%8b-wXutfvwmgB\">3. \u4f7f\u7528\u6b63\u78ba\u7684\u6578\u64da\u985e\u578b<\/h3>\n<p>\u5728Redis\u4e2d\uff0c\u78ba\u4fdd\u4f7f\u7528\u6b63\u78ba\u7684\u6578\u64da\u985e\u578b\u4f86\u5b58\u5132\u4e2d\u6587\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u985e\u578b\u4f86\u5b58\u5132\u6587\u672c\u6578\u64da\uff0c\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u56e0\u6578\u64da\u985e\u578b\u4e0d\u5339\u914d\u800c\u5c0e\u81f4\u7684\u4e82\u78bc\u554f\u984c\u3002<\/p>\n<h2 id=\"%e6%b8%ac%e8%a9%a6%e8%88%87%e9%a9%97%e8%ad%89-wXutfvwmgB\">\u6e2c\u8a66\u8207\u9a57\u8b49<\/h2>\n<p>\u5728\u5be6\u65bd\u4e0a\u8ff0\u89e3\u6c7a\u65b9\u6848\u5f8c\uff0c\u5efa\u8b70\u9032\u884c\u6e2c\u8a66\u4ee5\u78ba\u4fdd\u4e2d\u6587\u6578\u64da\u80fd\u5920\u6b63\u78ba\u5b58\u53d6\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u6aa2\u67e5\u5b58\u5132\u7684\u6578\u64da\uff1a<\/p>\n<pre><code># \u53d6\u51fa\u6578\u64da\u4e26\u89e3\u78bc\nretrieved_text = r.get('greeting').decode('utf-8')\nprint(retrieved_text)  # \u61c9\u8a72\u8f38\u51fa\uff1a\u4f60\u597d\uff0c\u4e16\u754c\uff01\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-wXutfvwmgB\">\u7e3d\u7d50<\/h2>\n<p>\u89e3\u6c7aRedis\u4e2d\u7684\u4e2d\u6587\u4e82\u78bc\u554f\u984c\u4e3b\u8981\u4f9d\u8cf4\u65bc\u6b63\u78ba\u7684\u7de8\u78bc\u548c\u5ba2\u6236\u7aef\u914d\u7f6e\u3002\u901a\u904e\u78ba\u4fdd\u4f7f\u7528UTF-8\u7de8\u78bc\u3001\u6aa2\u67e5\u5ba2\u6236\u7aef\u8a2d\u7f6e\u4ee5\u53ca\u4f7f\u7528\u6b63\u78ba\u7684\u6578\u64da\u985e\u578b\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u4e82\u78bc\u73fe\u8c61\u7684\u767c\u751f\u3002\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u4e2d\u6587\u6578\u64da\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c\u9019\u4e9b\u63aa\u65bd\u5c24\u70ba\u91cd\u8981\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u904b\u884c\u60a8\u7684Redis\u670d\u52d9\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u7ba1\u7406\u548c\u5b58\u5132\u6578\u64da\uff0c\u7121\u8ad6\u662f\u4e2d\u6587\u9084\u662f\u5176\u4ed6\u8a9e\u8a00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u6c7aRedis\u4e2d\u6587\u4e82\u78bc\u554f\u984c\uff0c\u5b78\u7fd2\u5982\u4f55\u6b63\u78ba\u5b58\u5165\u548c\u8b80\u53d6\u4e2d\u6587\u6578\u64da\uff0c\u78ba\u4fdd\u6578\u64da\u5b8c\u6574\u6027\u8207\u53ef\u8b80\u6027\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-163667","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/163667","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=163667"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/163667\/revisions"}],"predecessor-version":[{"id":163668,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/163667\/revisions\/163668"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=163667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=163667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=163667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}