Closed rootatdarkstar closed 8 years ago
If i use non-default database, should i call conn.Cmd("select", MyDBNumber) manually each time i retrieve connection from pool?
You can use a DialFunc in conjunction with NewCustom in order to do what you want. It would look something like this:
p, err := pool.NewCustom(network, addr, size, func(network, addr string) (*redis.Client, error) {
c, err := redis.Dial(network, addr)
if err != nil {
return nil, err
}
if err := c.Cmd("select", MyDBNumber).Err; err != nil {
return nil, err
}
return c, nil
})
Why it's not a part of connection syntax, as many Redis clients do?
While it's a bit more hassle to use something like DialFunc instead of making an extra parameter in some way explicitly for setting the db number, it's also significantly more flexible. The DialFunc can be used for just about anything, including setting the db number, setting timeouts, auth, and even things that the driver couldn't possibly be expected to support, like custom DNS resolving on each new connection (something that I've personally needed). So we're trading a small amount of convenience for a vast amount of flexibility.
Thanks for submitting the issue, hopefully this answers your questions! :)
Hello. If i use non-default database, should i call
conn.Cmd("select", MyDBNumber)
manually each time i retrieve connection from pool? Why it's not a part of connection syntax, as many Redis clients do?