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     }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章