Background of 0.

The Redis API is very simple and easy to program. The process is connected using a command line tool, or in your preferred language. This article describes connecting using the Go language through the Redigo library.

1. Redigo is introduced

Redigo is a client framework that connects to the Redis database

Github address: github.com/gomodule/re…

It is the recommended framework on the Official Redis website.

2. Basic operation examples

2.1 Importing Packages Import:

  import "github.com/gomodule/redigo/redis"
Copy the code

2.2 the connection

The Conn interface is the primary interface using Redis. The application creates the connection by calling the Dial function. When finished, the Connection Close method must be called.

Dial function

c, err := redis.Dial("tcp", ":6379") if err ! = nil { // handle error } defer c.Close()Copy the code

2.3 the Do function

Do is very generic, it’s used to execute instructions.

_, err = conn.Do("Set", "name", "tomjerry") if err ! = nil { fmt.Println("set err=", err) return } r, err := redis.String(conn.Do("Get", "name")) if err ! = nil { fmt.Println("set err=", err) return }Copy the code

2.4 Manipulating the String structure in REIDS

/ / operating c.D string structure in the reids o (" SET ", "hello", "world") s, _ : = redis. String (c.D o (" GET ", "hello")) FMT) Printf (" % # v \ n ", s)Copy the code

2.5 Operating the Hash Structure

M := map[string]string{"title": "Example2", "author": "Steve", "body": "Map", } if _, err := c.Do("HMSET", redis.Args{}.Add("id2").AddFlat(m)...) ; err ! = nil { fmt.Println(err) return }Copy the code

2.6 the pipe

c.Send("SET", "foo", "bar")
c.Send("GET", "foo")
c.Flush()
c.Receive() // reply from SET
v, err = c.Receive() // reply from GET
Copy the code

Publish and Subscribe

Implement the publish/subscribe server using the Send, Flush, and Receive methods.

c.Send("SUBSCRIBE", "example") c.Flush() for { reply, err := c.Receive() if err ! = nil { return err } // process pushed message }Copy the code

2.7 Some auxiliary methods for handling responses

You can use the redis.Bool, redis.Int, redis.Bytes, redis.String, redis.Strings, and redis.Values functions to help convert the response content to a specific type of value.

exists, err := redis.Bool(c.Do("EXISTS", "foo")) if err ! = nil { // handle error return from c.Do or type conversion error. }Copy the code

The Scan function converts the returned array elements to the data type of Go

var value1 int var value2 string reply, err := redis.Values(c.Do("MGET", "key1", "key2")) if err ! = nil { // handle error } if _, err := redis.Scan(reply, &value1, &value2); err ! = nil { // handle error } ``` # 3. The application calls the Get method to Get a connection from the Pool, and calls the Close method of the connection to return the connected resource to the Pool. Connection pooling is a common way to establish connections. ``` func newPool(addr string, password string) *redis.Pool { return &redis.Pool{ MaxIdle: 3, IdleTimeout: 240 * time.Second, // Dial or DialContext must be set. When both are set, DialContext takes precedence over Dial. Dial: func() (redis.Conn, error) { return redis.Dial("tcp", addr, redis.DialPassword(password)) }, } } var ( pool *redis.Pool ) func initPool() { redisServer := "dev.com:6379" password := "123456" pool = NewPool (redisServer, password) // defer c := pool.get () defer c.close () "world") } ``` # 4. Reference [https://github.com/gomodule/redigo] (https://github.com/gomodule/redigo) [https://godoc.org/github.com/gomodule/redigo/redis#pkg-examples](https://godoc.org/github.com/gomodule/redigo/redis#pkg -examples) ENDCopy the code