您可以使用
redis.Pool来管理多个连接,检查空闲连接是否仍在运行,并自动获取新连接。您也可以在拨打新连接时自动执行AUTH步骤:
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) ...}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)