{"id":202361,"date":"2025-05-10T15:01:45","date_gmt":"2025-05-10T07:01:45","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202361\/"},"modified":"2025-05-10T15:01:45","modified_gmt":"2025-05-10T07:01:45","slug":"redis-resp%e5%8d%8f%e8%ae%ae%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202361\/","title":{"rendered":"Redis\u00a0RESP\u534f\u8bae\u5982\u4f55\u5b9e\u73b0"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis&nbsp;RESP\u534f\u8bae\u5982\u4f55\u5b9e\u73b0<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300aRedis&nbsp;RESP\u534f\u8bae\u5982\u4f55\u5b9e\u73b0\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<\/p>\n<h4><strong>\u56de\u987eRESP\u534f\u8bae<\/strong><\/h4>\n<p><code>RESP<\/code>\u662f\u57fa\u4e8e<code>TCP<\/code>\u6765\u5b9e\u73b0\u7684<code>Redis<\/code>\u901a\u4fe1\u534f\u8bae\uff0c\u8be5\u534f\u8bae\u662f\u4ee5<code>\/r\/n<\/code>(\u884c)\u8fdb\u884c\u5206\u5272\u7684\uff0c\u534f\u8bae\u652f\u63015\u79cd\u7c7b\u578b\uff0c\u5177\u4f53\u4fe1\u606f\u5982\u4e0b:<\/p>\n<table>\n<tbody>\n<tr>\n<th>\u7c7b\u578b<\/th>\n<th>\u524d\u7f00<\/th>\n<th>\u5907\u6ce8<\/th>\n<\/tr>\n<tr>\n<td>\u7b80\u5355\u5b57\u7b26\u4e32<\/td>\n<td>+<\/td>\n<td>\u7b80\u5355\u5b57\u7b26\u4e32\u4ee5+\u5f00\u5934<\/td>\n<\/tr>\n<tr>\n<td>\u9519\u8bef\u6570\u636e<\/td>\n<td>&#8211;<\/td>\n<td>\u9519\u8bef\u6570\u636e\u4ee5-\u5f00\u5934<\/td>\n<\/tr>\n<tr>\n<td>\u6574\u6570<\/td>\n<td>:<\/td>\n<td>\u6574\u6570\u4ee5:\u5f00\u5934<\/td>\n<\/tr>\n<tr>\n<td>\u590d\u6742\u5b57\u7b26\u4e32<\/td>\n<td>$<\/td>\n<td>\u590d\u6742\u5b57\u7b26\u4e32\u4ee5$\u5f00\u5934<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u7ec4<\/td>\n<td>*<\/td>\n<td>\u6570\u7ec4\u4ee5*\u5f00\u5934<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5373\uff0c\u6211\u4eec\u5411<code>redis<\/code>\u53d1\u9001\u547d\u4ee4:<code>set name pdudo<\/code>\uff0c\u5176\u5b9e\u53d1\u9001\u7684\u5177\u4f53\u4fe1\u606f\u662f<\/p>\n<pre>*3\n$3\nset\n$4\nname\n$5\npdudo<\/pre>\n<p>\u800c\u670d\u52a1\u5668\u8fd4\u56de\u7684\u4fe1\u606f\u4e5f\u662f\u7c7b\u4f3c\u7684\uff0c\u53ea\u4e0d\u8fc7\u8fd8\u9700\u8981\u4e86\u89e3<code>+<\/code>\u548c<code>-<\/code>\uff0c\u8fd92\u4e2a\u524d\u7f00\u5206\u522b\u4ee3\u8868\u6b63\u786e\u6d88\u606f\u548c\u9519\u8bef\u7684\u6d88\u606f\u3002<\/p>\n<p>\u6211\u4eec\u51c6\u59072\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u6765\u6572\u4e00\u4e0b<\/p>\n<p>\u4f8b\u5b501<\/p>\n<pre>set&nbsp;name&nbsp;pdudo<\/pre>\n<p>\u4f8b\u5b502<\/p>\n<pre>lpush&nbsp;pdudo&nbsp;data1\nlpush&nbsp;pdudo&nbsp;data2\nlrange&nbsp;pdudo&nbsp;0&nbsp;-1<\/pre>\n<p>\u5feb\u6765\u52a8\u52a8\u4f60\u7684\u5c0f\u624b\u6307\uff0c\u770b\u80fd\u4e0d\u80fd\u6839\u636e<code>RESP<\/code>\u534f\u8bae\u89c4\u5219\uff0c\u5c06\u4e0a\u8ff0\u4f8b\u5b50\u547d\u4ee4\u6572\u51fa\u6765\u3002\u73b0\u5728\u4f60\u4f53\u4f1a\u5230\u4e86<code>Redis<\/code>\u5b98\u7f51\u4ecb\u7ecd<code>RESP<\/code>\u534f\u8bae\u65f6\u6240\u8ff0\u7684 <strong>\u7b80\u5355<\/strong> \u548c <strong>\u6613\u8bfb<\/strong> \u53ef\u4e48\uff1f<\/p>\n<p>\u5bf9\u4e8e<code>RESP<\/code>\u6765\u8bf4\uff0c\u4e00\u5b9a\u8981\u641e\u6e05\u695a\u534f\u8bae\u540e\uff0c\u6700\u597d\u80fd\u591f\u624b\u5199\u534f\u8bae\u53bb\u6267\u884c\uff0c\u518d\u8003\u8651\u5199\u7a0b\u5e8f\u53bb\u5b9e\u73b0\u534f\u8bae\uff01\uff01\uff01<\/p>\n<h4>\u5982\u4f55\u62c6\u89e3RESP\u534f\u8bae<\/h4>\n<p>\u7ec8\u4e8e\u5230\u4e86\u559c\u95fb\u4e50\u89c1\u7684\u73af\u8282\u4e86\uff0c\u6211\u4eec\u8981\u62c6\u89e3\u548c\u7ec4\u88c5\u534f\u8bae\u4e86\u3002 \u90a3\u6211\u4eec\u81f3\u5c11\u6765\u89e3\u51b3\u5982\u4e0b3\u4e2a\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\n<p>\u8be5\u534f\u8bae\u662f\u57fa\u4e8e<code>TCP<\/code>\u6d41\u7684\uff0c\u6211\u4eec\u5982\u4f55\u5224\u65ad\u6574\u4e2a\u547d\u4ee4\u4ec0\u4e48\u65f6\u5019\u7ed3\u675f\uff1f<\/p>\n<\/li>\n<li>\n<p>\u5982\u4f55\u62c6\u89e3\u547d\u4ee4\uff1f<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u534f\u8bae\u4ec0\u4e48\u65f6\u5019\u7ed3\u675f<\/strong><\/p>\n<p>\u4e00\u822c\u800c\u8a00\uff0c\u6211\u4eec\u81ea\u5df1\u5728\u4f7f\u7528<code>TCP<\/code>\u4f20\u8f93\u6570\u636e\uff0c\u90fd\u4f1a\u5728\u6570\u636e\u5f00\u5934\u5b9a\u4e492\u4e2a\u6216\u80054\u4e2a\u5b57\u8282\uff0c\u7528\u4e8e\u5b58\u50a8\u8be5\u6570\u636e\u6709\u591a\u5c11\u4e2a\u5b57\u8282\uff0c\u8fd9\u6837\u65b9\u4fbf\u68c0\u9a8c\u63a5\u6536\uff0c\u7c7b\u4f3c\u4e8e\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240423\/17138583316627671b9899f.jpg\" class=\"aligncenter\"><\/p>\n<p>\u800c<code>RESP<\/code>\u6709\u610f\u601d\u4e86\uff0c\u5b83\u662f\u4ee5<code>\/r\/n<\/code>\u6765\u5206\u5272\u7684\u3002\u6700\u524d\u9762\u4f1a\u4ee5\u524d\u7f00\u6765\u5224\u65ad\u5176\u7c7b\u578b\uff0c\u4f8b\u5982\u6211\u4eec\u53d1\u9001\u547d\u4ee4\uff0c\u5176\u4f1a\u7528\u5230\u7684\u524d\u7f00\u6709<code>*<\/code>\u4ee5\u53ca<code>$<\/code>\uff0c\u90a3\u4e48\u6211\u4eec\u5982\u4f55\u6765\u5224\u65ad\uff0c\u6211\u4eec\u8981\u8bfb\u53d6\u591a\u5c11\u4e2a<code>\/r\/n<\/code>\u5462\uff1f<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240423\/17138583316627671b9ba9e.jpg\" class=\"aligncenter\"><\/p>\n<p>\u56e0\u4e3a\u4e0a\u8ff0<code>*<\/code>\u4ee3\u8868\u6570\u7ec4\uff0c\u5373\u6709\u591a\u5c11\u7ec4\u6570\u636e\u9700\u8981\u5904\u7406\uff0c\u56fe\u4e2d\u4e3a<code>n<\/code>\u3002<\/p>\n<p>\u800c<code>$<\/code>\u8868\u793a\u590d\u6742\u5b57\u7b26\u4e32\uff0c\u5373\u9700\u8981\u83b7\u53d6<code>m<\/code>\u4e2a\u5b57\u7b26\u6570\u636e\uff0c\u4e0d\u5305\u542b<code>\/r\/n<\/code><\/p>\n<p><strong>\u5982\u4f55\u62c6\u89e3<\/strong><code>RESP<\/code><strong>\u534f\u8bae<\/strong><\/p>\n<p>\u82e5\u8981\u62c6\u89e3\u547d\u4ee4\uff0c\u5219\u6211\u4eec\u5f97\u83b7\u53d6\u547d\u4ee4\uff0c\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u62a5\u6587<code>$m<\/code>\uff0c\u5176\u5b9e\u8bb0\u5f55\u7684\u6709<code>m<\/code>\u957f\u5ea6\u7684\u6570\u636e(\u4e0d\u5305\u542b<code>\\r\\n<\/code>)\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u6765\u5199\u4f2a\u4ee3\u7801\u3002<\/p>\n<p>\u6839\u636e\u5982\u4e0a\uff0c\u6211\u4eec\u5f88\u5bb9\u6613\u5199\u51fa<strong>\u4f2a\u4ee3\u7801<\/strong>\u3002<\/p>\n<pre>func&nbsp;toArgs(rd&nbsp;*bufio.Reader)&nbsp;{\n\tdata&nbsp;,&nbsp;_&nbsp;,&nbsp;_&nbsp;:=&nbsp;rd.ReadLine()\n\tswitch&nbsp;data[0]&nbsp;{\n\tcase&nbsp;'*':\n\t\tn&nbsp;:=&nbsp;data[1:]&nbsp;\/\/&nbsp;\u5faa\u73afn\u6b21\n\t\tfor&nbsp;i:=0;i&lt;n;i++&nbsp;{\n\t\t\ttoArgs(rd)\n\t\t}\n\tcase&nbsp;'$':\n\t\tm&nbsp;:=&nbsp;data[1:]&nbsp;\/\/&nbsp;\u83b7\u53d6m\u4e2a\u6570\u636e\n\t\t\/\/&nbsp;\u83b7\u53d6m\u957f\u5ea6\u7684\u6570\u636e\u5373\u53ef\n\t}\n}<\/pre>\n<p>\u5982\u4e0a\u6211\u4eec\u5148\u83b7\u53d6\u524d\u7f00\u4e3a<code>*<\/code>\u7684\uff0c\u7ee7\u800c\u83b7\u53d6\u5176\u503c<code>n<\/code>\uff0c\u6211\u4eec\u5219\u5faa\u73af<code>n<\/code>\u6b21\uff0c\u5373\u53ef\u83b7\u53d6\u8be5\u62a5\u6587\u7684\u6570\u636e\u3002\u800c\u524d\u7f00\u4e3a<code>$<\/code>\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u8be5<code>m<\/code>\u957f\u5ea6\u7684\u6570\u636e\u5373\u53ef\uff0c\u8fd9\u91cc\u4e3b\u8981\u8981\u5904\u7406\u4e00\u4e0b<code>\\r\\n<\/code>\u3002<\/p>\n<p>\u5c06\u547d\u4ee4\u6784\u5efa<code>RESP<\/code>\u62a5\u6587\u89c4\u8303\uff0c\u6839\u636e\u62c6\u89e3\u53cd\u64cd\u4f5c\u5c31\u53ef\u4ee5\u4e86\uff0c\u8fd9\u91cc\u6682\u4e0d\u4ecb\u7ecd\u4e86\u3002<\/p>\n<p>\u4e0a\u8ff0\uff0c\u6211\u4eec\u6838\u5fc3\u529f\u80fd\u5df2\u7ecf\u63a2\u8ba8\u5b8c\u6bd5\u4e86\u3002<\/p>\n<h4><strong>\u529f\u80fd\u5b9e\u73b0<\/strong><\/h4>\n<p>\u4ee3\u7801\u5df2\u7ecf\u7f16\u5199\u5b8c\u6bd5\uff0c\u653e\u7f6e\u5728\u4e86<code>gitee<\/code>\u4e0a: gitee<\/p>\n<p>\u5982\u4e0a\u6211\u4eec\u5df2\u7ecf\u5b66\u4f1a\u4e86\u5982\u4f55\u62c6\u89e3\u548c\u7ec4\u88c5<code>RESP<\/code>\u534f\u8bae\u4e86\uff0c\u6211\u4eec\u63a5\u7740\u6765\u770b\uff0c\u6211\u4eec\u5982\u4f55\u7528<code>go<\/code>\u6765\u7f16\u5199\u62c6\u89e3\u548c\u7ec4\u88c5\u534f\u8bae\u7684\u4ee3\u7801\u5462\uff1f \u6211\u4eec\u53ef\u4ee5\u770b\u3002<\/p>\n<p>\u6211\u4eec\u5148\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\uff0c\u7136\u540e\u5c06\u5176\u5c01\u88c5\u4e3a<code>bufio.Reader<\/code>\uff0c\u6211\u4eec\u6765\u770b\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240423\/17138583316627671b9e988.jpg\" class=\"aligncenter\"><\/p>\n<p>\u56e0\u4e3a\u6211\u4eec\u8981\u4f7f\u7528<code>readLine()<\/code>\u51fd\u6570\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a<code>bufio.Reader<\/code>\u7c7b\u578b\uff0c\u82e5\u662f\u76f4\u63a5\u4ece<code>net.Conn<\/code>\u4e2d\u83b7\u53d6\uff0c\u4e0d\u7528\u8f6c\u6362\uff0c\u76f4\u63a5\u53ef\u4ee5\u4f7f\u7528 <code>bufio.Reader<\/code>\u7684\u3002<\/p>\n<p>\u6211\u4eec\u5c06\u4e0a\u8ff0\u4f2a\u4ee3\u7801\u7f16\u5199\u4e00\u4e0b\uff0c\u5b9e\u73b0\u62c6\u89e3\u7684\u529f\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240423\/17138583316627671ba1bd5.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5176\u5177\u4f53\u6267\u884c\u8fc7\u7a0b\u662f\u6211\u4eec\u5148\u83b7\u53d6\u4e00\u884c\u6570\u636e\uff0c\u653e\u7f6e\u5230<code>data<\/code>\u4e2d\uff0c\u800c\u540e\u5224\u65ad\u5176\u524d\u7f00\u662f\u4ec0\u4e48\uff0c\u82e5\u662f<code>*<\/code>\u5219\u53d6\u5176\u540e\u9762\u7684\u6570\u636e\uff0c\u5c06\u5176\u8f6c\u4e3a<code>int<\/code>\u7c7b\u578b<code>n<\/code>\uff0c\u800c\u540e\u518d\u9012\u5f52\u8be5\u51fd\u6570<code>n<\/code>\u6b21\uff0c\u800c\u540e\u4e2d\u9047\u5230<code>$<\/code>\uff0c\u6211\u4eec\u5219\u53d6\u540e\u9762\u7684\u6570\u636e\uff0c\u4e5f\u662f\u5c06\u5176\u8f6c\u4e3a<code>int<\/code>\u7c7b\u578b<code>m<\/code>\uff0c\u800c\u540e\u518d\u53d6<code>m<\/code>\u957f\u5ea6\u7684\u5b9e\u9645\u6570\u636e\uff0c\u8fd9\u5c31\u662f\u6211\u4eec\u7684\u547d\u4ee4\u4e86\uff0c\u6700\u540e\u6211\u4eec\u518d\u8e22\u6389\u547d\u4ee4\u7684<code>\\r\\n<\/code>\u5373\u53ef\u3002<\/p>\n<p>\u5176\u4e2d\uff0c\u6709\u4e00\u4e2a\u51fd\u6570\u662f<code>byteToInt<\/code>\u662f\u6211\u4eec\u81ea\u5df1\u5199\u7684\u901a\u8fc7\u5207\u7247\u8f6c\u4e3a\u6570\u5b57\u7684\u51fd\u6570\uff0c\u6211\u4eec\u770b\u4e0b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240423\/17138583316627671ba4dff.jpg\" class=\"aligncenter\"><\/p>\n<p>\u8be5\u51fd\u6570\u4e3b\u8981\u7684\u529f\u80fd\u662f\u5c06\u5176<code>[]byte<\/code>\u6570\u5b57\u8f6c\u6362\u4e3a<code>int<\/code>\u6570\u636e\u3002<\/p>\n<p>\u5982\u4e0a\uff0c\u6211\u4eec\u6574\u4e2a<code>RESP<\/code>\u534f\u8bae\u529f\u80fd\u5199\u5b8c\u4e86\uff0c\u6211\u4eec\u8fd0\u884c\u4e0b\u770b\u4e0b\u5b9e\u9645\u6548\u679c:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240423\/17138583316627671baa00a.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5f88\u663e\u7136\uff0c\u6211\u4eec\u6210\u529f\u62c6\u89e3\u4e86\u8be5\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis&nbsp;RESP\u534f&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-202361","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202361","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202361"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202361\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}