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
|
|
|
|
|
|
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
|
|
|
)
|
2019-08-22 21:04:30 +08:00
|
|
|
|
|
|
|
|
var (
|
2021-09-26 23:22:32 +08:00
|
|
|
localInstances = gmap.NewStrAnyMap(true)
|
2019-08-22 21:04:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Instance returns an instance of redis client with specified group.
|
2021-10-21 18:22:47 +08:00
|
|
|
// The `name` param is unnecessary, if `name` is not passed,
|
2019-08-22 21:04:30 +08:00
|
|
|
// 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]
|
|
|
|
|
}
|
2021-09-26 23:22:32 +08:00
|
|
|
v := localInstances.GetOrSetFuncLock(group, func() interface{} {
|
2019-08-22 21:04:30 +08:00
|
|
|
if config, ok := GetConfig(group); ok {
|
2021-09-23 21:23:22 +08:00
|
|
|
r, err := New(config)
|
|
|
|
|
if err != nil {
|
2022-01-28 14:51:49 +08:00
|
|
|
intlog.Errorf(context.TODO(), `%+v`, err)
|
2021-09-23 21:23:22 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
2019-08-22 21:04:30 +08:00
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if v != nil {
|
|
|
|
|
return v.(*Redis)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|