The go language operates on the methods of the redis connection pool

  • 2020-05-26 09:20:06
  • OfStack

This article illustrates how the go language operates on the redis connection pool. Share with you for your reference. The specific implementation method is as follows:


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)
    ...
}

I hope this article has been helpful to your programming of Go language.


Related articles: