background/config/redisConf.go

49 lines
880 B
Go
Raw Normal View History

2019-01-31 08:57:22 +00:00
package config
import (
"fmt"
"github.com/pkg/errors"
"gopkg.in/redis.v4"
2019-04-07 04:25:07 +00:00
"background/logs"
2019-01-31 08:57:22 +00:00
)
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()
2019-03-07 02:36:09 +00:00
if nil == redisConf {
2019-01-31 08:57:22 +00:00
return errors.New("Error Config Redis")
2019-03-07 02:36:09 +00:00
} else {
2019-01-31 08:57:22 +00:00
var e error
2019-03-07 02:36:09 +00:00
gRedis1, e = initRedis(GetRedis1().Addr, GetRedis1().Pwd, GetRedis1().DB, GetRedis1().PoolSize)
if nil != e {
2019-01-31 08:57:22 +00:00
logs.Error(e.Error())
return e
}
}
return nil
}
func RedisOne() *redis.Client {
return gRedis1
2019-03-07 02:36:09 +00:00
}