/
gredis_config.go
136 lines (122 loc) · 3.6 KB
/
gredis_config.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2019 gf Author(https://github.com/csrgxtu/gf). 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/csrgxtu/gf.
package gredis
import (
"github.com/csrgxtu/gf/internal/intlog"
"time"
"github.com/csrgxtu/gf/errors/gerror"
"github.com/csrgxtu/gf/container/gmap"
"github.com/csrgxtu/gf/text/gregex"
"github.com/csrgxtu/gf/text/gstr"
"github.com/csrgxtu/gf/util/gconv"
)
const (
DEFAULT_GROUP_NAME = "default" // Default configuration group name.
DEFAULT_REDIS_PORT = 6379 // Default redis port configuration if not passed.
)
var (
// Configuration groups.
configs = gmap.NewStrAnyMap(true)
)
// SetConfig sets the global configuration for specified group.
// If <name> is not passed, it sets configuration for the default group name.
func SetConfig(config Config, name ...string) {
group := DEFAULT_GROUP_NAME
if len(name) > 0 {
group = name[0]
}
configs.Set(group, config)
instances.Remove(group)
intlog.Printf(`SetConfig for group "%s": %+v`, group, config)
}
// SetConfigByStr sets the global configuration for specified group with string.
// If <name> is not passed, it sets configuration for the default group name.
func SetConfigByStr(str string, name ...string) error {
group := DEFAULT_GROUP_NAME
if len(name) > 0 {
group = name[0]
}
config, err := ConfigFromStr(str)
if err != nil {
return err
}
configs.Set(group, config)
instances.Remove(group)
return nil
}
// GetConfig returns the global configuration with specified group name.
// If <name> is not passed, it returns configuration of the default group name.
func GetConfig(name ...string) (config Config, ok bool) {
group := DEFAULT_GROUP_NAME
if len(name) > 0 {
group = name[0]
}
if v := configs.Get(group); v != nil {
return v.(Config), true
}
return Config{}, false
}
// RemoveConfig removes the global configuration with specified group.
// If <name> is not passed, it removes configuration of the default group name.
func RemoveConfig(name ...string) {
group := DEFAULT_GROUP_NAME
if len(name) > 0 {
group = name[0]
}
configs.Remove(group)
instances.Remove(group)
intlog.Printf(`RemoveConfig: %s`, group)
}
// ConfigFromStr parses and returns config from given str.
// Eg: host:port[,db,pass?maxIdle=x&maxActive=x&idleTimeout=x&maxConnLifetime=x]
func ConfigFromStr(str string) (config Config, err error) {
array, _ := gregex.MatchString(`([^:]+):*(\d*),{0,1}(\d*),{0,1}(.*)\?(.+)`, str)
if len(array) == 6 {
parse, _ := gstr.Parse(array[5])
config = Config{
Host: array[1],
Port: gconv.Int(array[2]),
Db: gconv.Int(array[3]),
Pass: array[4],
}
if config.Port == 0 {
config.Port = DEFAULT_REDIS_PORT
}
if v, ok := parse["maxIdle"]; ok {
config.MaxIdle = gconv.Int(v)
}
if v, ok := parse["maxActive"]; ok {
config.MaxActive = gconv.Int(v)
}
if v, ok := parse["idleTimeout"]; ok {
config.IdleTimeout = gconv.Duration(v) * time.Second
}
if v, ok := parse["maxConnLifetime"]; ok {
config.MaxConnLifetime = gconv.Duration(v) * time.Second
}
return
}
array, _ = gregex.MatchString(`([^:]+):*(\d*),{0,1}(\d*),{0,1}(.*)`, str)
if len(array) == 5 {
config = Config{
Host: array[1],
Port: gconv.Int(array[2]),
Db: gconv.Int(array[3]),
Pass: array[4],
}
if config.Port == 0 {
config.Port = DEFAULT_REDIS_PORT
}
} else {
err = gerror.Newf(`invalid redis configuration: "%s"`, str)
}
return
}
// ClearConfig removes all configurations and instances of redis.
func ClearConfig() {
configs.Clear()
instances.Clear()
}