This commit is contained in:
2023-05-18 17:52:26 +08:00
parent 7c499ad86a
commit 15d2d9ff95
4 changed files with 32 additions and 16 deletions

View File

@@ -61,6 +61,15 @@ func InitDefault(config *Config) error {
}
func Init(key string, config *Config) error {
rd, err := New(config)
if err != nil {
return err
}
instanceMap[key] = rd
return nil
}
func New(config *Config) (*MyRedis, error) {
var (
maxConnAge, _ = time.ParseDuration(DefaultConfig.MaxConnAge)
idleTimeout, _ = time.ParseDuration(DefaultConfig.IdleTimeout)
@@ -77,7 +86,7 @@ func Init(key string, config *Config) error {
if config.MaxConnAge != "" {
t, err := time.ParseDuration(config.MaxConnAge)
if err != nil {
return fmt.Errorf("parse MaxConnAge err: %s\n", err)
return nil, fmt.Errorf("parse MaxConnAge err: %s\n", err)
}
maxConnAge = t
@@ -86,7 +95,7 @@ func Init(key string, config *Config) error {
if config.IdleTimeout != "" {
t, err := time.ParseDuration(config.IdleTimeout)
if err != nil {
return fmt.Errorf("parse IdleTimeout err: %s\n", err)
return nil, fmt.Errorf("parse IdleTimeout err: %s\n", err)
}
idleTimeout = t
@@ -107,11 +116,10 @@ func Init(key string, config *Config) error {
rd.Client = client
ping := rd.Client.Ping(ctx)
if ping.Err() != nil {
return fmt.Errorf("connet redis err: %s", ping.Err())
return nil, fmt.Errorf("connet redis err: %s", ping.Err())
}
instanceMap[key] = rd
return nil
return rd, nil
}
// GetSimple 通用get
@@ -165,11 +173,11 @@ func (r *MyRedis) SetJson(key string, value interface{}, t ...time.Duration) (st
return r.Client.Set(ctx, key, v, t2).Result()
}
func Close(key string) {
func CloseInstance(key string) {
Instance(key).Client.Close()
}
func CloseAll() {
func CloseAllInstance() {
for _, v := range instanceMap {
v.Client.Close()
}