Skip to content

Commit aceac6c

Browse files
committed
Use context cache for dashaboard page to make it faster
1 parent 539ecc2 commit aceac6c

File tree

7 files changed

+39
-31
lines changed

7 files changed

+39
-31
lines changed

models/avatars/avatar.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ func generateEmailAvatarLink(ctx context.Context, email string, size int, final
153153
return DefaultAvatarLink()
154154
}
155155

156-
enableFederatedAvatar := system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureEnableFederatedAvatar)
156+
enableFederatedAvatar := system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureEnableFederatedAvatar,
157+
setting.GetDefaultEnableFederatedAvatar(setting.GetDefaultDisableGravatar()))
157158

158159
var err error
159160
if enableFederatedAvatar && system_model.LibravatarService != nil {
@@ -174,7 +175,9 @@ func generateEmailAvatarLink(ctx context.Context, email string, size int, final
174175
return urlStr
175176
}
176177

177-
disableGravatar := system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar)
178+
disableGravatar := system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar,
179+
setting.GetDefaultDisableGravatar(),
180+
)
178181
if !disableGravatar {
179182
// copy GravatarSourceURL, because we will modify its Path.
180183
avatarURLCopy := *system_model.GravatarSourceURL

models/system/setting.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,17 @@ func GetSetting(ctx context.Context, key string) (*Setting, error) {
9494
const contextCacheKey = "system_setting"
9595

9696
// GetSettingWithCache returns the setting value via the key
97-
func GetSettingWithCache(ctx context.Context, key string) (string, error) {
97+
func GetSettingWithCache(ctx context.Context, key, defaultVal string) (string, error) {
9898
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
9999
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
100100
res, err := GetSetting(ctx, key)
101101
if err != nil {
102+
if IsErrSettingIsNotExist(err) {
103+
return defaultVal, SetSetting(ctx, &Setting{
104+
SettingKey: key,
105+
SettingValue: defaultVal,
106+
})
107+
}
102108
return "", err
103109
}
104110
return res.SettingValue, nil
@@ -117,8 +123,8 @@ func GetSettingBool(ctx context.Context, key string) bool {
117123
return v
118124
}
119125

120-
func GetSettingWithCacheBool(ctx context.Context, key string) bool {
121-
s, _ := GetSettingWithCache(ctx, key)
126+
func GetSettingWithCacheBool(ctx context.Context, key string, defaultVal bool) bool {
127+
s, _ := GetSettingWithCache(ctx, key, strconv.FormatBool(defaultVal))
122128
v, _ := strconv.ParseBool(s)
123129
return v
124130
}

models/user/avatar.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ func (u *User) AvatarLinkWithSize(ctx context.Context, size int) string {
6767
useLocalAvatar := false
6868
autoGenerateAvatar := false
6969

70-
disableGravatar := system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar)
70+
disableGravatar := system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar,
71+
setting.GetDefaultDisableGravatar(),
72+
)
7173

7274
switch {
7375
case u.UseCustomAvatar:

models/user/user.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,9 @@ func ValidateCommitWithEmail(ctx context.Context, c *git.Commit) *User {
10791079
if c.Author == nil {
10801080
return nil
10811081
}
1082+
1083+
fmt.Println("11111----", c.Author.Email)
1084+
10821085
u, err := GetUserByEmail(ctx, c.Author.Email)
10831086
if err != nil {
10841087
return nil

modules/repository/commits.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"code.gitea.io/gitea/models/avatars"
1313
user_model "code.gitea.io/gitea/models/user"
14+
"code.gitea.io/gitea/modules/cache"
1415
"code.gitea.io/gitea/modules/git"
1516
"code.gitea.io/gitea/modules/log"
1617
"code.gitea.io/gitea/modules/setting"
@@ -134,35 +135,21 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi
134135
// AvatarLink tries to match user in database with e-mail
135136
// in order to show custom avatar, and falls back to general avatar link.
136137
func (pc *PushCommits) AvatarLink(ctx context.Context, email string) string {
137-
if pc.avatars == nil {
138-
pc.avatars = make(map[string]string)
139-
}
140-
avatar, ok := pc.avatars[email]
141-
if ok {
142-
return avatar
143-
}
144-
145138
size := avatars.DefaultAvatarPixelSize * setting.Avatar.RenderedSizeFactor
146139

147-
u, ok := pc.emailUsers[email]
148-
if !ok {
149-
var err error
150-
u, err = user_model.GetUserByEmail(ctx, email)
140+
v, _ := cache.GetWithContextCache(ctx, "push_commits", email, func() (string, error) {
141+
u, err := user_model.GetUserByEmail(ctx, email)
151142
if err != nil {
152-
pc.avatars[email] = avatars.GenerateEmailAvatarFastLink(ctx, email, size)
153143
if !user_model.IsErrUserNotExist(err) {
154144
log.Error("GetUserByEmail: %v", err)
155-
return ""
145+
return "", err
156146
}
157-
} else {
158-
pc.emailUsers[email] = u
147+
return avatars.GenerateEmailAvatarFastLink(ctx, email, size), nil
159148
}
160-
}
161-
if u != nil {
162-
pc.avatars[email] = u.AvatarLinkWithSize(ctx, size)
163-
}
149+
return u.AvatarLinkWithSize(ctx, size), nil
150+
})
164151

165-
return pc.avatars[email]
152+
return v
166153
}
167154

168155
// CommitToPushCommit transforms a git.Commit to PushCommit type.

routers/web/admin/users.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,9 @@ func EditUser(ctx *context.Context) {
314314
ctx.Data["DisableRegularOrgCreation"] = setting.Admin.DisableRegularOrgCreation
315315
ctx.Data["DisableMigrations"] = setting.Repository.DisableMigrations
316316
ctx.Data["AllowedUserVisibilityModes"] = setting.Service.AllowedUserVisibilityModesSlice.ToVisibleTypeSlice()
317-
ctx.Data["DisableGravatar"] = system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar)
317+
ctx.Data["DisableGravatar"] = system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar,
318+
setting.GetDefaultDisableGravatar(),
319+
)
318320

319321
prepareUserInfo(ctx)
320322
if ctx.Written() {
@@ -331,7 +333,8 @@ func EditUserPost(ctx *context.Context) {
331333
ctx.Data["PageIsAdminUsers"] = true
332334
ctx.Data["DisableMigrations"] = setting.Repository.DisableMigrations
333335
ctx.Data["AllowedUserVisibilityModes"] = setting.Service.AllowedUserVisibilityModesSlice.ToVisibleTypeSlice()
334-
ctx.Data["DisableGravatar"] = system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar)
336+
ctx.Data["DisableGravatar"] = system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar,
337+
setting.GetDefaultDisableGravatar())
335338

336339
u := prepareUserInfo(ctx)
337340
if ctx.Written() {

routers/web/user/setting/profile.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ func Profile(ctx *context.Context) {
4444
ctx.Data["Title"] = ctx.Tr("settings.profile")
4545
ctx.Data["PageIsSettingsProfile"] = true
4646
ctx.Data["AllowedUserVisibilityModes"] = setting.Service.AllowedUserVisibilityModesSlice.ToVisibleTypeSlice()
47-
ctx.Data["DisableGravatar"] = system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar)
47+
ctx.Data["DisableGravatar"] = system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar,
48+
setting.GetDefaultDisableGravatar(),
49+
)
4850

4951
ctx.HTML(http.StatusOK, tplSettingsProfile)
5052
}
@@ -86,7 +88,9 @@ func ProfilePost(ctx *context.Context) {
8688
ctx.Data["Title"] = ctx.Tr("settings")
8789
ctx.Data["PageIsSettingsProfile"] = true
8890
ctx.Data["AllowedUserVisibilityModes"] = setting.Service.AllowedUserVisibilityModesSlice.ToVisibleTypeSlice()
89-
ctx.Data["DisableGravatar"] = system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar)
91+
ctx.Data["DisableGravatar"] = system_model.GetSettingWithCacheBool(ctx, system_model.KeyPictureDisableGravatar,
92+
setting.GetDefaultDisableGravatar(),
93+
)
9094

9195
if ctx.HasError() {
9296
ctx.HTML(http.StatusOK, tplSettingsProfile)

0 commit comments

Comments
 (0)