mirror of
https://gitee.com/johng/gf
synced 2026-07-02 19:31:07 +08:00
40 lines
1011 B
Go
40 lines
1011 B
Go
// 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
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/container/gmap"
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
|
)
|
|
|
|
var (
|
|
localInstances = gmap.NewKVMap[string, *Redis](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 {
|
|
group := DefaultGroupName
|
|
if len(name) > 0 && name[0] != "" {
|
|
group = name[0]
|
|
}
|
|
return localInstances.GetOrSetFuncLock(group, func() *Redis {
|
|
if config, ok := GetConfig(group); ok {
|
|
r, err := New(config)
|
|
if err != nil {
|
|
intlog.Errorf(context.TODO(), `%+v`, err)
|
|
return nil
|
|
}
|
|
return r
|
|
}
|
|
return nil
|
|
})
|
|
}
|