import (
redigo "github.com/gomodule/redigo/redis"
)
func NewPool() *redigo.Pool {
//return &redigo.Pool{
// MaxIdle: 3,
// IdleTimeout: 240 * time.Second,
// // Dial or DialContext must be set. When both are set, DialContext takes precedence over Dial.
// Dial: func () (redigo.Conn, error) { return redigo.Dial("tcp", "localhost",
// redigo.DialPassword("123"),
// ) },
//}
// timeout := time.Duration(20)
addr := fmt.Sprintf("%s:%s", "127.0.0.1", "6379")
redisPool := &redigo.Pool{
MaxIdle: 10,
MaxActive: 50,
IdleTimeout: 240 * time.Second,
Wait: true,
MaxConnLifetime: 60 * time.Second,
Dial: func() (redigo.Conn, error) {
con, err := redigo.Dial("tcp", addr,
redigo.DialPassword(""),
redigo.DialDatabase(0))
_, err = con.Do("PING")
return con, err
},
TestOnBorrow: func(c redigo.Conn, t time.Time) error {
if time.Since(t) < time.Minute {
return nil
}
_, err := c.Do("PING")
if err != nil {
fmt.Println(err)
}
return err
},
}
return redisPool
}
func main() {
pool := NewPool()
conn := pool.Get()
defer conn.Close()
s, err := conn.Do("LPUSH", "r_list", "tik tok")
log.Println(s)
if err == nil {
log.Println("this is ok")
} else {
log.Println("this is failed")
}
}