go redis 连接池

package main
import (
    "fmt"
    red "github.com/gomodule/redigo/redis"
    "time"
)
type Redis struct {
    pool     *red.Pool
}
var redis *Redis
func initRedis() {
    redis = new(Redis)
    redis.pool = &red.Pool{
        MaxIdle:     256,
        MaxActive:   0,
        IdleTimeout: time.Duration(120),
        Dial: func() (red.Conn, error) {
            return red.Dial(
                "tcp",
                "127.0.0.1:6379",
                red.DialReadTimeout(time.Duration(1000)*time.Millisecond),
                red.DialWriteTimeout(time.Duration(1000)*time.Millisecond),
                red.DialConnectTimeout(time.Duration(1000)*time.Millisecond),
                red.DialDatabase(0),
            )
        },
    }
}
func main(){
    initRedis()
    conn := redis.pool.Get()
    defer conn.Close()
    conn.Do("set","hello","world")  // 写入操作
    fmt.Println(2)
    result,err := conn.Do("get","hello")  // 查询操作
    if err != nil {
        fmt.Print(err.Error())
    }
    str,_:=red.String(result,err)
    fmt.Println(str)
}

 

你可能感兴趣的:(Golang,go,redis,go,连接redis,go,操作redis,golang,redis)