关于通讯协议,见https://redis.readthedocs.org/en/latest/topic/protocol.html
1)命令 set mykey myvalue 对应 要发送到Redis的字符串(要转化为二进制数据)是
"*3\r\n$3\r\nset\r\n$5\r\nmykey\r\n$7\r\nmyvalue\r\n"
2)命令 get mykey 对应字符串是 "*2\r\n$3\r\nget\r\n$5\r\nmykey\r\n"
3)最后得到Redis发回的响应是 "+OK\r\n$7\r\nmyvalue\r\n"
我的问题是,Redis这样的响应格式,是否意味者 客户端发完命令(需要得到返回
值的命令如get)后,必须要等待回应到达之后才能发送下一个命令? 这样对客户端来说 效率是否低了点?
This is the normal TCP streaming method.
Your "low efficiency for the client" is just your fantasy, right? Is there any data to support this? Without any data to support it, can it be judged correctly based on subjective feelings?
There is also the relationship between efficiency and business. Generally speaking, you will only start to consider efficiency issues when efficiency does not support business. Isn’t it a bit premature for you to consider this efficiency before you have done anything?
One final addition. . This is the definition in the tcp protocol. If there is anything unclear, http://en.wikipedia.org/wiki/Transmission_Control_Protocol go here to learn about the tcp specification. .
Maybe this question becomes: Can redis only send one command in one request? will be better
Conclusion: No matter which redis request protocol is used, sending multiple commands in one request is supported
redis request protocol
Non-standard redis request protocol format (also known as Inline):
Standard redis request protocol format:
So when we need to set the value of the key name to diaocow, we can use the above two protocol formats to complete it (interested readers can use the telnet or nc command to test it themselves)
How does the Redis request protocol support batch execution? (This is what everyone often calls pipeline mode)
For example, I need to execute the following two commands in batches:
set name diaocow
set country china
Then the data in a request will look like this (assuming we use the standard protocol): *3rn$3rnsetrn$4rnnamern$7rndiaocowrn*3rn$3rnsetrn$7rncountryrn$5rnchinarn
Then inside redis, it will be parsed like this, pseudo code:
parseCommandInfo will be parsed according to different protocols: if the first byte is '*', the standard protocol will be used
Remarks: All redis clients implement the function of sending commands in batches, which is nothing more than sending multiple commands to redis according to the format we just mentioned. Interested readers can refer to the client in the language they are familiar with