Skip to content

Commit 0697075

Browse files
lunnylafriks
andauthored
Fix get system setting bug when enabled redis cache (#22298)
backport #22295, fix #22281 Co-authored-by: Lauris BH <[email protected]>
1 parent f1e07d8 commit 0697075

File tree

5 files changed

+16
-53
lines changed

5 files changed

+16
-53
lines changed

models/avatars/avatar.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ func generateEmailAvatarLink(email string, size int, final bool) string {
154154
return DefaultAvatarLink()
155155
}
156156

157-
enableFederatedAvatarSetting, _ := system_model.GetSetting(system_model.KeyPictureEnableFederatedAvatar)
158-
enableFederatedAvatar := enableFederatedAvatarSetting.GetValueBool()
157+
enableFederatedAvatar := system_model.GetSettingBool(system_model.KeyPictureEnableFederatedAvatar)
159158

160159
var err error
161160
if enableFederatedAvatar && system_model.LibravatarService != nil {
@@ -176,9 +175,7 @@ func generateEmailAvatarLink(email string, size int, final bool) string {
176175
return urlStr
177176
}
178177

179-
disableGravatarSetting, _ := system_model.GetSetting(system_model.KeyPictureDisableGravatar)
180-
181-
disableGravatar := disableGravatarSetting.GetValueBool()
178+
disableGravatar := system_model.GetSettingBool(system_model.KeyPictureDisableGravatar)
182179
if !disableGravatar {
183180
// copy GravatarSourceURL, because we will modify its Path.
184181
avatarURLCopy := *system_model.GravatarSourceURL

models/system/setting.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,22 @@ func GetSettingNoCache(key string) (*Setting, error) {
9393
}
9494

9595
// GetSetting returns the setting value via the key
96-
func GetSetting(key string) (*Setting, error) {
97-
return cache.Get(genSettingCacheKey(key), func() (*Setting, error) {
96+
func GetSetting(key string) (string, error) {
97+
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
9898
res, err := GetSettingNoCache(key)
9999
if err != nil {
100-
return nil, err
100+
return "", err
101101
}
102-
return res, nil
102+
return res.SettingValue, nil
103103
})
104104
}
105105

106106
// GetSettingBool return bool value of setting,
107107
// none existing keys and errors are ignored and result in false
108108
func GetSettingBool(key string) bool {
109109
s, _ := GetSetting(key)
110-
return s.GetValueBool()
110+
v, _ := strconv.ParseBool(s)
111+
return v
111112
}
112113

113114
// GetSettings returns specific settings
@@ -184,8 +185,8 @@ func SetSettingNoVersion(key, value string) error {
184185

185186
// SetSetting updates a users' setting for a specific key
186187
func SetSetting(setting *Setting) error {
187-
_, err := cache.Set(genSettingCacheKey(setting.SettingKey), func() (*Setting, error) {
188-
return setting, upsertSettingValue(strings.ToLower(setting.SettingKey), setting.SettingValue, setting.Version)
188+
_, err := cache.GetString(genSettingCacheKey(setting.SettingKey), func() (string, error) {
189+
return setting.SettingValue, upsertSettingValue(strings.ToLower(setting.SettingKey), setting.SettingValue, setting.Version)
189190
})
190191
if err != nil {
191192
return err

models/user/avatar.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ func (u *User) AvatarLinkWithSize(size int) string {
6868
useLocalAvatar := false
6969
autoGenerateAvatar := false
7070

71-
disableGravatarSetting, _ := system_model.GetSetting(system_model.KeyPictureDisableGravatar)
72-
73-
disableGravatar := disableGravatarSetting.GetValueBool()
71+
disableGravatar := system_model.GetSettingBool(system_model.KeyPictureDisableGravatar)
7472

7573
switch {
7674
case u.UseCustomAvatar:

models/user/setting.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ func genSettingCacheKey(userID int64, key string) string {
5454
}
5555

5656
// GetSetting returns the setting value via the key
57-
func GetSetting(uid int64, key string) (*Setting, error) {
58-
return cache.Get(genSettingCacheKey(uid, key), func() (*Setting, error) {
57+
func GetSetting(uid int64, key string) (string, error) {
58+
return cache.GetString(genSettingCacheKey(uid, key), func() (string, error) {
5959
res, err := GetSettingNoCache(uid, key)
6060
if err != nil {
61-
return nil, err
61+
return "", err
6262
}
63-
return res, nil
63+
return res.SettingValue, nil
6464
})
6565
}
6666

@@ -155,7 +155,7 @@ func SetUserSetting(userID int64, key, value string) error {
155155
return err
156156
}
157157

158-
_, err := cache.Set(genSettingCacheKey(userID, key), func() (string, error) {
158+
_, err := cache.GetString(genSettingCacheKey(userID, key), func() (string, error) {
159159
return value, upsertUserSettingValue(userID, key, value)
160160
})
161161

modules/cache/cache.go

-33
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,6 @@ func GetCache() mc.Cache {
4646
return conn
4747
}
4848

49-
// Get returns the key value from cache with callback when no key exists in cache
50-
func Get[V interface{}](key string, getFunc func() (V, error)) (V, error) {
51-
if conn == nil || setting.CacheService.TTL == 0 {
52-
return getFunc()
53-
}
54-
55-
cached := conn.Get(key)
56-
if value, ok := cached.(V); ok {
57-
return value, nil
58-
}
59-
60-
value, err := getFunc()
61-
if err != nil {
62-
return value, err
63-
}
64-
65-
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
66-
}
67-
68-
// Set updates and returns the key value in the cache with callback. The old value is only removed if the updateFunc() is successful
69-
func Set[V interface{}](key string, valueFunc func() (V, error)) (V, error) {
70-
if conn == nil || setting.CacheService.TTL == 0 {
71-
return valueFunc()
72-
}
73-
74-
value, err := valueFunc()
75-
if err != nil {
76-
return value, err
77-
}
78-
79-
return value, conn.Put(key, value, setting.CacheService.TTLSeconds())
80-
}
81-
8249
// GetString returns the key value from cache with callback when no key exists in cache
8350
func GetString(key string, getFunc func() (string, error)) (string, error) {
8451
if conn == nil || setting.CacheService.TTL == 0 {

0 commit comments

Comments
 (0)