-
Notifications
You must be signed in to change notification settings - Fork 14
/
setting_mysql.go
116 lines (101 loc) · 2.58 KB
/
setting_mysql.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
package setting
import (
"strconv"
"strings"
"github.com/coretrix/hitrix/pkg/entity"
"github.com/latolukasz/beeorm"
)
type serviceSetting struct {
cache map[string]*entity.SettingsEntity
}
func NewSettingService() ServiceSettingInterface {
return &serviceSetting{cache: map[string]*entity.SettingsEntity{}}
}
func (s *serviceSetting) Get(ormService *beeorm.Engine, key string) (*entity.SettingsEntity, bool) {
if cachedEntity, exists := s.cache[key]; exists {
return cachedEntity, true
}
query := beeorm.NewRedisSearchQuery()
query.FilterString("Key", key)
settingEntity := &entity.SettingsEntity{}
found := ormService.RedisSearchOne(settingEntity, query)
if !found {
return nil, false
}
if !settingEntity.Editable {
s.cache[key] = settingEntity
}
return settingEntity, true
}
func (s *serviceSetting) GetString(ormService *beeorm.Engine, key string) (string, bool) {
setting, found := s.Get(ormService, key)
if found {
return setting.Value, true
}
return "", false
}
func (s *serviceSetting) GetInt(ormService *beeorm.Engine, key string) (int, bool) {
setting, found := s.Get(ormService, key)
if !found {
return 0, false
}
i, err := strconv.ParseInt(setting.Value, 10, 64)
if err != nil {
return 0, false
}
return int(i), true
}
func (s *serviceSetting) GetUint(ormService *beeorm.Engine, key string) (uint, bool) {
setting, found := s.Get(ormService, key)
if !found {
return 0, false
}
i, err := strconv.ParseUint(setting.Value, 10, 64)
if err != nil {
return 0, false
}
return uint(i), true
}
func (s *serviceSetting) GetInt64(ormService *beeorm.Engine, key string) (int64, bool) {
setting, found := s.Get(ormService, key)
if !found {
return 0, false
}
i, err := strconv.ParseInt(setting.Value, 10, 64)
if err != nil {
return 0, false
}
return i, true
}
func (s *serviceSetting) GetUint64(ormService *beeorm.Engine, key string) (uint64, bool) {
setting, found := s.Get(ormService, key)
if !found {
return 0, false
}
i, err := strconv.ParseUint(setting.Value, 10, 64)
if err != nil {
return 0, false
}
return i, true
}
func (s *serviceSetting) GetFloat64(ormService *beeorm.Engine, key string) (float64, bool) {
setting, found := s.Get(ormService, key)
if !found {
return 0, false
}
i, err := strconv.ParseFloat(setting.Value, 64)
if err != nil {
return 0, false
}
return i, true
}
func (s *serviceSetting) GetBool(ormService *beeorm.Engine, key string) (bool, bool) {
setting, found := s.Get(ormService, key)
if !found {
return false, false
}
if strings.ToLower(setting.Value) == "false" {
return false, true
}
return true, true
}