diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-02-15 14:37:34 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-15 14:37:34 +0100 |
commit | bd820aa9c52da4568b460a0b8604287f8ed8df26 (patch) | |
tree | 15e59e1d4f705b1c5adbd418ed711dfd602a1b25 /services/repository | |
parent | Add tooltip to issue reference (#22913) (diff) | |
download | forgejo-bd820aa9c52da4568b460a0b8604287f8ed8df26.tar.xz forgejo-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 'services/repository')
-rw-r--r-- | services/repository/files/cherry_pick.go | 2 | ||||
-rw-r--r-- | services/repository/files/commit.go | 4 | ||||
-rw-r--r-- | services/repository/files/file.go | 2 | ||||
-rw-r--r-- | services/repository/files/patch.go | 2 | ||||
-rw-r--r-- | services/repository/push.go | 2 |
5 files changed, 6 insertions, 6 deletions
diff --git a/services/repository/files/cherry_pick.go b/services/repository/files/cherry_pick.go index 6bc67e2636..c1c5bfb617 100644 --- a/services/repository/files/cherry_pick.go +++ b/services/repository/files/cherry_pick.go @@ -115,7 +115,7 @@ func CherryPick(ctx context.Context, repo *repo_model.Repository, doer *user_mod } fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil - verification := GetPayloadCommitVerification(commit) + verification := GetPayloadCommitVerification(ctx, commit) fileResponse := &structs.FileResponse{ Commit: fileCommitResponse, Verification: verification, diff --git a/services/repository/files/commit.go b/services/repository/files/commit.go index 9d237f1e22..3e4627487b 100644 --- a/services/repository/files/commit.go +++ b/services/repository/files/commit.go @@ -66,9 +66,9 @@ func CountDivergingCommits(ctx context.Context, repo *repo_model.Repository, bra } // GetPayloadCommitVerification returns the verification information of a commit -func GetPayloadCommitVerification(commit *git.Commit) *structs.PayloadCommitVerification { +func GetPayloadCommitVerification(ctx context.Context, commit *git.Commit) *structs.PayloadCommitVerification { verification := &structs.PayloadCommitVerification{} - commitVerification := asymkey_model.ParseCommitWithSignature(commit) + commitVerification := asymkey_model.ParseCommitWithSignature(ctx, commit) if commit.Signature != nil { verification.Signature = commit.Signature.Signature verification.Payload = commit.Signature.Payload diff --git a/services/repository/files/file.go b/services/repository/files/file.go index ddd64a5399..2bac4372d3 100644 --- a/services/repository/files/file.go +++ b/services/repository/files/file.go @@ -21,7 +21,7 @@ import ( func GetFileResponseFromCommit(ctx context.Context, repo *repo_model.Repository, commit *git.Commit, branch, treeName string) (*api.FileResponse, error) { fileContents, _ := GetContents(ctx, repo, treeName, branch, false) // ok if fails, then will be nil fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil - verification := GetPayloadCommitVerification(commit) + verification := GetPayloadCommitVerification(ctx, commit) fileResponse := &api.FileResponse{ Content: fileContents, Commit: fileCommitResponse, diff --git a/services/repository/files/patch.go b/services/repository/files/patch.go index f65199cfcb..19d089b9e4 100644 --- a/services/repository/files/patch.go +++ b/services/repository/files/patch.go @@ -183,7 +183,7 @@ func ApplyDiffPatch(ctx context.Context, repo *repo_model.Repository, doer *user } fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil - verification := GetPayloadCommitVerification(commit) + verification := GetPayloadCommitVerification(ctx, commit) fileResponse := &structs.FileResponse{ Commit: fileCommitResponse, Verification: verification, diff --git a/services/repository/push.go b/services/repository/push.go index ef6460cef4..8aa8be6aa2 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -355,7 +355,7 @@ func pushUpdateAddTags(ctx context.Context, repo *repo_model.Repository, gitRepo var ok bool author, ok = emailToUser[sig.Email] if !ok { - author, err = user_model.GetUserByEmailContext(ctx, sig.Email) + author, err = user_model.GetUserByEmail(ctx, sig.Email) if err != nil && !user_model.IsErrUserNotExist(err) { return fmt.Errorf("GetUserByEmail: %w", err) } |