Golang:在Redigo的RedisPool上选择DB

func initPool(server, pass string, database int) *redis.Pool {
	return &redis.Pool{
		MaxIdle:   80,
		MaxActive: 12000,
		Dial: func() (redis.Conn, error) {
			conn, err := redis.Dial("tcp", server,
				redis.DialReadTimeout(time.Second*10),
				redis.DialConnectTimeout(time.Second*30),
				redis.DialPassword(pass),
				redis.DialDatabase(database),
			)
			if err != nil {
				log.Println("ERROR: fail init redis pool:", err.Error())
				return nil, fmt.Errorf("ERROR: fail init redis pool: %s", err.Error())
			}
			return conn, err
		},
	}
}

  

第二种方法

 1 &redis.Pool{
 2     MaxIdle:   80,
 3     MaxActive: 12000, // max number of connections
 4     Dial: func() (redis.Conn, error) {
 5         c, err := redis.Dial("tcp", host+":"+port)
 6         if err != nil {
 7             return nil, err
 8         }
 9         _, err := c.Do("SELECT", dbNum)
10         if err != nil {
11            c.Close()
12            return nil, err
13         }
14         return c, nil
15     }

 

posted @ 2020-06-01 10:42  闲云-野鹤  阅读(1480)  评论(0编辑  收藏  举报