-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
gredis_instance.go
44 lines (39 loc) · 1.09 KB
/
gredis_instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// 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 for instance management of redis client.
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 {
group := DefaultGroupName
if len(name) > 0 && name[0] != "" {
group = name[0]
}
v := localInstances.GetOrSetFuncLock(group, func() interface{} {
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
})
if v != nil {
return v.(*Redis)
}
return nil
}