40 lines
862 B
Go
40 lines
862 B
Go
package redis
|
|
|
|
import (
|
|
"service/bizcommon/util"
|
|
"service/library/configcenter"
|
|
"service/library/logger"
|
|
)
|
|
|
|
var defaultRedisClient *Client
|
|
|
|
func GetRedisClient() *Client {
|
|
return defaultRedisClient
|
|
}
|
|
|
|
func Init(cfg *configcenter.RedisConfig) (err error) {
|
|
defaultRedisClient, err = NewRedisClient(cfg)
|
|
if err != nil {
|
|
logger.Error("NewRedisClient fail, cfg: %v, err: %v", util.ToJson(cfg), err)
|
|
return err
|
|
}
|
|
return
|
|
}
|
|
|
|
func NewRedisClient(cfg *configcenter.RedisConfig) (*Client, error) {
|
|
cli := new(Client)
|
|
err := cli.StartAndGC(Options{
|
|
Addr: cfg.Uri,
|
|
Password: cfg.Password,
|
|
MaxActive: cfg.MaxActive,
|
|
MaxIdle: cfg.MaxIdle,
|
|
IdleTimeout: cfg.IdleTimeout,
|
|
Prefix: cfg.Prefix,
|
|
})
|
|
if err != nil {
|
|
logger.Error("NewRedisClient fail, cfg: %v, err: %v", util.ToJson(cfg), err)
|
|
return nil, err
|
|
}
|
|
return cli, err
|
|
}
|