2020-12-03 05:38:30 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-03 05:38:30 +08:00
|
|
|
|
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2022-12-03 10:48:26 +08:00
|
|
|
"context"
|
2022-01-18 21:18:30 +08:00
|
|
|
"time"
|
|
|
|
|
2020-12-03 05:38:30 +08:00
|
|
|
"code.gitea.io/gitea/models"
|
2021-11-28 19:58:28 +08:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 03:57:58 +08:00
|
|
|
unit_model "code.gitea.io/gitea/models/unit"
|
2021-12-24 12:26:52 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-12-03 05:38:30 +08:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToRepo converts a Repository to api.Repository
|
2022-12-03 10:48:26 +08:00
|
|
|
func ToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.AccessMode) *api.Repository {
|
|
|
|
return innerToRepo(ctx, repo, mode, false)
|
2020-12-03 05:38:30 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 10:48:26 +08:00
|
|
|
func innerToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.AccessMode, isParent bool) *api.Repository {
|
2020-12-03 05:38:30 +08:00
|
|
|
var parent *api.Repository
|
|
|
|
|
|
|
|
cloneLink := repo.CloneLink()
|
|
|
|
permission := &api.Permission{
|
2021-11-28 19:58:28 +08:00
|
|
|
Admin: mode >= perm.AccessModeAdmin,
|
|
|
|
Push: mode >= perm.AccessModeWrite,
|
|
|
|
Pull: mode >= perm.AccessModeRead,
|
2020-12-03 05:38:30 +08:00
|
|
|
}
|
|
|
|
if !isParent {
|
2022-12-03 10:48:26 +08:00
|
|
|
err := repo.GetBaseRepo(ctx)
|
2020-12-03 05:38:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if repo.BaseRepo != nil {
|
2022-12-03 10:48:26 +08:00
|
|
|
parent = innerToRepo(ctx, repo.BaseRepo, mode, true)
|
2020-12-03 05:38:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
// check enabled/disabled units
|
2020-12-03 05:38:30 +08:00
|
|
|
hasIssues := false
|
|
|
|
var externalTracker *api.ExternalTracker
|
|
|
|
var internalTracker *api.InternalTracker
|
2022-12-10 10:46:31 +08:00
|
|
|
if unit, err := repo.GetUnit(ctx, unit_model.TypeIssues); err == nil {
|
2020-12-03 05:38:30 +08:00
|
|
|
config := unit.IssuesConfig()
|
|
|
|
hasIssues = true
|
|
|
|
internalTracker = &api.InternalTracker{
|
|
|
|
EnableTimeTracker: config.EnableTimetracker,
|
|
|
|
AllowOnlyContributorsToTrackTime: config.AllowOnlyContributorsToTrackTime,
|
|
|
|
EnableIssueDependencies: config.EnableDependencies,
|
|
|
|
}
|
2022-12-10 10:46:31 +08:00
|
|
|
} else if unit, err := repo.GetUnit(ctx, unit_model.TypeExternalTracker); err == nil {
|
2020-12-03 05:38:30 +08:00
|
|
|
config := unit.ExternalTrackerConfig()
|
|
|
|
hasIssues = true
|
|
|
|
externalTracker = &api.ExternalTracker{
|
2022-10-07 20:49:30 +08:00
|
|
|
ExternalTrackerURL: config.ExternalTrackerURL,
|
|
|
|
ExternalTrackerFormat: config.ExternalTrackerFormat,
|
|
|
|
ExternalTrackerStyle: config.ExternalTrackerStyle,
|
|
|
|
ExternalTrackerRegexpPattern: config.ExternalTrackerRegexpPattern,
|
2020-12-03 05:38:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hasWiki := false
|
|
|
|
var externalWiki *api.ExternalWiki
|
2022-12-10 10:46:31 +08:00
|
|
|
if _, err := repo.GetUnit(ctx, unit_model.TypeWiki); err == nil {
|
2020-12-03 05:38:30 +08:00
|
|
|
hasWiki = true
|
2022-12-10 10:46:31 +08:00
|
|
|
} else if unit, err := repo.GetUnit(ctx, unit_model.TypeExternalWiki); err == nil {
|
2020-12-03 05:38:30 +08:00
|
|
|
hasWiki = true
|
|
|
|
config := unit.ExternalWikiConfig()
|
|
|
|
externalWiki = &api.ExternalWiki{
|
|
|
|
ExternalWikiURL: config.ExternalWikiURL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hasPullRequests := false
|
|
|
|
ignoreWhitespaceConflicts := false
|
|
|
|
allowMerge := false
|
|
|
|
allowRebase := false
|
|
|
|
allowRebaseMerge := false
|
|
|
|
allowSquash := false
|
2022-07-15 16:00:01 +08:00
|
|
|
allowRebaseUpdate := false
|
|
|
|
defaultDeleteBranchAfterMerge := false
|
2021-12-10 09:27:50 +08:00
|
|
|
defaultMergeStyle := repo_model.MergeStyleMerge
|
2023-02-13 14:09:52 +08:00
|
|
|
defaultAllowMaintainerEdit := false
|
2022-12-10 10:46:31 +08:00
|
|
|
if unit, err := repo.GetUnit(ctx, unit_model.TypePullRequests); err == nil {
|
2020-12-03 05:38:30 +08:00
|
|
|
config := unit.PullRequestsConfig()
|
|
|
|
hasPullRequests = true
|
|
|
|
ignoreWhitespaceConflicts = config.IgnoreWhitespaceConflicts
|
|
|
|
allowMerge = config.AllowMerge
|
|
|
|
allowRebase = config.AllowRebase
|
|
|
|
allowRebaseMerge = config.AllowRebaseMerge
|
|
|
|
allowSquash = config.AllowSquash
|
2022-07-15 16:00:01 +08:00
|
|
|
allowRebaseUpdate = config.AllowRebaseUpdate
|
|
|
|
defaultDeleteBranchAfterMerge = config.DefaultDeleteBranchAfterMerge
|
2021-03-27 22:55:40 +08:00
|
|
|
defaultMergeStyle = config.GetDefaultMergeStyle()
|
2023-02-13 14:09:52 +08:00
|
|
|
defaultAllowMaintainerEdit = config.DefaultAllowMaintainerEdit
|
2020-12-03 05:38:30 +08:00
|
|
|
}
|
|
|
|
hasProjects := false
|
2022-12-10 10:46:31 +08:00
|
|
|
if _, err := repo.GetUnit(ctx, unit_model.TypeProjects); err == nil {
|
2020-12-03 05:38:30 +08:00
|
|
|
hasProjects = true
|
|
|
|
}
|
|
|
|
|
2023-03-17 01:30:42 +08:00
|
|
|
hasReleases := false
|
|
|
|
if _, err := repo.GetUnit(ctx, unit_model.TypeReleases); err == nil {
|
|
|
|
hasReleases = true
|
|
|
|
}
|
|
|
|
|
|
|
|
hasPackages := false
|
|
|
|
if _, err := repo.GetUnit(ctx, unit_model.TypePackages); err == nil {
|
|
|
|
hasPackages = true
|
|
|
|
}
|
|
|
|
|
|
|
|
hasActions := false
|
|
|
|
if _, err := repo.GetUnit(ctx, unit_model.TypeActions); err == nil {
|
|
|
|
hasActions = true
|
|
|
|
}
|
|
|
|
|
2023-02-18 20:11:03 +08:00
|
|
|
if err := repo.LoadOwner(ctx); err != nil {
|
2020-12-03 05:38:30 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-10 10:46:31 +08:00
|
|
|
numReleases, _ := repo_model.GetReleaseCountByRepoID(ctx, repo.ID, repo_model.FindReleasesOptions{IncludeDrafts: false, IncludeTags: false})
|
2020-12-03 05:38:30 +08:00
|
|
|
|
2021-01-03 07:47:47 +08:00
|
|
|
mirrorInterval := ""
|
2022-01-18 21:18:30 +08:00
|
|
|
var mirrorUpdated time.Time
|
2021-01-03 07:47:47 +08:00
|
|
|
if repo.IsMirror {
|
2021-12-10 09:27:50 +08:00
|
|
|
var err error
|
2022-12-10 10:46:31 +08:00
|
|
|
repo.Mirror, err = repo_model.GetMirrorByRepoID(ctx, repo.ID)
|
2021-12-10 09:27:50 +08:00
|
|
|
if err == nil {
|
2021-01-03 07:47:47 +08:00
|
|
|
mirrorInterval = repo.Mirror.Interval.String()
|
2022-01-18 21:18:30 +08:00
|
|
|
mirrorUpdated = repo.Mirror.UpdatedUnix.AsTime()
|
2021-01-03 07:47:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 12:26:52 +08:00
|
|
|
var transfer *api.RepoTransfer
|
|
|
|
if repo.Status == repo_model.RepositoryPendingTransfer {
|
2022-12-10 10:46:31 +08:00
|
|
|
t, err := models.GetPendingRepositoryTransfer(ctx, repo)
|
2021-12-24 12:26:52 +08:00
|
|
|
if err != nil && !models.IsErrNoPendingTransfer(err) {
|
|
|
|
log.Warn("GetPendingRepositoryTransfer: %v", err)
|
|
|
|
} else {
|
2022-12-10 10:46:31 +08:00
|
|
|
if err := t.LoadAttributes(ctx); err != nil {
|
2021-12-24 12:26:52 +08:00
|
|
|
log.Warn("LoadAttributes of RepoTransfer: %v", err)
|
|
|
|
} 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
|
|
|
transfer = ToRepoTransfer(ctx, t)
|
2021-12-24 12:26:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 14:33:40 +08:00
|
|
|
var language string
|
|
|
|
if repo.PrimaryLanguage != nil {
|
|
|
|
language = repo.PrimaryLanguage.Language
|
|
|
|
}
|
|
|
|
|
|
|
|
repoAPIURL := repo.APIURL()
|
|
|
|
|
2020-12-03 05:38:30 +08:00
|
|
|
return &api.Repository{
|
2022-07-15 16:00:01 +08:00
|
|
|
ID: repo.ID,
|
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
|
|
|
Owner: ToUserWithAccessMode(ctx, repo.Owner, mode),
|
2022-07-15 16:00:01 +08:00
|
|
|
Name: repo.Name,
|
|
|
|
FullName: repo.FullName(),
|
|
|
|
Description: repo.Description,
|
|
|
|
Private: repo.IsPrivate,
|
|
|
|
Template: repo.IsTemplate,
|
|
|
|
Empty: repo.IsEmpty,
|
|
|
|
Archived: repo.IsArchived,
|
|
|
|
Size: int(repo.Size / 1024),
|
|
|
|
Fork: repo.IsFork,
|
|
|
|
Parent: parent,
|
|
|
|
Mirror: repo.IsMirror,
|
|
|
|
HTMLURL: repo.HTMLURL(),
|
|
|
|
SSHURL: cloneLink.SSH,
|
|
|
|
CloneURL: cloneLink.HTTPS,
|
|
|
|
OriginalURL: repo.SanitizedOriginalURL(),
|
|
|
|
Website: repo.Website,
|
|
|
|
Language: language,
|
|
|
|
LanguagesURL: repoAPIURL + "/languages",
|
|
|
|
Stars: repo.NumStars,
|
|
|
|
Forks: repo.NumForks,
|
|
|
|
Watchers: repo.NumWatches,
|
|
|
|
OpenIssues: repo.NumOpenIssues,
|
|
|
|
OpenPulls: repo.NumOpenPulls,
|
|
|
|
Releases: int(numReleases),
|
|
|
|
DefaultBranch: repo.DefaultBranch,
|
|
|
|
Created: repo.CreatedUnix.AsTime(),
|
|
|
|
Updated: repo.UpdatedUnix.AsTime(),
|
2023-04-26 22:46:26 +08:00
|
|
|
ArchivedAt: repo.ArchivedUnix.AsTime(),
|
2022-07-15 16:00:01 +08:00
|
|
|
Permissions: permission,
|
|
|
|
HasIssues: hasIssues,
|
|
|
|
ExternalTracker: externalTracker,
|
|
|
|
InternalTracker: internalTracker,
|
|
|
|
HasWiki: hasWiki,
|
|
|
|
HasProjects: hasProjects,
|
2023-03-17 01:30:42 +08:00
|
|
|
HasReleases: hasReleases,
|
|
|
|
HasPackages: hasPackages,
|
|
|
|
HasActions: hasActions,
|
2022-07-15 16:00:01 +08:00
|
|
|
ExternalWiki: externalWiki,
|
|
|
|
HasPullRequests: hasPullRequests,
|
|
|
|
IgnoreWhitespaceConflicts: ignoreWhitespaceConflicts,
|
|
|
|
AllowMerge: allowMerge,
|
|
|
|
AllowRebase: allowRebase,
|
|
|
|
AllowRebaseMerge: allowRebaseMerge,
|
|
|
|
AllowSquash: allowSquash,
|
|
|
|
AllowRebaseUpdate: allowRebaseUpdate,
|
|
|
|
DefaultDeleteBranchAfterMerge: defaultDeleteBranchAfterMerge,
|
|
|
|
DefaultMergeStyle: string(defaultMergeStyle),
|
2023-02-13 14:09:52 +08:00
|
|
|
DefaultAllowMaintainerEdit: defaultAllowMaintainerEdit,
|
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
|
|
|
AvatarURL: repo.AvatarLink(ctx),
|
2022-07-15 16:00:01 +08:00
|
|
|
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
|
|
|
|
MirrorInterval: mirrorInterval,
|
|
|
|
MirrorUpdated: mirrorUpdated,
|
|
|
|
RepoTransfer: transfer,
|
2021-12-24 12:26:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToRepoTransfer convert a models.RepoTransfer to a structs.RepeTransfer
|
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 ToRepoTransfer(ctx context.Context, t *models.RepoTransfer) *api.RepoTransfer {
|
|
|
|
teams, _ := ToTeams(ctx, t.Teams, false)
|
2021-12-24 12:26:52 +08:00
|
|
|
|
|
|
|
return &api.RepoTransfer{
|
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
|
|
|
Doer: ToUser(ctx, t.Doer, nil),
|
|
|
|
Recipient: ToUser(ctx, t.Recipient, nil),
|
2021-12-24 12:26:52 +08:00
|
|
|
Teams: teams,
|
2020-12-03 05:38:30 +08:00
|
|
|
}
|
|
|
|
}
|