golang - redis connection pool

Redis of operation, can be connected through the pool by redis golang, process is as follows:

(1) a certain number of connections initialized beforehand, into the connection pool;

(2) operation when needed go redis, taken directly from the connection to the connection pool;

(3) It saves time temporary redis acquired, thereby improving the efficiency;

package main

import (
    "fmt"

    "github.com/garyburd/redigo/redis"
)

var pool *redis.Pool

func init() {
    pool = &redis.Pool{
        MaxIdle:     8,
        MaxActive:   0,
        IdleTimeout: 100,
        Dial: func() (redis.Conn, error) {
            return redis.Dial("tcp", "localhost:6379")
        },
    }
}

func main() {
    conn := pool.Get()
    defer conn.Close()

    _, err1 := conn.Do("HMSet", "user1", "name", "beijing", "address", "beijing")
    if err1 != nil {
        fmt.Println("HMSet err=", err1)
        return
    }
    _, err3 := conn.Do("HMSet", "user2" , " Name " , " Wuhan " , " address " , " Wuhan " )
     IF Err3! = Nil { 
        fmt.Println ( " HMSet ERR = " , Err3)
         return 
    } 

    // read data to redis, r is returned air Interface 
    R & lt, ERR2: = redis.Strings (conn.Do ( " HMGet " , " user1 " , " name " , " address "))
    if err2 != nil {
        fmt.Println("HMGet err=", err2)
        return
    }
    for i, v := range r {
        fmt.Printf("r[%d]=%v\n", i, v)
    }
}

Guess you like

Origin www.cnblogs.com/xiximayou/p/11978926.html