2020-09-18 20:09:26 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-18 20:09:26 +08:00
|
|
|
|
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
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.
2023-02-15 21:37:34 +08:00
|
|
|
"context"
|
2021-11-17 02:18:25 +08:00
|
|
|
"net/url"
|
2020-09-18 20:09:26 +08:00
|
|
|
"time"
|
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-09-18 20:09:26 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
2022-03-29 23:47:44 +08:00
|
|
|
"code.gitea.io/gitea/services/gitdiff"
|
2020-09-18 20:09:26 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// ToCommitUser convert a git.Signature to an api.CommitUser
|
|
|
|
func ToCommitUser(sig *git.Signature) *api.CommitUser {
|
|
|
|
return &api.CommitUser{
|
|
|
|
Identity: api.Identity{
|
|
|
|
Name: sig.Name,
|
|
|
|
Email: sig.Email,
|
|
|
|
},
|
|
|
|
Date: sig.When.UTC().Format(time.RFC3339),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToCommitMeta convert a git.Tag to an api.CommitMeta
|
2021-12-10 09:27:50 +08:00
|
|
|
func ToCommitMeta(repo *repo_model.Repository, tag *git.Tag) *api.CommitMeta {
|
2020-09-18 20:09:26 +08:00
|
|
|
return &api.CommitMeta{
|
2020-10-05 12:07:54 +08:00
|
|
|
SHA: tag.Object.String(),
|
|
|
|
URL: util.URLJoin(repo.APIURL(), "git/commits", tag.ID.String()),
|
|
|
|
Created: tag.Tagger.When,
|
2020-09-18 20:09:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToPayloadCommit convert a git.Commit to api.PayloadCommit
|
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.
2023-02-15 21:37:34 +08:00
|
|
|
func ToPayloadCommit(ctx context.Context, repo *repo_model.Repository, c *git.Commit) *api.PayloadCommit {
|
2020-09-18 20:09:26 +08:00
|
|
|
authorUsername := ""
|
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.
2023-02-15 21:37:34 +08:00
|
|
|
if author, err := user_model.GetUserByEmail(ctx, c.Author.Email); err == nil {
|
2020-09-18 20:09:26 +08:00
|
|
|
authorUsername = author.Name
|
2021-11-24 17:49:20 +08:00
|
|
|
} else if !user_model.IsErrUserNotExist(err) {
|
2020-09-18 20:09:26 +08:00
|
|
|
log.Error("GetUserByEmail: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
committerUsername := ""
|
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.
2023-02-15 21:37:34 +08:00
|
|
|
if committer, err := user_model.GetUserByEmail(ctx, c.Committer.Email); err == nil {
|
2020-09-18 20:09:26 +08:00
|
|
|
committerUsername = committer.Name
|
2021-11-24 17:49:20 +08:00
|
|
|
} else if !user_model.IsErrUserNotExist(err) {
|
2020-09-18 20:09:26 +08:00
|
|
|
log.Error("GetUserByEmail: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.PayloadCommit{
|
|
|
|
ID: c.ID.String(),
|
|
|
|
Message: c.Message(),
|
|
|
|
URL: util.URLJoin(repo.HTMLURL(), "commit", c.ID.String()),
|
|
|
|
Author: &api.PayloadUser{
|
|
|
|
Name: c.Author.Name,
|
|
|
|
Email: c.Author.Email,
|
|
|
|
UserName: authorUsername,
|
|
|
|
},
|
|
|
|
Committer: &api.PayloadUser{
|
|
|
|
Name: c.Committer.Name,
|
|
|
|
Email: c.Committer.Email,
|
|
|
|
UserName: committerUsername,
|
|
|
|
},
|
|
|
|
Timestamp: c.Author.When,
|
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.
2023-02-15 21:37:34 +08:00
|
|
|
Verification: ToVerification(ctx, c),
|
2020-09-18 20:09:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 09:06:05 +08:00
|
|
|
type ToCommitOptions struct {
|
|
|
|
Stat bool
|
|
|
|
Verification bool
|
|
|
|
Files bool
|
|
|
|
}
|
|
|
|
|
2020-09-18 20:09:26 +08:00
|
|
|
// ToCommit convert a git.Commit to api.Commit
|
2023-05-09 09:06:05 +08:00
|
|
|
func ToCommit(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user_model.User, opts ToCommitOptions) (*api.Commit, error) {
|
2020-09-18 20:09:26 +08:00
|
|
|
var apiAuthor, apiCommitter *api.User
|
|
|
|
|
|
|
|
// Retrieve author and committer information
|
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
var cacheAuthor *user_model.User
|
2020-09-18 20:09:26 +08:00
|
|
|
var ok bool
|
|
|
|
if userCache == nil {
|
2021-11-24 17:49:20 +08:00
|
|
|
cacheAuthor = (*user_model.User)(nil)
|
2020-09-18 20:09:26 +08:00
|
|
|
ok = false
|
|
|
|
} else {
|
|
|
|
cacheAuthor, ok = userCache[commit.Author.Email]
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
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.
2023-02-15 21:37:34 +08:00
|
|
|
apiAuthor = ToUser(ctx, cacheAuthor, nil)
|
2020-09-18 20:09:26 +08:00
|
|
|
} else {
|
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.
2023-02-15 21:37:34 +08:00
|
|
|
author, err := user_model.GetUserByEmail(ctx, commit.Author.Email)
|
2021-11-24 17:49:20 +08:00
|
|
|
if err != nil && !user_model.IsErrUserNotExist(err) {
|
2020-09-18 20:09:26 +08:00
|
|
|
return nil, err
|
|
|
|
} else if err == nil {
|
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.
2023-02-15 21:37:34 +08:00
|
|
|
apiAuthor = ToUser(ctx, author, nil)
|
2020-09-18 20:09:26 +08:00
|
|
|
if userCache != nil {
|
|
|
|
userCache[commit.Author.Email] = author
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
var cacheCommitter *user_model.User
|
2020-09-18 20:09:26 +08:00
|
|
|
if userCache == nil {
|
2021-11-24 17:49:20 +08:00
|
|
|
cacheCommitter = (*user_model.User)(nil)
|
2020-09-18 20:09:26 +08:00
|
|
|
ok = false
|
|
|
|
} else {
|
|
|
|
cacheCommitter, ok = userCache[commit.Committer.Email]
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
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.
2023-02-15 21:37:34 +08:00
|
|
|
apiCommitter = ToUser(ctx, cacheCommitter, nil)
|
2020-09-18 20:09:26 +08:00
|
|
|
} else {
|
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.
2023-02-15 21:37:34 +08:00
|
|
|
committer, err := user_model.GetUserByEmail(ctx, commit.Committer.Email)
|
2021-11-24 17:49:20 +08:00
|
|
|
if err != nil && !user_model.IsErrUserNotExist(err) {
|
2020-09-18 20:09:26 +08:00
|
|
|
return nil, err
|
|
|
|
} else if err == nil {
|
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.
2023-02-15 21:37:34 +08:00
|
|
|
apiCommitter = ToUser(ctx, committer, nil)
|
2020-09-18 20:09:26 +08:00
|
|
|
if userCache != nil {
|
|
|
|
userCache[commit.Committer.Email] = committer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve parent(s) of the commit
|
|
|
|
apiParents := make([]*api.CommitMeta, commit.ParentCount())
|
|
|
|
for i := 0; i < commit.ParentCount(); i++ {
|
|
|
|
sha, _ := commit.ParentID(i)
|
|
|
|
apiParents[i] = &api.CommitMeta{
|
2021-11-17 02:18:25 +08:00
|
|
|
URL: repo.APIURL() + "/git/commits/" + url.PathEscape(sha.String()),
|
2020-09-18 20:09:26 +08:00
|
|
|
SHA: sha.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 11:21:04 +08:00
|
|
|
res := &api.Commit{
|
2020-09-18 20:09:26 +08:00
|
|
|
CommitMeta: &api.CommitMeta{
|
2021-11-17 02:18:25 +08:00
|
|
|
URL: repo.APIURL() + "/git/commits/" + url.PathEscape(commit.ID.String()),
|
2021-11-05 13:30:46 +08:00
|
|
|
SHA: commit.ID.String(),
|
|
|
|
Created: commit.Committer.When,
|
2020-09-18 20:09:26 +08:00
|
|
|
},
|
2021-11-17 02:18:25 +08:00
|
|
|
HTMLURL: repo.HTMLURL() + "/commit/" + url.PathEscape(commit.ID.String()),
|
2020-09-18 20:09:26 +08:00
|
|
|
RepoCommit: &api.RepoCommit{
|
2021-11-17 02:18:25 +08:00
|
|
|
URL: repo.APIURL() + "/git/commits/" + url.PathEscape(commit.ID.String()),
|
2020-09-18 20:09:26 +08:00
|
|
|
Author: &api.CommitUser{
|
|
|
|
Identity: api.Identity{
|
2021-06-28 06:15:42 +08:00
|
|
|
Name: commit.Author.Name,
|
|
|
|
Email: commit.Author.Email,
|
2020-09-18 20:09:26 +08:00
|
|
|
},
|
|
|
|
Date: commit.Author.When.Format(time.RFC3339),
|
|
|
|
},
|
|
|
|
Committer: &api.CommitUser{
|
|
|
|
Identity: api.Identity{
|
|
|
|
Name: commit.Committer.Name,
|
|
|
|
Email: commit.Committer.Email,
|
|
|
|
},
|
|
|
|
Date: commit.Committer.When.Format(time.RFC3339),
|
|
|
|
},
|
|
|
|
Message: commit.Message(),
|
|
|
|
Tree: &api.CommitMeta{
|
2021-11-17 02:18:25 +08:00
|
|
|
URL: repo.APIURL() + "/git/trees/" + url.PathEscape(commit.ID.String()),
|
2021-11-05 13:30:46 +08:00
|
|
|
SHA: commit.ID.String(),
|
|
|
|
Created: commit.Committer.When,
|
2020-09-18 20:09:26 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Author: apiAuthor,
|
|
|
|
Committer: apiCommitter,
|
|
|
|
Parents: apiParents,
|
2022-10-06 11:21:04 +08:00
|
|
|
}
|
|
|
|
|
2023-05-09 09:06:05 +08:00
|
|
|
// Retrieve verification for commit
|
|
|
|
if opts.Verification {
|
|
|
|
res.RepoCommit.Verification = ToVerification(ctx, commit)
|
|
|
|
}
|
|
|
|
|
2022-10-06 11:21:04 +08:00
|
|
|
// Retrieve files affected by the commit
|
2023-05-09 09:06:05 +08:00
|
|
|
if opts.Files {
|
2022-10-06 11:21:04 +08:00
|
|
|
fileStatus, err := git.GetCommitFileStatus(gitRepo.Ctx, repo.RepoPath(), commit.ID.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-09 09:06:05 +08:00
|
|
|
|
2022-10-06 11:21:04 +08:00
|
|
|
affectedFileList := make([]*api.CommitAffectedFiles, 0, len(fileStatus.Added)+len(fileStatus.Removed)+len(fileStatus.Modified))
|
|
|
|
for _, files := range [][]string{fileStatus.Added, fileStatus.Removed, fileStatus.Modified} {
|
|
|
|
for _, filename := range files {
|
|
|
|
affectedFileList = append(affectedFileList, &api.CommitAffectedFiles{
|
|
|
|
Filename: filename,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 09:06:05 +08:00
|
|
|
res.Files = affectedFileList
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get diff stats for commit
|
|
|
|
if opts.Stat {
|
2022-10-06 11:21:04 +08:00
|
|
|
diff, err := gitdiff.GetDiff(gitRepo, &gitdiff.DiffOptions{
|
|
|
|
AfterCommitID: commit.ID.String(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Stats = &api.CommitStats{
|
2022-03-29 23:47:44 +08:00
|
|
|
Total: diff.TotalAddition + diff.TotalDeletion,
|
|
|
|
Additions: diff.TotalAddition,
|
|
|
|
Deletions: diff.TotalDeletion,
|
2022-10-06 11:21:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
2020-09-18 20:09:26 +08:00
|
|
|
}
|