forked from apolloconfig/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.go
139 lines (109 loc) · 2.79 KB
/
repository.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
137
138
139
package agollo
import (
"strconv"
"github.com/zouyx/agollo/v3/agcache"
"github.com/zouyx/agollo/v3/component/log"
"github.com/zouyx/agollo/v3/component/notify"
"github.com/zouyx/agollo/v3/storage"
"github.com/zouyx/agollo/v3/utils"
)
//GetConfig 根据namespace获取apollo配置
func GetConfig(namespace string) *storage.Config {
return GetConfigAndInit(namespace)
}
//GetConfigAndInit 根据namespace获取apollo配置
func GetConfigAndInit(namespace string) *storage.Config {
if namespace == "" {
return nil
}
config, ok := storage.GetApolloConfigCache().Load(namespace)
if !ok {
//init cache
storage.CreateNamespaceConfig(namespace)
//sync config
notify.SyncNamespaceConfig(namespace)
}
config, ok = storage.GetApolloConfigCache().Load(namespace)
if !ok {
return nil
}
return config.(*storage.Config)
}
//GetConfigCache 根据namespace获取apollo配置的缓存
func GetConfigCache(namespace string) agcache.CacheInterface {
config := GetConfigAndInit(namespace)
if config == nil {
return nil
}
return config.GetCache()
}
//GetDefaultConfigCache 获取默认缓存
func GetDefaultConfigCache() agcache.CacheInterface {
config := GetConfigAndInit(storage.GetDefaultNamespace())
if config != nil {
return config.GetCache()
}
return nil
}
//GetApolloConfigCache 获取默认namespace的apollo配置
func GetApolloConfigCache() agcache.CacheInterface {
return GetDefaultConfigCache()
}
//GetValue 获取配置
func GetValue(key string) string {
value := getConfigValue(key)
if value == nil {
return utils.Empty
}
return value.(string)
}
//GetStringValue 获取string配置值
func GetStringValue(key string, defaultValue string) string {
value := GetValue(key)
if value == utils.Empty {
return defaultValue
}
return value
}
//GetIntValue 获取int配置值
func GetIntValue(key string, defaultValue int) int {
value := GetValue(key)
i, err := strconv.Atoi(value)
if err != nil {
log.Debug("convert to int fail!error:", err)
return defaultValue
}
return i
}
//GetFloatValue 获取float配置值
func GetFloatValue(key string, defaultValue float64) float64 {
value := GetValue(key)
i, err := strconv.ParseFloat(value, 64)
if err != nil {
log.Debug("convert to float fail!error:", err)
return defaultValue
}
return i
}
//GetBoolValue 获取bool 配置值
func GetBoolValue(key string, defaultValue bool) bool {
value := GetValue(key)
b, err := strconv.ParseBool(value)
if err != nil {
log.Debug("convert to bool fail!error:", err)
return defaultValue
}
return b
}
func getConfigValue(key string) interface{} {
cache := GetDefaultConfigCache()
if cache == nil {
return utils.Empty
}
value, err := cache.Get(key)
if err != nil {
log.Errorf("get config value fail!key:%s,err:%s", key, err)
return utils.Empty
}
return string(value)
}