2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-08-22 21:04:30 +08:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
|
import "github.com/gogf/gf/container/gmap"
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Instance map
|
|
|
|
instances = 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
|
2019-08-22 21:04:30 +08:00
|
|
|
if len(name) > 0 && name[0] != "" {
|
|
|
|
group = name[0]
|
|
|
|
}
|
|
|
|
v := instances.GetOrSetFuncLock(group, func() interface{} {
|
|
|
|
if config, ok := GetConfig(group); ok {
|
|
|
|
r := New(config)
|
|
|
|
r.group = group
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if v != nil {
|
|
|
|
return v.(*Redis)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|