mediocregopher / radix.v2

Redis client for Go
http://godoc.org/github.com/mediocregopher/radix.v2
MIT License
433 stars 92 forks source link

Q: Should i call "select" manually and why it's not a part of connection syntax? #34

Closed rootatdarkstar closed 8 years ago

rootatdarkstar commented 8 years ago

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?

mediocregopher commented 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! :)