打赏

redis-mysql连接池

1.Redis连接池创建

func GetRedisDBPool(addr, pwd string, db int) *redis.Pool {
	return &redis.Pool{
		MaxIdle:     6,
		IdleTimeout: 240 * time.Second,
		Dial: func() (redis.Conn, error) {
			opts := []redis.DialOption{
				redis.DialConnectTimeout(5 * time.Second),
				redis.DialReadTimeout(2 * time.Second),
				redis.DialWriteTimeout(2 * time.Second),
				redis.DialDatabase(db),
				redis.DialPassword(pwd),
			}
			c, err := redis.Dial("tcp", addr, opts...)
			if err != nil {
				fmt.Printf("connect %s error %s", addr, err.Error())
				return nil, err
			}
			return c, err
		},
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			_, err := c.Do("PING")
			return err
		},
	}
}

2.mysql 连接

func GetDbByHost(user string, password string, host string,)  (*gorm.DB, error){
	var mysql = fmt.Sprintf(
		"%s:%s@tcp(%s:3306)/shop?charset=utf8mb4&parseTime=True&loc=Local",
		user, password, host,
	)

	log.Print(mysql)
	DB, err := gorm.Open("mysql", mysql)
	if err != nil {
		return nil, err
	}
	DB.DB().SetMaxIdleConns(10)
	DB.DB().SetMaxOpenConns(100)
	return DB, nil
}

----------------------------------【喜欢打赏】-------------------------------------------

小主,辛苦啦!文章棒棒哒,赏杯咖啡吧...
打赏

----------------------------------【喜欢打赏】-------------------------------------------

posted @ 2020-02-13 09:34  苍山落暮  阅读(552)  评论(0编辑  收藏  举报