Fix set system setting failure once it cached (#22334)

backport #22333
tokarchuk/v1.18
Lunny Xiao 2 years ago committed by GitHub
parent adc0bcaebb
commit 16d7596635
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      models/system/setting.go
  2. 6
      models/system/setting_test.go

@ -13,7 +13,7 @@ import (
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/setting" setting_module "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
"strk.kbt.io/projects/go/libravatar" "strk.kbt.io/projects/go/libravatar"
@ -89,7 +89,7 @@ func GetSettingNoCache(key string) (*Setting, error) {
if len(v) == 0 { if len(v) == 0 {
return nil, ErrSettingIsNotExist{key} return nil, ErrSettingIsNotExist{key}
} }
return v[key], nil return v[strings.ToLower(key)], nil
} }
// GetSetting returns the setting value via the key // GetSetting returns the setting value via the key
@ -132,7 +132,7 @@ func GetSettings(keys []string) (map[string]*Setting, error) {
type AllSettings map[string]*Setting type AllSettings map[string]*Setting
func (settings AllSettings) Get(key string) Setting { func (settings AllSettings) Get(key string) Setting {
if v, ok := settings[key]; ok { if v, ok := settings[strings.ToLower(key)]; ok {
return *v return *v
} }
return Setting{} return Setting{}
@ -185,14 +185,17 @@ func SetSettingNoVersion(key, value string) error {
// 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.GetString(genSettingCacheKey(setting.SettingKey), func() (string, error) { if err := upsertSettingValue(strings.ToLower(setting.SettingKey), setting.SettingValue, setting.Version); err != nil {
return setting.SettingValue, upsertSettingValue(strings.ToLower(setting.SettingKey), setting.SettingValue, setting.Version)
})
if err != nil {
return err return err
} }
setting.Version++ setting.Version++
cc := cache.GetCache()
if cc != nil {
return cc.Put(genSettingCacheKey(setting.SettingKey), setting.SettingValue, setting_module.CacheService.TTLSeconds())
}
return nil return nil
} }
@ -244,7 +247,7 @@ func Init() error {
var disableGravatar bool var disableGravatar bool
disableGravatarSetting, err := GetSettingNoCache(KeyPictureDisableGravatar) disableGravatarSetting, err := GetSettingNoCache(KeyPictureDisableGravatar)
if IsErrSettingIsNotExist(err) { if IsErrSettingIsNotExist(err) {
disableGravatar = setting.GetDefaultDisableGravatar() disableGravatar = setting_module.GetDefaultDisableGravatar()
disableGravatarSetting = &Setting{SettingValue: strconv.FormatBool(disableGravatar)} disableGravatarSetting = &Setting{SettingValue: strconv.FormatBool(disableGravatar)}
} else if err != nil { } else if err != nil {
return err return err
@ -255,7 +258,7 @@ func Init() error {
var enableFederatedAvatar bool var enableFederatedAvatar bool
enableFederatedAvatarSetting, err := GetSettingNoCache(KeyPictureEnableFederatedAvatar) enableFederatedAvatarSetting, err := GetSettingNoCache(KeyPictureEnableFederatedAvatar)
if IsErrSettingIsNotExist(err) { if IsErrSettingIsNotExist(err) {
enableFederatedAvatar = setting.GetDefaultEnableFederatedAvatar(disableGravatar) enableFederatedAvatar = setting_module.GetDefaultEnableFederatedAvatar(disableGravatar)
enableFederatedAvatarSetting = &Setting{SettingValue: strconv.FormatBool(enableFederatedAvatar)} enableFederatedAvatarSetting = &Setting{SettingValue: strconv.FormatBool(enableFederatedAvatar)}
} else if err != nil { } else if err != nil {
return err return err
@ -263,16 +266,16 @@ func Init() error {
enableFederatedAvatar = disableGravatarSetting.GetValueBool() enableFederatedAvatar = disableGravatarSetting.GetValueBool()
} }
if setting.OfflineMode { if setting_module.OfflineMode {
disableGravatar = true disableGravatar = true
enableFederatedAvatar = false enableFederatedAvatar = false
} }
if enableFederatedAvatar || !disableGravatar { if enableFederatedAvatar || !disableGravatar {
var err error var err error
GravatarSourceURL, err = url.Parse(setting.GravatarSource) GravatarSourceURL, err = url.Parse(setting_module.GravatarSource)
if err != nil { if err != nil {
return fmt.Errorf("Failed to parse Gravatar URL(%s): %w", setting.GravatarSource, err) return fmt.Errorf("Failed to parse Gravatar URL(%s): %w", setting_module.GravatarSource, err)
} }
} }

@ -34,10 +34,14 @@ func TestSettings(t *testing.T) {
assert.EqualValues(t, newSetting.SettingValue, settings[strings.ToLower(keyName)].SettingValue) assert.EqualValues(t, newSetting.SettingValue, settings[strings.ToLower(keyName)].SettingValue)
// updated setting // updated setting
updatedSetting := &system.Setting{SettingKey: keyName, SettingValue: "100", Version: newSetting.Version} updatedSetting := &system.Setting{SettingKey: keyName, SettingValue: "100", Version: settings[strings.ToLower(keyName)].Version}
err = system.SetSetting(updatedSetting) err = system.SetSetting(updatedSetting)
assert.NoError(t, err) assert.NoError(t, err)
value, err := system.GetSetting(keyName)
assert.NoError(t, err)
assert.EqualValues(t, updatedSetting.SettingValue, value)
// get all settings // get all settings
settings, err = system.GetAllSettings() settings, err = system.GetAllSettings()
assert.NoError(t, err) assert.NoError(t, err)

Loading…
Cancel
Save