blog_backend_api/config/redisConf.go

58 lines
1.1 KiB
Go

/*
* @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
*/
package config
import (
"background/logs"
"fmt"
"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()
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
}