Created
May 14, 2015 05:00
-
-
Save zlwu/1b7c0a3624546af70898 to your computer and use it in GitHub Desktop.
Redigo pool with database selection (http://stackoverflow.com/questions/25708256/golang-selecting-db-on-a-redispool-in-redigo)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a pool embedding the original pool and adding adbno state | |
type DbnoPool struct { | |
redis.Pool | |
dbno int | |
} | |
// "overriding" the Get method | |
func (p *DbnoPool)Get() Connection { | |
conn := p.Pool.Get() | |
conn.Do("SELECT", p.dbno) | |
return conn | |
} | |
pool := &DbnoPool { | |
redis.Pool{ | |
MaxIdle: 80, | |
MaxActive: 12000, // max number of connections | |
Dial: func() (redis.Conn, error) { | |
c, err := redis.Dial("tcp", host+":"+port) | |
if err != nil { | |
panic(err.Error()) | |
} | |
return c, err | |
}, | |
3, // the db number | |
} | |
//now you call it normally | |
conn := pool.Get() | |
defer conn.Close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment