func newPool(host string, db int) *redis.Pool {
    return &redis.Pool {
        MaxIdle: 50,
        MaxActive: 100,
        Dial: func() (redis.Conn, error) {
            options := redis.DialDatabase(db)
            c, err := redis.Dial("tcp", host, options)
            if err != nil {
                panic(err.Error())
            }   
            return c, err 
        },  
    }   
}

var pool *redis.Pool

func GetSimPl(key string) string {
    c := pool1.Get()
    defer c.Close()

    ret, err := redis.Strings(c.Do("ZREVRANGE", mid, 0, -1, "WITHSCORES"))
    fmt.Println(ret)

    ret, err = redis.String(c.Do("GET", mid))
    fmt.Println(ret)

    ret, err = redis.Int64s(c.Do("ZREVRANGE", mid, 0, -1))
    fmt.Println(ret)
    
    ret, err = redis.Int64(c.Do("GET", mid))
    fmt.Println(ret)
        
    return "Succ"
}

func init() {
    pool = newPool("192.168.1.1:6379", 0)
}

 

 

参考文献:

https://github.com/gomodule/redigo/wiki/FAQ

https://godoc.org/github.com/gomodule/redigo/redis#pkg-examples

https://studygolang.com/articles/3029

https://stackoverflow.com/questions/24387350/re-using-redigo-connection-instead-of-recreating-it-every-time