update
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ func TestRedis(t *testing.T) {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer CloseAll()
|
||||
defer CloseAllInstance()
|
||||
|
||||
set, err := Instance().Set(context.Background(), "name", "qwe123", time.Minute).Result()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user