2020-12-02 14:38:30 -07:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-02 14:38:30 -07:00
|
|
|
|
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2022-12-02 19:48:26 -07:00
|
|
|
"context"
|
2022-01-18 06:18:30 -07:00
|
|
|
"time"
|
|
|
|
|
2020-12-02 14:38:30 -07:00
|
|
|
"code.gitea.io/gitea/models"
|
2024-01-14 19:19:25 -07:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-28 04:58:28 -07:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2023-06-22 07:08:08 -06:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-09 18:27:50 -07:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-09 12:57:58 -07:00
|
|
|
unit_model "code.gitea.io/gitea/models/unit"
|
2021-12-23 21:26:52 -07:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-12-02 14:38:30 -07:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToRepo converts a Repository to api.Repository
|
2023-06-22 07:08:08 -06:00
|
|
|
func ToRepo(ctx context.Context, repo *repo_model.Repository, permissionInRepo access_model.Permission) *api.Repository {
|
|
|
|
return innerToRepo(ctx, repo, permissionInRepo, false)
|
2020-12-02 14:38:30 -07:00
|
|
|
}
|
|
|
|
|
2023-06-22 07:08:08 -06:00
|
|
|
func innerToRepo(ctx context.Context, repo *repo_model.Repository, permissionInRepo access_model.Permission, isParent bool) *api.Repository {
|
2020-12-02 14:38:30 -07:00
|
|
|
var parent *api.Repository
|
|
|
|
|
2023-06-22 07:08:08 -06:00
|
|
|
if permissionInRepo.Units == nil && permissionInRepo.UnitsMode == nil {
|
|
|
|
// If Units and UnitsMode are both nil, it means that it's a hard coded permission,
|
|
|
|
// like access_model.Permission{AccessMode: perm.AccessModeAdmin}.
|
|
|
|
// So we need to load units for the repo, or UnitAccessMode will always return perm.AccessModeNone.
|
|
|
|
_ = repo.LoadUnits(ctx) // the error is not important, so ignore it
|
|
|
|
permissionInRepo.Units = repo.Units
|
|
|
|
}
|
|
|
|
|
2020-12-02 14:38:30 -07:00
|
|
|
cloneLink := repo.CloneLink()
|
|
|
|
permission := &api.Permission{
|
2023-06-22 07:08:08 -06:00
|
|
|
Admin: permissionInRepo.AccessMode >= perm.AccessModeAdmin,
|
|
|
|
Push: permissionInRepo.UnitAccessMode(unit_model.TypeCode) >= perm.AccessModeWrite,
|
|
|
|
Pull: permissionInRepo.UnitAccessMode(unit_model.TypeCode) >= perm.AccessModeRead,
|
2020-12-02 14:38:30 -07:00
|
|
|
}
|
|
|
|
if !isParent {
|
2022-12-02 19:48:26 -07:00
|
|
|
err := repo.GetBaseRepo(ctx)
|
2020-12-02 14:38:30 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if repo.BaseRepo != nil {
|
2023-06-22 07:08:08 -06:00
|
|
|
// FIXME: The permission of the parent repo is not correct.
|
|
|
|
// It's the permission of the current repo, so it's probably different from the parent repo.
|
|
|
|
// But there isn't a good way to get the permission of the parent repo, because the doer is not passed in.
|
|
|
|
// Use the permission of the current repo to keep the behavior consistent with the old API.
|
|
|
|
// Maybe the right way is setting the permission of the parent repo to nil, empty is better than wrong.
|
|
|
|
parent = innerToRepo(ctx, repo.BaseRepo, permissionInRepo, true)
|
2020-12-02 14:38:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 10:46:10 -07:00
|
|
|
// check enabled/disabled units
|
2020-12-02 14:38:30 -07:00
|
|
|
hasIssues := false
|
|
|
|
var externalTracker *api.ExternalTracker
|
|
|
|
var internalTracker *api.InternalTracker
|
2022-12-09 19:46:31 -07:00
|
|
|
if unit, err := repo.GetUnit(ctx, unit_model.TypeIssues); err == nil {
|
2020-12-02 14:38:30 -07:00
|
|
|
config := unit.IssuesConfig()
|
|
|
|
hasIssues = true
|
|
|
|
internalTracker = &api.InternalTracker{
|
|
|
|
EnableTimeTracker: config.EnableTimetracker,
|
|
|
|
AllowOnlyContributorsToTrackTime: config.AllowOnlyContributorsToTrackTime,
|
|
|
|
EnableIssueDependencies: config.EnableDependencies,
|
|
|
|
}
|
2022-12-09 19:46:31 -07:00
|
|
|
} else if unit, err := repo.GetUnit(ctx, unit_model.TypeExternalTracker); err == nil {
|
2020-12-02 14:38:30 -07:00
|
|
|
config := unit.ExternalTrackerConfig()
|
|
|
|
hasIssues = true
|
|
|
|
externalTracker = &api.ExternalTracker{
|
2022-10-07 06:49:30 -06:00
|
|
|
ExternalTrackerURL: config.ExternalTrackerURL,
|
|
|
|
ExternalTrackerFormat: config.ExternalTrackerFormat,
|
|
|
|
ExternalTrackerStyle: config.ExternalTrackerStyle,
|
|
|
|
ExternalTrackerRegexpPattern: config.ExternalTrackerRegexpPattern,
|
2020-12-02 14:38:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hasWiki := false
|
|
|
|
var externalWiki *api.ExternalWiki
|
2022-12-09 19:46:31 -07:00
|
|
|
if _, err := repo.GetUnit(ctx, unit_model.TypeWiki); err == nil {
|
2020-12-02 14:38:30 -07:00
|
|
|
hasWiki = true
|
2022-12-09 19:46:31 -07:00
|
|
|
} else if unit, err := repo.GetUnit(ctx, unit_model.TypeExternalWiki); err == nil {
|
2020-12-02 14:38:30 -07:00
|
|
|
hasWiki = true
|
|
|
|
config := unit.ExternalWikiConfig()
|
|
|
|
externalWiki = &api.ExternalWiki{
|
|
|
|
ExternalWikiURL: config.ExternalWikiURL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hasPullRequests := false
|
|
|
|
ignoreWhitespaceConflicts := false
|
|
|
|
allowMerge := false
|
|
|
|
allowRebase := false
|
|
|
|
allowRebaseMerge := false
|
|
|
|
allowSquash := false
|
2024-02-12 15:37:23 -07:00
|
|
|
allowFastForwardOnly := false
|
2022-07-15 02:00:01 -06:00
|
|
|
allowRebaseUpdate := false
|
|
|
|
defaultDeleteBranchAfterMerge := false
|
2021-12-09 18:27:50 -07:00
|
|
|
defaultMergeStyle := repo_model.MergeStyleMerge
|
2023-02-12 23:09:52 -07:00
|
|
|
defaultAllowMaintainerEdit := false
|
2022-12-09 19:46:31 -07:00
|
|
|
if unit, err := repo.GetUnit(ctx, unit_model.TypePullRequests); err == nil {
|
2020-12-02 14:38:30 -07:00
|
|
|
config := unit.PullRequestsConfig()
|
|
|
|
hasPullRequests = true
|
|
|
|
ignoreWhitespaceConflicts = config.IgnoreWhitespaceConflicts
|
|
|
|
allowMerge = config.AllowMerge
|
|
|
|
allowRebase = config.AllowRebase
|
|
|
|
allowRebaseMerge = config.AllowRebaseMerge
|
|
|
|
allowSquash = config.AllowSquash
|
2024-02-12 15:37:23 -07:00
|
|
|
allowFastForwardOnly = config.AllowFastForwardOnly
|
2022-07-15 02:00:01 -06:00
|
|
|
allowRebaseUpdate = config.AllowRebaseUpdate
|
|
|
|
defaultDeleteBranchAfterMerge = config.DefaultDeleteBranchAfterMerge
|
2021-03-27 08:55:40 -06:00
|
|
|
defaultMergeStyle = config.GetDefaultMergeStyle()
|
2023-02-12 23:09:52 -07:00
|
|
|
defaultAllowMaintainerEdit = config.DefaultAllowMaintainerEdit
|
2020-12-02 14:38:30 -07:00
|
|
|
}
|
|
|
|
hasProjects := false
|
2022-12-09 19:46:31 -07:00
|
|
|
if _, err := repo.GetUnit(ctx, unit_model.TypeProjects); err == nil {
|
2020-12-02 14:38:30 -07:00
|
|
|
hasProjects = true
|
|
|
|
}
|
|
|
|
|
2023-03-16 11:30:42 -06: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 05:11:03 -07:00
|
|
|
if err := repo.LoadOwner(ctx); err != nil {
|
2020-12-02 14:38:30 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-14 19:19:25 -07:00
|
|
|
numReleases, _ := db.Count[repo_model.Release](ctx, repo_model.FindReleasesOptions{
|
|
|
|
IncludeDrafts: false,
|
|
|
|
IncludeTags: false,
|
|
|
|
RepoID: repo.ID,
|
|
|
|
})
|
2020-12-02 14:38:30 -07:00
|
|
|
|
2021-01-02 16:47:47 -07:00
|
|
|
mirrorInterval := ""
|
2022-01-18 06:18:30 -07:00
|
|
|
var mirrorUpdated time.Time
|
2021-01-02 16:47:47 -07:00
|
|
|
if repo.IsMirror {
|
2023-05-15 13:02:10 -06:00
|
|
|
pullMirror, err := repo_model.GetMirrorByRepoID(ctx, repo.ID)
|
2021-12-09 18:27:50 -07:00
|
|
|
if err == nil {
|
2023-05-15 13:02:10 -06:00
|
|
|
mirrorInterval = pullMirror.Interval.String()
|
|
|
|
mirrorUpdated = pullMirror.UpdatedUnix.AsTime()
|
2021-01-02 16:47:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 21:26:52 -07:00
|
|
|
var transfer *api.RepoTransfer
|
|
|
|
if repo.Status == repo_model.RepositoryPendingTransfer {
|
2022-12-09 19:46:31 -07:00
|
|
|
t, err := models.GetPendingRepositoryTransfer(ctx, repo)
|
2021-12-23 21:26:52 -07:00
|
|
|
if err != nil && !models.IsErrNoPendingTransfer(err) {
|
|
|
|
log.Warn("GetPendingRepositoryTransfer: %v", err)
|
|
|
|
} else {
|
2022-12-09 19:46:31 -07:00
|
|
|
if err := t.LoadAttributes(ctx); err != nil {
|
2021-12-23 21:26:52 -07: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 06:37:34 -07:00
|
|
|
transfer = ToRepoTransfer(ctx, t)
|
2021-12-23 21:26:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 23:33:40 -07:00
|
|
|
var language string
|
|
|
|
if repo.PrimaryLanguage != nil {
|
|
|
|
language = repo.PrimaryLanguage.Language
|
|
|
|
}
|
|
|
|
|
|
|
|
repoAPIURL := repo.APIURL()
|
|
|
|
|
2020-12-02 14:38:30 -07:00
|
|
|
return &api.Repository{
|
2022-07-15 02:00:01 -06:00
|
|
|
ID: repo.ID,
|
2023-06-22 07:08:08 -06:00
|
|
|
Owner: ToUserWithAccessMode(ctx, repo.Owner, permissionInRepo.AccessMode),
|
2022-07-15 02:00:01 -06: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(),
|
2023-09-24 13:02:47 -06:00
|
|
|
URL: repoAPIURL,
|
2022-07-15 02:00:01 -06:00
|
|
|
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 08:46:26 -06:00
|
|
|
ArchivedAt: repo.ArchivedUnix.AsTime(),
|
2022-07-15 02:00:01 -06:00
|
|
|
Permissions: permission,
|
|
|
|
HasIssues: hasIssues,
|
|
|
|
ExternalTracker: externalTracker,
|
|
|
|
InternalTracker: internalTracker,
|
|
|
|
HasWiki: hasWiki,
|
|
|
|
HasProjects: hasProjects,
|
2023-03-16 11:30:42 -06:00
|
|
|
HasReleases: hasReleases,
|
|
|
|
HasPackages: hasPackages,
|
|
|
|
HasActions: hasActions,
|
2022-07-15 02:00:01 -06:00
|
|
|
ExternalWiki: externalWiki,
|
|
|
|
HasPullRequests: hasPullRequests,
|
|
|
|
IgnoreWhitespaceConflicts: ignoreWhitespaceConflicts,
|
|
|
|
AllowMerge: allowMerge,
|
|
|
|
AllowRebase: allowRebase,
|
|
|
|
AllowRebaseMerge: allowRebaseMerge,
|
|
|
|
AllowSquash: allowSquash,
|
2024-02-12 15:37:23 -07:00
|
|
|
AllowFastForwardOnly: allowFastForwardOnly,
|
2022-07-15 02:00:01 -06:00
|
|
|
AllowRebaseUpdate: allowRebaseUpdate,
|
|
|
|
DefaultDeleteBranchAfterMerge: defaultDeleteBranchAfterMerge,
|
|
|
|
DefaultMergeStyle: string(defaultMergeStyle),
|
2023-02-12 23:09:52 -07: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 06:37:34 -07:00
|
|
|
AvatarURL: repo.AvatarLink(ctx),
|
2022-07-15 02:00:01 -06:00
|
|
|
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
|
|
|
|
MirrorInterval: mirrorInterval,
|
|
|
|
MirrorUpdated: mirrorUpdated,
|
|
|
|
RepoTransfer: transfer,
|
2021-12-23 21:26:52 -07: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 06:37:34 -07:00
|
|
|
func ToRepoTransfer(ctx context.Context, t *models.RepoTransfer) *api.RepoTransfer {
|
|
|
|
teams, _ := ToTeams(ctx, t.Teams, false)
|
2021-12-23 21:26:52 -07: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 06:37:34 -07:00
|
|
|
Doer: ToUser(ctx, t.Doer, nil),
|
|
|
|
Recipient: ToUser(ctx, t.Recipient, nil),
|
2021-12-23 21:26:52 -07:00
|
|
|
Teams: teams,
|
2020-12-02 14:38:30 -07:00
|
|
|
}
|
|
|
|
}
|