-
Notifications
You must be signed in to change notification settings - Fork 9
/
config_cache.go
87 lines (62 loc) · 1.63 KB
/
config_cache.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
package tgo
import (
"sync"
)
var (
cacheConfigMux sync.Mutex
cacheConfig *ConfigCache
)
type ConfigCache struct {
Redis ConfigCacheRedis
RedisP ConfigCacheRedis // 持久化Redis
}
type ConfigCacheRedis struct {
Address []string
Prefix string
Expire int
ReadTimeout int
WriteTimeout int
ConnectTimeout int
PoolMaxIdle int
PoolMaxActive int
PoolIdleTimeout int
PoolMinActive int
}
func configCacheGet() {
if cacheConfig == nil || len(cacheConfig.Redis.Address) == 0 {
cacheConfigMux.Lock()
defer cacheConfigMux.Unlock()
if cacheConfig == nil || len(cacheConfig.Redis.Address) == 0 {
cacheConfig = &ConfigCache{}
defaultCacheConfig := configCacheGetDefault()
configGet("cache", cacheConfig, defaultCacheConfig)
}
}
}
func configCacheClear() {
cacheConfigMux.Lock()
defer cacheConfigMux.Unlock()
cacheConfig = nil
}
func configCacheGetDefault() *ConfigCache {
return &ConfigCache{Redis: ConfigCacheRedis{[]string{"ip:port"}, "prefix", 604800, 1000, 1000, 1000, 10, 100, 180000, 2}, RedisP: ConfigCacheRedis{[]string{"ip:port"}, "prefix", 604800, 1000, 1000, 1000, 10, 100, 180000, 2}}
}
func ConfigCacheGetRedis() *ConfigCacheRedis {
configCacheGet()
redisConfig := cacheConfig.Redis
if &redisConfig == nil {
//log
redisConfig = configCacheGetDefault().Redis
}
return &redisConfig
}
func ConfigCacheGetRedisWithConn(persistent bool) *ConfigCacheRedis {
configCacheGet()
var redisConfig ConfigCacheRedis
if !persistent {
redisConfig = cacheConfig.Redis
} else {
redisConfig = cacheConfig.RedisP
}
return &redisConfig
}