diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-02-15 21:37:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-15 21:37:34 +0800 |
commit | bd820aa9c52da4568b460a0b8604287f8ed8df26 (patch) | |
tree | 15e59e1d4f705b1c5adbd418ed711dfd602a1b25 /tests | |
parent | 03638f9725de3cda5207384098b38462f56d442e (diff) | |
download | gitea-bd820aa9c52da4568b460a0b8604287f8ed8df26.tar.gz gitea-bd820aa9c52da4568b460a0b8604287f8ed8df26.zip |
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/integration/api_comment_test.go | 2 | ||||
-rw-r--r-- | tests/integration/api_issue_reaction_test.go | 6 | ||||
-rw-r--r-- | tests/integration/api_team_test.go | 3 | ||||
-rw-r--r-- | tests/integration/api_team_user_test.go | 3 | ||||
-rw-r--r-- | tests/integration/api_user_orgs_test.go | 9 | ||||
-rw-r--r-- | tests/integration/user_avatar_test.go | 3 |
6 files changed, 15 insertions, 11 deletions
diff --git a/tests/integration/api_comment_test.go b/tests/integration/api_comment_test.go index cc7712e548..70affc9899 100644 --- a/tests/integration/api_comment_test.go +++ b/tests/integration/api_comment_test.go @@ -128,7 +128,7 @@ func TestAPIGetComment(t *testing.T) { DecodeJSON(t, resp, &apiComment) assert.NoError(t, comment.LoadPoster(db.DefaultContext)) - expect := convert.ToComment(comment) + expect := convert.ToComment(db.DefaultContext, comment) assert.Equal(t, expect.ID, apiComment.ID) assert.Equal(t, expect.Poster.FullName, apiComment.Poster.FullName) diff --git a/tests/integration/api_issue_reaction_test.go b/tests/integration/api_issue_reaction_test.go index 76140d7511..42793c66cf 100644 --- a/tests/integration/api_issue_reaction_test.go +++ b/tests/integration/api_issue_reaction_test.go @@ -65,7 +65,7 @@ func TestAPIIssuesReactions(t *testing.T) { DecodeJSON(t, resp, &apiReactions) expectResponse := make(map[int]api.Reaction) expectResponse[0] = api.Reaction{ - User: convert.ToUser(user2, user2), + User: convert.ToUser(db.DefaultContext, user2, user2), Reaction: "eyes", Created: time.Unix(1573248003, 0), } @@ -125,12 +125,12 @@ func TestAPICommentReactions(t *testing.T) { DecodeJSON(t, resp, &apiReactions) expectResponse := make(map[int]api.Reaction) expectResponse[0] = api.Reaction{ - User: convert.ToUser(user2, user2), + User: convert.ToUser(db.DefaultContext, user2, user2), Reaction: "laugh", Created: time.Unix(1573248004, 0), } expectResponse[1] = api.Reaction{ - User: convert.ToUser(user1, user1), + User: convert.ToUser(db.DefaultContext, user1, user1), Reaction: "laugh", Created: time.Unix(1573248005, 0), } diff --git a/tests/integration/api_team_test.go b/tests/integration/api_team_test.go index 27fe5e12e6..7920513136 100644 --- a/tests/integration/api_team_test.go +++ b/tests/integration/api_team_test.go @@ -10,6 +10,7 @@ import ( "testing" auth_model "code.gitea.io/gitea/models/auth" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" @@ -210,7 +211,7 @@ func checkTeamResponse(t *testing.T, testName string, apiTeam *api.Team, name, d func checkTeamBean(t *testing.T, id int64, name, description string, includesAllRepositories bool, permission string, units []string, unitsMap map[string]string) { team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: id}) assert.NoError(t, team.GetUnits(), "GetUnits") - apiTeam, err := convert.ToTeam(team) + apiTeam, err := convert.ToTeam(db.DefaultContext, team) assert.NoError(t, err) checkTeamResponse(t, fmt.Sprintf("checkTeamBean/%s_%s", name, description), apiTeam, name, description, includesAllRepositories, permission, units, unitsMap) } diff --git a/tests/integration/api_team_user_test.go b/tests/integration/api_team_user_test.go index ec977fa572..468697a393 100644 --- a/tests/integration/api_team_user_test.go +++ b/tests/integration/api_team_user_test.go @@ -9,6 +9,7 @@ import ( "time" auth_model "code.gitea.io/gitea/models/auth" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" api "code.gitea.io/gitea/modules/structs" @@ -34,7 +35,7 @@ func TestAPITeamUser(t *testing.T) { user2.Created = user2.Created.In(time.Local) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user2"}) - expectedUser := convert.ToUser(user, user) + expectedUser := convert.ToUser(db.DefaultContext, user, user) // test time via unix timestamp assert.EqualValues(t, expectedUser.LastLogin.Unix(), user2.LastLogin.Unix()) diff --git a/tests/integration/api_user_orgs_test.go b/tests/integration/api_user_orgs_test.go index 831ca018b4..8f914b4875 100644 --- a/tests/integration/api_user_orgs_test.go +++ b/tests/integration/api_user_orgs_test.go @@ -9,6 +9,7 @@ import ( "testing" auth_model "code.gitea.io/gitea/models/auth" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" api "code.gitea.io/gitea/modules/structs" @@ -35,7 +36,7 @@ func TestUserOrgs(t *testing.T) { Name: user17.Name, UserName: user17.Name, FullName: user17.FullName, - AvatarURL: user17.AvatarLink(), + AvatarURL: user17.AvatarLink(db.DefaultContext), Description: "", Website: "", Location: "", @@ -46,7 +47,7 @@ func TestUserOrgs(t *testing.T) { Name: user3.Name, UserName: user3.Name, FullName: user3.FullName, - AvatarURL: user3.AvatarLink(), + AvatarURL: user3.AvatarLink(db.DefaultContext), Description: "", Website: "", Location: "", @@ -105,7 +106,7 @@ func TestMyOrgs(t *testing.T) { Name: user17.Name, UserName: user17.Name, FullName: user17.FullName, - AvatarURL: user17.AvatarLink(), + AvatarURL: user17.AvatarLink(db.DefaultContext), Description: "", Website: "", Location: "", @@ -116,7 +117,7 @@ func TestMyOrgs(t *testing.T) { Name: user3.Name, UserName: user3.Name, FullName: user3.FullName, - AvatarURL: user3.AvatarLink(), + AvatarURL: user3.AvatarLink(db.DefaultContext), Description: "", Website: "", Location: "", diff --git a/tests/integration/user_avatar_test.go b/tests/integration/user_avatar_test.go index 08ecb8b749..7aeba6a334 100644 --- a/tests/integration/user_avatar_test.go +++ b/tests/integration/user_avatar_test.go @@ -12,6 +12,7 @@ import ( "net/url" "testing" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/avatar" @@ -73,7 +74,7 @@ func TestUserAvatar(t *testing.T) { user2 = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) // owner of the repo3, is an org - req = NewRequest(t, "GET", user2.AvatarLinkWithSize(0)) + req = NewRequest(t, "GET", user2.AvatarLinkWithSize(db.DefaultContext, 0)) _ = session.MakeRequest(t, req, http.StatusOK) // Can't test if the response matches because the image is re-generated on upload but checking that this at least doesn't give a 404 should be enough. |