前端之家收集整理的这篇文章主要介绍了
golang操作redis连接池,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
func newPool(server,password string) *redis.Pool {
return &redis.Pool{
MaxIdle: 3,IdleTimeout: 240 * time.Second,Dial: func () (redis.Conn,error) {
c,err := redis.Dial("tcp",server)
if err != nil {
return nil,err
}
if _,err := c.Do("AUTH",password); err != nil {
c.Close()
return nil,err
}
return c,err
},TestOnBorrow: func(c redis.Conn,t time.Time) error {
_,err := c.Do("PING")
return err
},}
}
var (
pool *redis.Pool
redisServer = flag.String("redisServer",":6379","")
redisPassword = flag.String("redisPassword","","")
)
func main() {
flag.Parse()
pool = newPool(*redisServer,*redisPassword)
...
}