Home > Database > Redis > body text

How to connect golang redis client

WBOY
Release: 2023-05-29 09:14:08
forward
752 people have browsed it

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    }  }
Copy after login


#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
Copy after login

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 format (type) of the command sent by the client: 5 types

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 type

2. 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 ":" colon

Format:: Number \r\n

eg: ":1000\r\n"

4. Large string type Bulk Strings, starting with the "$" dollar sign , length limit 512M

Format: $ 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" null

5. Array type Arrays, starting with "*" asterisk

Format: *Number of array elements \r\n All other types (no need for \r\n at the end)

Note: Only the \r\n after the number of elements belongs to the array, The \r\n at the end is usually the

of the element eg: "*0\r\n" Empty array

"*2\r\n$2\r\nfoo\r\n $3\r\nbar\r\n" The array contains 2 elements, which are the string foo and bar

  "*3\r\n:1\r\n:2\r\n:3 \r\n" " The array contains 3 integers: 1, 2, 3

"*5\r\n:1\r\n:2\r\n:3\r\n:4\ r\n$6\r\nfoobar\r\n" Array containing mixed types

"*-1\r\n" "Null array

"*2\r\n*3 \r\n:1\r\n:2\r\n:3\r\n*2\r\n Foo\r\n-Bar\r\n" Nested arrays, the outer array contains 2 Arrays, after sorting as follows:

"*2\r\n

   *3\r\n:1\r\n:2\r\n:3\r\n

   *2\r\n Foo\r\n-Bar\r\n"

The 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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template