-
Notifications
You must be signed in to change notification settings - Fork 14
/
setting_mysql.go
133 lines (102 loc) · 2.69 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package setting
import (
"strconv"
"strings"
redisearch "github.com/coretrix/beeorm-redisearch-plugin"
"github.com/coretrix/hitrix/datalayer"
"github.com/coretrix/hitrix/pkg/entity"
)
type serviceSetting struct {
cache map[string]*entity.SettingsEntity
}
func NewSettingService() ServiceSettingInterface {
return &serviceSetting{cache: map[string]*entity.SettingsEntity{}}
}
func (s *serviceSetting) Get(ormService *datalayer.ORM, key string) (*entity.SettingsEntity, bool) {
if cachedEntity, exists := s.cache[key]; exists {
return cachedEntity, true
}
query := redisearch.NewRedisSearchQuery()
query.FilterString("Key", key)
settingEntity := &entity.SettingsEntity{}
found := ormService.RedisSearchOne(settingEntity, query)
if !found {
return nil, false
}
if !settingEntity.Editable && !settingEntity.Deletable {
s.cache[key] = settingEntity
}
return settingEntity, true
}
func (s *serviceSetting) GetString(ormService *datalayer.ORM, key string) (string, bool) {
setting, found := s.Get(ormService, key)
if found {
return setting.Value, true
}
return "", false
}
func (s *serviceSetting) GetInt(ormService *datalayer.ORM, 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 *datalayer.ORM, 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 *datalayer.ORM, 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 *datalayer.ORM, 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 *datalayer.ORM, 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 *datalayer.ORM, 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
}