Currently, redis is very red and red, and many application scenarios are suitable for using reids for caching or direct storage, such as MySQL front-end caching and ranking in mobile games. So how can we use golang to operate redis?
Familiar with redis, the first response is to implement communication between a client and the redis service according to the redis protocol. If you are not familiar with redis, you may say that the official c client is encapsulated with CGO. Yes, both methods are acceptable. Since redis is so popular, is there anyone doing this? The answer is yes. There are many golang clients in the client list on the official redis website. At this time, you may have to fight again. Which one should I use?
Anyone familiar with reids knows that clients on the official website and stars are good clients, just like stars in the sky. However, golang is different from Python in that both of them add stars. What is true or false?
I also know the specifics, but I probably browsed the source code. Both of them use golang to implement the redis protocol, but the source code of Radix is not so clear, the source code of redigo can be relatively clear to the command, and redigo says it supports all redis commands. Then I searched several articles on the Internet: 1/Article 2. Finally I chose redigo to try again.
1. Establish a connection
conn , err := redis.DialTimeout("tcp", ":6379", 0, 1*time.Second, 1*time.Second)
The parameters indicate the network type "TCP", address and port, connection timeout, read timeout, and write timeout respectively. After a connection is established. We can perform other operations. Check the size of the database.
Size, err: = conn. Do ("dbsize") FMT. printf ("size is % d \ n", size) // output: size is 8
After useconn.Close()
Close the connection.
2. Basic Command Execution
For the most basic commands, we call them in a unified manner:
Do(commandName string, args ...interface{}) (reply interface{}, err error)
This interface is the same as the redis command.
We know that the redis protocol is based on the slave stream. How is the do function serialized? The conversion rules are as follows:
Go Type Conversion[]byte Sent as isstring Sent as isint, int64 strconv.FormatInt(v)float64 strconv.FormatFloat(v, ‘g‘, -1, 64)bool true -> "1", false -> "0"nil ""all other types fmt.Print(v)
In fact, the byte array and string remain unchanged. The integer and floating point number are converted to the corresponding string. bool is expressed as 1 or 0, and nil is a null string.
Next, let's look at the type of returned results after execution:
Redis type Go typeerror redis.Errorinteger int64simple string stringbulk string []byte or nil if value not present.array []interface{} or nil if value not present.
As shown in the preceding table, the type in redis is converted to the type in go on the left, which does not need to be explained. Let's look at several examples:
conn , err := redis.DialTimeout("tcp", ":6379", 0, 1*time.Second, 1*time.Second)if err != nil { panic(err)}size ,err:= conn.Do("DBSIZE")fmt.Printf("size is %d \n",size)_,err = conn.Do("SET","user:user0",123)_,err = conn.Do("SET","user:user1",456)_,err = conn.Do("APPEND","user:user0",87)user0,err := redis.Int(conn.Do("GET","user:user0"))user1,err := redis.Int(conn.Do("GET","user:user1"))fmt.Printf("user0 is %d , user1 is %d \n",user0,user1)conn.Close()
Common objects (integer, string, and floating point number) are returned from redis ). Redis provides a type conversion function for conversion:
func Bool(reply interface{}, err error) (bool, error)func Bytes(reply interface{}, err error) ([]byte, error)func Float64(reply interface{}, err error) (float64, error)func Int(reply interface{}, err error) (int, error)func Int64(reply interface{}, err error) (int64, error)func String(reply interface{}, err error) (string, error)func Strings(reply interface{}, err error) ([]string, error)func Uint64(reply interface{}, err error) (uint64, error)
Here we only use the set and get commands. For other examples, see conn_test.go of redigo.