background/config/redisConf.go

58 lines
1.1 KiB
Go
Raw Normal View History

/*
* @Author: your name
* @Date: 2019-04-07 11:19:50
* @LastEditTime: 2021-07-17 22:23:23
* @LastEditors: your name
* @Description: In User Settings Edit
* @FilePath: \background\config\redisConf.go
*/
2019-01-31 08:57:22 +00:00
package config
import (
"background/logs"
2019-01-31 08:57:22 +00:00
"fmt"
2019-01-31 08:57:22 +00:00
"github.com/pkg/errors"
"gopkg.in/redis.v4"
)
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
}