forked from gogf/gf
/
gins_redis.go
65 lines (61 loc) · 1.82 KB
/
gins_redis.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 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 gins
import (
"fmt"
"github.com/gogf/gf/database/gredis"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/gutil"
)
const (
frameCoreComponentNameRedis = "gf.core.component.redis"
configNodeNameRedis = "redis"
)
// Redis returns an instance of redis client with specified configuration group name.
func Redis(name ...string) *gredis.Redis {
config := Config()
group := gredis.DefaultGroupName
if len(name) > 0 && name[0] != "" {
group = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", frameCoreComponentNameRedis, group)
result := instances.GetOrSetFuncLock(instanceKey, func() interface{} {
// If already configured, it returns the redis instance.
if _, ok := gredis.GetConfig(group); ok {
return gredis.Instance(group)
}
// Or else, it parses the default configuration file and returns a new redis instance.
var m map[string]interface{}
if _, v := gutil.MapPossibleItemByKey(Config().GetMap("."), configNodeNameRedis); v != nil {
m = gconv.Map(v)
}
if len(m) > 0 {
if v, ok := m[group]; ok {
redisConfig, err := gredis.ConfigFromStr(gconv.String(v))
if err != nil {
panic(err)
}
return gredis.New(redisConfig)
} else {
panic(fmt.Sprintf(`configuration for redis not found for group "%s"`, group))
}
} else {
filepath, err := config.GetFilePath()
if err != nil {
panic(err)
}
panic(fmt.Sprintf(
`incomplete configuration for redis: "redis" node not found in config file "%s"`,
filepath,
))
}
return nil
})
if result != nil {
return result.(*gredis.Redis)
}
return nil
}