{"id":167143,"date":"2024-11-05T22:43:29","date_gmt":"2024-11-05T14:43:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/167143\/"},"modified":"2024-11-05T22:43:30","modified_gmt":"2024-11-05T14:43:30","slug":"%e5%88%a9%e7%94%a8redis%e5%ad%98%e5%84%b2%e5%90%84%e9%a1%9e%e5%b0%8d%e8%b1%a1%e7%9a%84%e6%96%b0%e7%99%bc%e7%8f%be%ef%bc%88redis%e8%83%bd%e5%ad%98%e5%85%a5%e5%b0%8d%e8%b1%a1%e5%98%9b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/167143\/","title":{"rendered":"\u5229\u7528Redis\u5b58\u5132\u5404\u985e\u5c0d\u8c61\u7684\u65b0\u767c\u73fe\uff08redis\u80fd\u5b58\u5165\u5c0d\u8c61\u561b\uff09"},"content":{"rendered":"<h1 id=\"%e5%88%a9%e7%94%a8redis%e5%ad%98%e5%84%b2%e5%90%84%e9%a1%9e%e5%b0%8d%e8%b1%a1%e7%9a%84%e6%96%b0%e7%99%bc%e7%8f%be%ef%bc%88redis%e8%83%bd%e5%ad%98%e5%85%a5%e5%b0%8d%e8%b1%a1%e5%98%9b%ef%bc%89-kFlSwGlpLK\">\u5229\u7528Redis\u5b58\u5132\u5404\u985e\u5c0d\u8c61\u7684\u65b0\u767c\u73fe\uff08redis\u80fd\u5b58\u5165\u5c0d\u8c61\u561b\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u5feb\u901f\u4e14\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6027\u80fd\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u56e0\u5176\u5feb\u901f\u7684\u6578\u64da\u8b80\u53d6\u548c\u5beb\u5165\u80fd\u529b\u800c\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002\u8a31\u591a\u958b\u767c\u8005\u548c\u4f01\u696d\u958b\u59cb\u63a2\u7d22Redis\u7684\u6f5b\u529b\uff0c\u5c24\u5176\u662f\u5728\u5b58\u5132\u5404\u985e\u5c0d\u8c61\u65b9\u9762\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eRedis\u80fd\u5426\u5b58\u5132\u5c0d\u8c61\uff0c\u4ee5\u53ca\u5982\u4f55\u6709\u6548\u5730\u5229\u7528Redis\u4f86\u7ba1\u7406\u548c\u5b58\u5132\u5404\u985e\u6578\u64da\u5c0d\u8c61\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-kFlSwGlpLK\">Redis\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u958b\u6e90\u7684\u5167\u5b58\u6578\u64da\u7d50\u69cb\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\u5b83\u7684\u9ad8\u6027\u80fd\u7279\u6027\u4f7f\u5176\u6210\u70ba\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u5be6\u6642\u6578\u64da\u5206\u6790\u7b49\u61c9\u7528\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"redis%e8%83%bd%e5%90%a6%e5%ad%98%e5%84%b2%e5%b0%8d%e8%b1%a1%ef%bc%9f-kFlSwGlpLK\">Redis\u80fd\u5426\u5b58\u5132\u5c0d\u8c61\uff1f<\/h2>\n<p>Redis\u672c\u8eab\u4e26\u4e0d\u76f4\u63a5\u652f\u6301\u5c0d\u8c61\u7684\u5b58\u5132\uff0c\u4f46\u53ef\u4ee5\u901a\u904e\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u65b9\u5f0f\u4f86\u5be6\u73fe\u3002\u9019\u610f\u5473\u8457\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5c07\u5c0d\u8c61\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\u6216\u4e8c\u9032\u5236\u683c\u5f0f\uff0c\u7136\u5f8c\u5c07\u5176\u5b58\u5132\u5728Redis\u4e2d\u3002\u7576\u9700\u8981\u4f7f\u7528\u9019\u4e9b\u5c0d\u8c61\u6642\uff0c\u518d\u5c07\u5176\u5f9eRedis\u4e2d\u8b80\u53d6\u4e26\u53cd\u5e8f\u5217\u5316\u56de\u539f\u59cb\u5c0d\u8c61\u3002<\/p>\n<h3 id=\"%e5%ba%8f%e5%88%97%e5%8c%96%e8%88%87%e5%8f%8d%e5%ba%8f%e5%88%97%e5%8c%96-kFlSwGlpLK\">\u5e8f\u5217\u5316\u8207\u53cd\u5e8f\u5217\u5316<\/h3>\n<p>\u5e8f\u5217\u5316\u662f\u5c07\u5c0d\u8c61\u8f49\u63db\u70ba\u53ef\u5b58\u5132\u6216\u50b3\u8f38\u7684\u683c\u5f0f\u7684\u904e\u7a0b\uff0c\u800c\u53cd\u5e8f\u5217\u5316\u5247\u662f\u5c07\u5b58\u5132\u7684\u6578\u64da\u8f49\u63db\u56de\u5c0d\u8c61\u7684\u904e\u7a0b\u3002\u5728Redis\u4e2d\uff0c\u5e38\u7528\u7684\u5e8f\u5217\u5316\u683c\u5f0f\u5305\u62ecJSON\u3001MessagePack\u548cProtocol Buffers\u7b49\u3002<\/p>\n<h4 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e4%bd%bf%e7%94%a8json%e5%ba%8f%e5%88%97%e5%8c%96%e5%b0%8d%e8%b1%a1-kFlSwGlpLK\">\u793a\u4f8b\uff1a\u4f7f\u7528JSON\u5e8f\u5217\u5316\u5c0d\u8c61<\/h4>\n<pre><code>const redis = require('redis');\nconst client = redis.createClient();\nconst user = {\n    id: 1,\n    name: 'Alice',\n    age: 30\n};\n\n\/\/ \u5c07\u5c0d\u8c61\u5e8f\u5217\u5316\u70baJSON\u5b57\u7b26\u4e32\nconst userJson = JSON.stringify(user);\n\n\/\/ \u5b58\u5132\u5230Redis\nclient.set('user:1', userJson, (err, reply) =&gt; {\n    if (err) throw err;\n    console.log(reply); \/\/ OK\n});\n\n\/\/ \u5f9eRedis\u4e2d\u8b80\u53d6\u4e26\u53cd\u5e8f\u5217\u5316\nclient.get('user:1', (err, reply) =&gt; {\n    if (err) throw err;\n    const userObj = JSON.parse(reply);\n    console.log(userObj); \/\/ { id: 1, name: 'Alice', age: 30 }\n});\n<\/code><\/pre>\n<h2 id=\"redis%e7%9a%84%e6%95%b8%e6%93%9a%e7%b5%90%e6%a7%8b%e9%81%b8%e6%93%87-kFlSwGlpLK\">Redis\u7684\u6578\u64da\u7d50\u69cb\u9078\u64c7<\/h2>\n<p>\u5728\u5b58\u5132\u5c0d\u8c61\u6642\uff0c\u9078\u64c7\u5408\u9069\u7684Redis\u6578\u64da\u7d50\u69cb\u4e5f\u975e\u5e38\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u5e7e\u7a2e\u5e38\u898b\u7684\u6578\u64da\u7d50\u69cb\u53ca\u5176\u9069\u7528\u5834\u666f\uff1a<\/p>\n<ul>\n<li><strong>\u5b57\u7b26\u4e32\uff1a<\/strong>\u9069\u5408\u5b58\u5132\u7c21\u55ae\u7684\u5c0d\u8c61\u6216\u6578\u64da\uff0c\u5982\u7528\u6236\u540d\u3001\u96fb\u5b50\u90f5\u4ef6\u7b49\u3002<\/li>\n<li><strong>\u54c8\u5e0c\uff1a<\/strong>\u9069\u5408\u5b58\u5132\u5177\u6709\u591a\u500b\u5c6c\u6027\u7684\u5c0d\u8c61\uff0c\u5982\u7528\u6236\u8cc7\u6599\uff0c\u53ef\u4ee5\u5c07\u6bcf\u500b\u5c6c\u6027\u4f5c\u70ba\u54c8\u5e0c\u7684\u5b57\u6bb5\u3002<\/li>\n<li><strong>\u5217\u8868\uff1a<\/strong>\u9069\u5408\u5b58\u5132\u6709\u5e8f\u7684\u6578\u64da\u96c6\u5408\uff0c\u5982\u6d88\u606f\u968a\u5217\u6216\u7528\u6236\u6d3b\u52d5\u8a18\u9304\u3002<\/li>\n<li><strong>\u96c6\u5408\uff1a<\/strong>\u9069\u5408\u5b58\u5132\u552f\u4e00\u7684\u6578\u64da\uff0c\u5982\u7528\u6236\u6a19\u7c64\u6216\u8208\u8da3\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e7%9a%84%e5%84%aa%e5%8b%a2-kFlSwGlpLK\">Redis\u7684\u512a\u52e2<\/h2>\n<p>\u4f7f\u7528Redis\u5b58\u5132\u5c0d\u8c61\u6709\u591a\u500b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u7684\u5167\u5b58\u5b58\u5132\u7279\u6027\u4f7f\u5f97\u6578\u64da\u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u975e\u5e38\u5feb\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\uff1a<\/strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u80fd\u5920\u6eff\u8db3\u4e0d\u540c\u7684\u5b58\u5132\u9700\u6c42\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>Redis\u63d0\u4f9b\u4e86\u591a\u7a2e\u6301\u4e45\u5316\u6a5f\u5236\uff0c\u80fd\u5920\u5728\u7cfb\u7d71\u91cd\u555f\u5f8c\u6062\u5fa9\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-kFlSwGlpLK\">\u7d50\u8ad6<\/h2>\n<p>Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u96d6\u7136\u4e0d\u76f4\u63a5\u652f\u6301\u5c0d\u8c61\u7684\u5b58\u5132\uff0c\u4f46\u901a\u904e\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u65b9\u5f0f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9748\u6d3b\u5730\u5c07\u5404\u985e\u5c0d\u8c61\u5b58\u5132\u5728Redis\u4e2d\u3002\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u7d50\u69cb\u548c\u5e8f\u5217\u5316\u683c\u5f0f\uff0c\u53ef\u4ee5\u9032\u4e00\u6b65\u63d0\u5347\u6578\u64da\u5b58\u5132\u7684\u6548\u7387\u548c\u9748\u6d3b\u6027\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6027\u80fd\u6578\u64da\u5b58\u5132\u7684\u61c9\u7528\uff0cRedis\u7121\u7591\u662f\u4e00\u500b\u503c\u5f97\u8003\u616e\u7684\u9078\u64c7\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u548c\u5176\u4ed6\u670d\u52d9\u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Redis\u5b58\u5132\u5404\u985e\u5c0d\u8c61\u7684\u65b0\u767c\u73fe\uff0c\u4e86\u89e3\u5982\u4f55\u6709\u6548\u5730\u5c07\u5c0d\u8c61\u6578\u64da\u5b58\u5165Redis\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\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-167143","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167143","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=167143"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167143\/revisions"}],"predecessor-version":[{"id":167144,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167143\/revisions\/167144"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=167143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=167143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=167143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}