Implemented the redis client, including a connection pool and redis pipleline
conn.go
func (c *conn) Do(cmd string, args ...interface {}) (interface{}, error){
if cmd != "" { if err := c.writeCommand(cmd, args); err != nil { return nil, c.fatal(err) } } if err := c.bw.Flush(); err != nil { return nil, c.fatal(err) } for i := 0; i <= pending; i++ { var e error if reply, e = c.readReply(); e != nil { return nil, c.fatal(e) } if e, ok := reply.(Error); ok && err == nil { err = e } }
}
#The method encapsulates the three processes of a request: Send, Flush and Receive
1, send writes the request to the output buffer
2, Flush sends the buffer command to the server
3, Receive receives the response from the server
https://godoc.org/github.com/ gomodule/redigo/redis#hdr-Pipelining
// conn is the low-level implementation of Conn
Because redis is a text protocol, it needs to be serialized according to the redis protocol when sending, and deserialized according to the redis protocol when receiving.
The interval symbol is \r\n under Linux and \n
# under Windows. ##1. Simple Strings, starting with " "plus sign Format: String\r\n String cannot contain CR or LF (line breaks are not allowed) eg: " OK\r\n" Note: In order to send binary-safe strings, it is generally recommended to use the following Bulk Strings type2. Errors, with " -"Begins with a minus sign Format: - Error prefix Error message \r\n Error message cannot contain CR or LF (line breaks are not allowed), Errors are very similar to Simple Strings, different Erros will be treated as an exception eg: "-Error unknow command 'foobar'\r\n"3. Integer type Integer, starting with ":" colonFormat:: Number \r\n eg: ":1000\r\n"4. Large string type Bulk Strings, starting with the "$" dollar sign , length limit 512MFormat: $ String length \r\n String \r\n String cannot contain CR or LF (line breaks are not allowed); eg: "$6\r\nfoobar\r\n" where the string is foobar, and 6 is the character length of foobar
"$0 \r\n\r\n" " Empty string "$-1\r\n" "$-1\r\n" null5. Array type Arrays, starting with "*" asteriskThe above is the detailed content of How to connect golang redis client. For more information, please follow other related articles on the PHP Chinese website!