Files
gf/database/gredis/gredis_instance.go

44 lines
1.0 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gredis
2021-09-23 21:23:22 +08:00
import (
"context"
2021-11-13 23:23:55 +08:00
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/internal/intlog"
2021-09-23 21:23:22 +08:00
)
var (
2021-09-26 23:22:32 +08:00
localInstances = gmap.NewStrAnyMap(true)
)
// Instance returns an instance of redis client with specified group.
// The `name` param is unnecessary, if `name` is not passed,
// it returns a redis instance with default configuration group.
func Instance(name ...string) *Redis {
2020-12-14 19:34:02 +08:00
group := DefaultGroupName
if len(name) > 0 && name[0] != "" {
group = name[0]
}
2021-09-26 23:22:32 +08:00
v := localInstances.GetOrSetFuncLock(group, func() interface{} {
if config, ok := GetConfig(group); ok {
2021-09-23 21:23:22 +08:00
r, err := New(config)
if err != nil {
intlog.Errorf(context.TODO(), `%+v`, err)
2021-09-23 21:23:22 +08:00
return nil
}
return r
}
return nil
})
if v != nil {
return v.(*Redis)
}
return nil
}