49 lines
874 B
Go
49 lines
874 B
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/pkg/errors"
|
|
"gopkg.in/redis.v4"
|
|
"user/logs"
|
|
)
|
|
|
|
var (
|
|
gRedis1 *redis.Client
|
|
)
|
|
|
|
func initRedis(addr string, pwd string, db int, coon int) (*redis.Client, error) {
|
|
|
|
red := redis.NewClient(&redis.Options{
|
|
Addr: addr,
|
|
Password: pwd,
|
|
DB: db,
|
|
PoolSize: coon,
|
|
})
|
|
|
|
_, err := red.Ping().Result()
|
|
if nil != err {
|
|
red.Close()
|
|
return nil, errors.New(fmt.Sprintf("fail to ping redis,addr :%s , pwd :%s ,DB :%d", addr, pwd, db))
|
|
}
|
|
|
|
return red, nil
|
|
}
|
|
|
|
func InitRedis() error {
|
|
redisConf := GetRedis1()
|
|
if nil == redisConf {
|
|
return errors.New("Error Config Redis")
|
|
} else {
|
|
var e error
|
|
gRedis1, e = initRedis(GetRedis1().Addr, GetRedis1().Pwd, GetRedis1().DB, GetRedis1().PoolSize)
|
|
if nil != e {
|
|
logs.Error(e.Error())
|
|
return e
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func RedisOne() *redis.Client {
|
|
return gRedis1
|
|
}
|