Fix #22281 In #21621 , `Get[V]` and `Set[V]` has been introduced, so that cache value will be `*Setting`. For memory cache it's OK. But for redis cache, it can only store `string` for the current implementation. This PR revert some of changes of that and just store or return a `string` for system setting.tags/v1.19.0-rc0
return DefaultAvatarLink() | return DefaultAvatarLink() | ||||
} | } | ||||
enableFederatedAvatarSetting, _ := system_model.GetSetting(system_model.KeyPictureEnableFederatedAvatar) | |||||
enableFederatedAvatar := enableFederatedAvatarSetting.GetValueBool() | |||||
enableFederatedAvatar := system_model.GetSettingBool(system_model.KeyPictureEnableFederatedAvatar) | |||||
var err error | var err error | ||||
if enableFederatedAvatar && system_model.LibravatarService != nil { | if enableFederatedAvatar && system_model.LibravatarService != nil { | ||||
return urlStr | return urlStr | ||||
} | } | ||||
disableGravatarSetting, _ := system_model.GetSetting(system_model.KeyPictureDisableGravatar) | |||||
disableGravatar := disableGravatarSetting.GetValueBool() | |||||
disableGravatar := system_model.GetSettingBool(system_model.KeyPictureDisableGravatar) | |||||
if !disableGravatar { | if !disableGravatar { | ||||
// copy GravatarSourceURL, because we will modify its Path. | // copy GravatarSourceURL, because we will modify its Path. | ||||
avatarURLCopy := *system_model.GravatarSourceURL | avatarURLCopy := *system_model.GravatarSourceURL |
} | } | ||||
// GetSetting returns the setting value via the key | // GetSetting returns the setting value via the key | ||||
func GetSetting(key string) (*Setting, error) { | |||||
return cache.Get(genSettingCacheKey(key), func() (*Setting, error) { | |||||
func GetSetting(key string) (string, error) { | |||||
return cache.GetString(genSettingCacheKey(key), func() (string, error) { | |||||
res, err := GetSettingNoCache(key) | res, err := GetSettingNoCache(key) | ||||
if err != nil { | if err != nil { | ||||
return nil, err | |||||
return "", err | |||||
} | } | ||||
return res, nil | |||||
return res.SettingValue, nil | |||||
}) | }) | ||||
} | } | ||||
// none existing keys and errors are ignored and result in false | // none existing keys and errors are ignored and result in false | ||||
func GetSettingBool(key string) bool { | func GetSettingBool(key string) bool { | ||||
s, _ := GetSetting(key) | s, _ := GetSetting(key) | ||||
return s.GetValueBool() | |||||
v, _ := strconv.ParseBool(s) | |||||
return v | |||||
} | } | ||||
// GetSettings returns specific settings | // GetSettings returns specific settings | ||||
// SetSetting updates a users' setting for a specific key | // SetSetting updates a users' setting for a specific key | ||||
func SetSetting(setting *Setting) error { | func SetSetting(setting *Setting) error { | ||||
_, err := cache.Set(genSettingCacheKey(setting.SettingKey), func() (*Setting, error) { | |||||
return setting, upsertSettingValue(strings.ToLower(setting.SettingKey), setting.SettingValue, setting.Version) | |||||
_, err := cache.GetString(genSettingCacheKey(setting.SettingKey), func() (string, error) { | |||||
return setting.SettingValue, upsertSettingValue(strings.ToLower(setting.SettingKey), setting.SettingValue, setting.Version) | |||||
}) | }) | ||||
if err != nil { | if err != nil { | ||||
return err | return err |
useLocalAvatar := false | useLocalAvatar := false | ||||
autoGenerateAvatar := false | autoGenerateAvatar := false | ||||
disableGravatarSetting, _ := system_model.GetSetting(system_model.KeyPictureDisableGravatar) | |||||
disableGravatar := disableGravatarSetting.GetValueBool() | |||||
disableGravatar := system_model.GetSettingBool(system_model.KeyPictureDisableGravatar) | |||||
switch { | switch { | ||||
case u.UseCustomAvatar: | case u.UseCustomAvatar: |
} | } | ||||
// GetSetting returns the setting value via the key | // GetSetting returns the setting value via the key | ||||
func GetSetting(uid int64, key string) (*Setting, error) { | |||||
return cache.Get(genSettingCacheKey(uid, key), func() (*Setting, error) { | |||||
func GetSetting(uid int64, key string) (string, error) { | |||||
return cache.GetString(genSettingCacheKey(uid, key), func() (string, error) { | |||||
res, err := GetSettingNoCache(uid, key) | res, err := GetSettingNoCache(uid, key) | ||||
if err != nil { | if err != nil { | ||||
return nil, err | |||||
return "", err | |||||
} | } | ||||
return res, nil | |||||
return res.SettingValue, nil | |||||
}) | }) | ||||
} | } | ||||
return err | return err | ||||
} | } | ||||
_, err := cache.Set(genSettingCacheKey(userID, key), func() (string, error) { | |||||
_, err := cache.GetString(genSettingCacheKey(userID, key), func() (string, error) { | |||||
return value, upsertUserSettingValue(userID, key, value) | return value, upsertUserSettingValue(userID, key, value) | ||||
}) | }) | ||||
return conn | return conn | ||||
} | } | ||||
// Get returns the key value from cache with callback when no key exists in cache | |||||
func Get[V interface{}](key string, getFunc func() (V, error)) (V, error) { | |||||
if conn == nil || setting.CacheService.TTL == 0 { | |||||
return getFunc() | |||||
} | |||||
cached := conn.Get(key) | |||||
if value, ok := cached.(V); ok { | |||||
return value, nil | |||||
} | |||||
value, err := getFunc() | |||||
if err != nil { | |||||
return value, err | |||||
} | |||||
return value, conn.Put(key, value, setting.CacheService.TTLSeconds()) | |||||
} | |||||
// Set updates and returns the key value in the cache with callback. The old value is only removed if the updateFunc() is successful | |||||
func Set[V interface{}](key string, valueFunc func() (V, error)) (V, error) { | |||||
if conn == nil || setting.CacheService.TTL == 0 { | |||||
return valueFunc() | |||||
} | |||||
value, err := valueFunc() | |||||
if err != nil { | |||||
return value, err | |||||
} | |||||
return value, conn.Put(key, value, setting.CacheService.TTLSeconds()) | |||||
} | |||||
// GetString returns the key value from cache with callback when no key exists in cache | // GetString returns the key value from cache with callback when no key exists in cache | ||||
func GetString(key string, getFunc func() (string, error)) (string, error) { | func GetString(key string, getFunc func() (string, error)) (string, error) { | ||||
if conn == nil || setting.CacheService.TTL == 0 { | if conn == nil || setting.CacheService.TTL == 0 { |