You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by GitBox <gi...@apache.org> on 2020/12/17 13:23:07 UTC

[GitHub] [incubator-brpc] cooper-zhzhang edited a comment on issue #1305: 使用Redis Channel的时候发送Redis命令的疑问

cooper-zhzhang edited a comment on issue #1305:
URL: https://github.com/apache/incubator-brpc/issues/1305#issuecomment-747419590


   `request->AddCommand("MGET %s", keys.c_str());`
   之所以错误这个和redis的序列化有关系
   
   一个简单的例子
   `set my_key 1`
   序列化后为 `$3set\r\n$6my_key\r\n$11\r\n`
   表示客户端给服务器发送三个字符串(使用`\r\n`分割) 第一个是命令 其余两个是命令的参数
   服务器收到后理解为 set 命令后面跟两个参数  一个参数是my_key一个参数是1
   
   `std::string keys = "my_key_2 my_key_1";
   request->AddCommand("MGET %s", keys.c_str());`
   这个在序列化的时候直接使用keys.c_str() 替换了%s 
   所以序列化后为 `$4\r\nmget\r\n$17\r\nmy_key_2 my_key_1\r\n`
   服务器认为收到两个字符串
   第一个是`mget` 第二个是`my_key_2 my_key_1`
   即 mget 命令跟一个参数 参数为`my_key_2 my_key_1`注意参数中的空格,
   服务器认为你要找key为`my_key_2 my_key_1`的键值对
   
   正确的用法为 `request->AddCommand("MGET %s %s", "my_key_2", "my_key_1")`
   或者
   `request->AddCommand(std::string("MGET ") + "my_key_2 my_key_1");`
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org