Golang study notes golang operation Redis

Reids 
Install import
go get github.com/garyburd/redigo/redis
import "github.com/garyburd/redigo/redis"
use
import "github.com/garyburd/redigo/redis"
func main() {
    c, err := redis.Dial("tcp", "localhost:6379")
    if err != nil {
        fmt.Println("conn redis failed, err:", err)
        return
    }
    defer c.Close()
}

set & get

        _, err = c.Do("Set", "name", "nick")
    if err != nil {
        fmt.Println(err)
        return
    }
    r, err := redis.String(c.Do("Get", "name"))
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(r)

mset & mget

       _, err = c.Do("MSet", "name", "nick", "age", "18")
    if err != nil {
        fmt.Println("MSet error: ", err)
        return
    }
    r2, err := redis.Strings(c.Do("MGet", "name", "age"))
    if err != nil {
        fmt.Println("MGet error: ", err)
        return
    }
    fmt.Println(r2)

hset & hget

  _, err = c.Do("HSet", "names", "nick", "suoning")
    if err != nil {
        fmt.Println("hset error: ", err)
        return
    }
    r, err = redis.String(c.Do("HGet", "names", "nick"))
    if err != nil {
        fmt.Println("hget error: ", err)
        return
    }
    fmt.Println(r)

expire

 _, err = c.Do("expire", "names", 5)
    if err != nil {
        fmt.Println("expire error: ", err)
        return
    }

lpush & lpop & llen

  _, err = c.Do("lpush", "Queue", "nick", "dawn", 9)
    if err != nil {
        fmt.Println("lpush error: ", err)
        return
    }
    for {
        r, err = redis.String(c.Do("lpop", "Queue"))
        if err != nil {
            fmt.Println("lpop error: ", err)
            break
        }
        fmt.Println(r)
    }
    r3, err := redis.Int(c.Do("llen", "Queue"))
    if err != nil {
        fmt.Println("llen error: ", err)
        return
    }
connection pool
The explanation of each parameter is as follows:
MaxIdle: The maximum number of idle connections, which means that even if there is no redis connection, N idle connections can still be maintained without being cleared, and they are on standby at any time.
MaxActive: The maximum number of active connections, indicating that there are at most N connections at the same time
IdleTimeout: The maximum idle connection waiting time, after this time, the idle connection will be closed
pool := &redis.Pool{
        MaxIdle:     16,
        MaxActive:   1024,
        IdleTimeout: 300,
        Dial: func() (redis.Conn, error) {
            return redis.Dial("tcp", "localhost:6379")
        },
    }
package main
import (
    "fmt"
    "github.com/garyburd/redigo/redis"
)
var pool *redis.Pool
func init() {
    pool = &redis.Pool{
        MaxIdle:     16,
        MaxActive:   1024,
        IdleTimeout: 300,
        Dial: func() (redis.Conn, error) {
            return redis.Dial("tcp", "localhost:6379")
        },
    }
}
func main() {
    c := pool.Get()
    defer c.Close()
    _, err := c.Do("Set", "name", "nick")
    if err != nil {
        fmt.Println(err)
        return
    }
    r, err := redis.String(c.Do("Get", "name"))
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(r)
}
pipeline operation
The request/response service can realize continuous processing of new requests, the client can send multiple commands to the server without waiting for a response, and finally read multiple responses at once.
Use Send(), Flush(), Receive() methods to support pipeline operations
Send writes commands to the connection's output buffer.
Flush clears the connection's output buffer and writes it to the server.
Recevie reads the server's responses sequentially in FIFO order.
func main() {
    c, err := redis.Dial("tcp", "localhost:6379")
    if err != nil {
        fmt.Println("conn redis failed, err:", err)
        return
    }
    defer c.Close()
    c.Send("SET", "name1", "sss1")
    c.Send("SET", "name2", "sss2")
    c.Flush()
    v, err := c.Receive()
    fmt.Printf("v:%v,err:%v\n", v, err)
    v, err = c.Receive()
    fmt.Printf("v:%v,err:%v\n", v, err)
    v, err = c.Receive()    // 夯住,一直等待
    fmt.Printf("v:%v,err:%v\n", v, err)
}

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish