Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dashboard ignored system setting cache #21621

Merged
merged 18 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions models/avatars/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,17 @@ func generateEmailAvatarLink(email string, size int, final bool) string {
return DefaultAvatarLink()
}

enableFederatedAvatar, _ := system_model.GetSetting(system_model.KeyPictureEnableFederatedAvatar)
enableFederatedAvatarStr, _ := cache.GetString(system_model.GenCacheKey(system_model.KeyPictureEnableFederatedAvatar), func() (string, error) {
res, err := system_model.GetSetting(system_model.KeyPictureEnableFederatedAvatar)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
enableFederatedAvatar, _ := strconv.ParseBool(enableFederatedAvatarStr)

var err error
if enableFederatedAvatar != nil && enableFederatedAvatar.GetValueBool() && system_model.LibravatarService != nil {
if enableFederatedAvatarStr != "" && enableFederatedAvatar && system_model.LibravatarService != nil {
emailHash := saveEmailHash(email)
if final {
// for final link, we can spend more time on slow external query
Expand All @@ -171,8 +178,16 @@ func generateEmailAvatarLink(email string, size int, final bool) string {
return urlStr
}

disableGravatar, _ := system_model.GetSetting(system_model.KeyPictureDisableGravatar)
if disableGravatar != nil && !disableGravatar.GetValueBool() {
disableGravatarStr, _ := cache.GetString(system_model.GenCacheKey(system_model.KeyPictureDisableGravatar), func() (string, error) {
res, err := system_model.GetSetting(system_model.KeyPictureDisableGravatar)
if err != nil {
return "", err
}
return res.SettingValue, nil
})

disableGravatar, _ := strconv.ParseBool(disableGravatarStr)
if disableGravatarStr != "" && !disableGravatar {
// copy GravatarSourceURL, because we will modify its Path.
avatarURLCopy := *system_model.GravatarSourceURL
avatarURLCopy.Path = path.Join(avatarURLCopy.Path, HashEmail(email))
Expand Down
5 changes: 5 additions & 0 deletions models/system/setting_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ const (
KeyPictureDisableGravatar = "picture.disable_gravatar"
KeyPictureEnableFederatedAvatar = "picture.enable_federated_avatar"
)

// GenCacheKey returns the cache key for some configuration
func GenCacheKey(key string) string {
return "system.setting." + key
}
16 changes: 11 additions & 5 deletions models/user/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (
"fmt"
"image/png"
"io"
"strconv"
"strings"

"code.gitea.io/gitea/models/avatars"
"code.gitea.io/gitea/models/db"
system_model "code.gitea.io/gitea/models/system"
"code.gitea.io/gitea/modules/avatar"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
Expand Down Expand Up @@ -68,11 +70,15 @@ func (u *User) AvatarLinkWithSize(size int) string {
useLocalAvatar := false
autoGenerateAvatar := false

var disableGravatar bool
disableGravatarSetting, _ := system_model.GetSetting(system_model.KeyPictureDisableGravatar)
if disableGravatarSetting != nil {
disableGravatar = disableGravatarSetting.GetValueBool()
}
disableGravatarStr, _ := cache.GetString(system_model.GenCacheKey(system_model.KeyPictureDisableGravatar), func() (string, error) {
res, err := system_model.GetSetting(system_model.KeyPictureDisableGravatar)
if err != nil {
return "", err
}
return res.SettingValue, nil
})

lunny marked this conversation as resolved.
Show resolved Hide resolved
disableGravatar, _ := strconv.ParseBool(disableGravatarStr)

switch {
case u.UseCustomAvatar:
Expand Down
8 changes: 2 additions & 6 deletions modules/system/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ import (
"code.gitea.io/gitea/modules/cache"
)

func genKey(key string) string {
return "system.setting." + key
}

// GetSetting returns the setting value via the key
func GetSetting(key string) (string, error) {
return cache.GetString(genKey(key), func() (string, error) {
return cache.GetString(system.GenCacheKey(key), func() (string, error) {
res, err := system.GetSetting(key)
if err != nil {
return "", err
Expand All @@ -36,7 +32,7 @@ func GetSettingBool(key string) bool {

// SetSetting sets the setting value
func SetSetting(key, value string, version int) error {
cache.Remove(genKey(key))
cache.Remove(system.GenCacheKey(key))

return system.SetSetting(&system.Setting{
SettingKey: key,
Expand Down