func MakeRedisPool(proto, addr string, db, maxIdle int) {
	if pool != nil {
		pool.Close()
	}
	pool = redis.NewPool(func() (redis.Conn, error) {
		c, err := redis.Dial(proto, addr)
		if err != nil {
			return nil, err
		}
		_, err = c.Do("SELECT", db)
		if err != nil {
			return nil, err
		}
		return c, nil
	}, maxIdle)
}
示例#2
0
func MakeRedisPool(config RedisConfig) {
	if pool != nil {
		pool.Close()
	}
	pool = redis.NewPool(func() (redis.Conn, error) {
		c, err := redis.Dial("tcp", config.Addr)
		if err != nil {
			return nil, err
		}
		if config.Password != "" {
			_, err = c.Do("AUTH", config.Password)
			if err != nil {
				return nil, err
			}
		}
		_, err = c.Do("SELECT", config.Database)
		if err != nil {
			return nil, err
		}
		return c, nil
	}, config.MaxIdle)
}