2020-12-02 22:38:30 +01:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-02 22:38:30 +01:00
|
|
|
|
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2022-12-03 03:48:26 +01:00
|
|
|
"context"
|
2022-01-18 14:18:30 +01:00
|
|
|
"time"
|
|
|
|
|
2020-12-02 22:38:30 +01:00
|
|
|
"code.gitea.io/gitea/models"
|
2024-01-15 03:19:25 +01:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-28 12:58:28 +01:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2023-06-22 15:08:08 +02:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 02:27:50 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-09 20:57:58 +01:00
|
|
|
unit_model "code.gitea.io/gitea/models/unit"
|
2021-12-24 05:26:52 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-12-02 22:38:30 +01:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToRepo converts a Repository to api.Repository
|
2023-06-22 15:08:08 +02: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 22:38:30 +01:00
|
|
|
}
|
|
|
|
|
2023-06-22 15:08:08 +02:00
|
|
|
func innerToRepo(ctx context.Context, repo *repo_model.Repository, permissionInRepo access_model.Permission, isParent bool) *api.Repository {
|
2020-12-02 22:38:30 +01:00
|
|
|
var parent *api.Repository
|
|
|
|
|
2023-06-22 15:08:08 +02: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 22:38:30 +01:00
|
|
|
cloneLink := repo.CloneLink()
|
|
|
|
permission := &api.Permission{
|
2023-06-22 15:08:08 +02: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 22:38:30 +01:00
|
|
|
}
|
|
|
|
if !isParent {
|
2022-12-03 03:48:26 +01:00
|
|
|
err := repo.GetBaseRepo(ctx)
|
2020-12-02 22:38:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if repo.BaseRepo != nil {
|
2023-06-22 15:08:08 +02: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 22:38:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
// check enabled/disabled units
|
2020-12-02 22:38:30 +01:00
|
|
|
hasIssues := false
|
|
|
|
var externalTracker *api.ExternalTracker
|
|
|
|
var internalTracker *api.InternalTracker
|
2022-12-10 03:46:31 +01:00
|
|
|
if unit, err := repo.GetUnit(ctx, unit_model.TypeIssues); err == nil {
|
2020-12-02 22:38:30 +01:00
|
|
|
config := unit.IssuesConfig()
|
|
|
|
hasIssues = true
|
|
|
|
internalTracker = &api.InternalTracker{
|
|
|
|
EnableTimeTracker: config.EnableTimetracker,
|
|
|
|
AllowOnlyContributorsToTrackTime: config.AllowOnlyContributorsToTrackTime,
|
|
|
|
EnableIssueDependencies: config.EnableDependencies,
|
|
|
|
}
|
2022-12-10 03:46:31 +01:00
|
|
|
} else if unit, err := repo.GetUnit(ctx, unit_model.TypeExternalTracker); err == nil {
|
2020-12-02 22:38:30 +01:00
|
|
|
config := unit.ExternalTrackerConfig()
|
|
|
|
hasIssues = true
|
|
|
|
externalTracker = &api.ExternalTracker{
|
2022-10-07 14:49:30 +02:00
|
|
|
ExternalTrackerURL: config.ExternalTrackerURL,
|
|
|
|
ExternalTrackerFormat: config.ExternalTrackerFormat,
|
|
|
|
ExternalTrackerStyle: config.ExternalTrackerStyle,
|
|
|
|
ExternalTrackerRegexpPattern: config.ExternalTrackerRegexpPattern,
|
2020-12-02 22:38:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hasWiki := false
|
|
|
|
var externalWiki *api.ExternalWiki
|
2022-12-10 03:46:31 +01:00
|
|
|
if _, err := repo.GetUnit(ctx, unit_model.TypeWiki); err == nil {
|
2020-12-02 22:38:30 +01:00
|
|
|
hasWiki = true
|
2022-12-10 03:46:31 +01:00
|
|
|
} else if unit, err := repo.GetUnit(ctx, unit_model.TypeExternalWiki); err == nil {
|
2020-12-02 22:38:30 +01: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 23:37:23 +01:00
|
|
|
allowFastForwardOnly := false
|
2022-07-15 10:00:01 +02:00
|
|
|
allowRebaseUpdate := false
|
|
|
|
defaultDeleteBranchAfterMerge := false
|
2021-12-10 02:27:50 +01:00
|
|
|
defaultMergeStyle := repo_model.MergeStyleMerge
|
2023-02-13 07:09:52 +01:00
|
|
|
defaultAllowMaintainerEdit := false
|
2022-12-10 03:46:31 +01:00
|
|
|
if unit, err := repo.GetUnit(ctx, unit_model.TypePullRequests); err == nil {
|
2020-12-02 22:38:30 +01:00
|
|
|
config := unit.PullRequestsConfig()
|
|
|
|
hasPullRequests = true
|
|
|
|
ignoreWhitespaceConflicts = config.IgnoreWhitespaceConflicts
|
|
|
|
allowMerge = config.AllowMerge
|
|
|
|
allowRebase = config.AllowRebase
|
|
|
|
allowRebaseMerge = config.AllowRebaseMerge
|
|
|
|
allowSquash = config.AllowSquash
|
2024-02-12 23:37:23 +01:00
|
|
|
allowFastForwardOnly = config.AllowFastForwardOnly
|
2022-07-15 10:00:01 +02:00
|
|
|
allowRebaseUpdate = config.AllowRebaseUpdate
|
|
|
|
defaultDeleteBranchAfterMerge = config.DefaultDeleteBranchAfterMerge
|
2021-03-27 15:55:40 +01:00
|
|
|
defaultMergeStyle = config.GetDefaultMergeStyle()
|
2023-02-13 07:09:52 +01:00
|
|
|
defaultAllowMaintainerEdit = config.DefaultAllowMaintainerEdit
|
2020-12-02 22:38:30 +01:00
|
|
|
}
|
|
|
|
hasProjects := false
|
2024-03-04 03:56:52 +01:00
|
|
|
projectsMode := repo_model.ProjectsModeAll
|
|
|
|
if unit, err := repo.GetUnit(ctx, unit_model.TypeProjects); err == nil {
|
2020-12-02 22:38:30 +01:00
|
|
|
hasProjects = true
|
2024-03-04 03:56:52 +01:00
|
|
|
config := unit.ProjectsConfig()
|
|
|
|
projectsMode = config.ProjectsMode
|
2020-12-02 22:38:30 +01:00
|
|
|
}
|
|
|
|
|
2023-03-16 18:30:42 +01: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 13:11:03 +01:00
|
|
|
if err := repo.LoadOwner(ctx); err != nil {
|
2020-12-02 22:38:30 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-15 03:19:25 +01:00
|
|
|
numReleases, _ := db.Count[repo_model.Release](ctx, repo_model.FindReleasesOptions{
|
|
|
|
IncludeDrafts: false,
|
|
|
|
IncludeTags: false,
|
|
|
|
RepoID: repo.ID,
|
|
|
|
})
|
2020-12-02 22:38:30 +01:00
|
|
|
|
2021-01-03 00:47:47 +01:00
|
|
|
mirrorInterval := ""
|
2022-01-18 14:18:30 +01:00
|
|
|
var mirrorUpdated time.Time
|
2021-01-03 00:47:47 +01:00
|
|
|
if repo.IsMirror {
|
2023-05-15 21:02:10 +02:00
|
|
|
pullMirror, err := repo_model.GetMirrorByRepoID(ctx, repo.ID)
|
2021-12-10 02:27:50 +01:00
|
|
|
if err == nil {
|
2023-05-15 21:02:10 +02:00
|
|
|
mirrorInterval = pullMirror.Interval.String()
|
|
|
|
mirrorUpdated = pullMirror.UpdatedUnix.AsTime()
|
2021-01-03 00:47:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 05:26:52 +01:00
|
|
|
var transfer *api.RepoTransfer
|
|
|
|
if repo.Status == repo_model.RepositoryPendingTransfer {
|
2022-12-10 03:46:31 +01:00
|
|
|
t, err := models.GetPendingRepositoryTransfer(ctx, repo)
|
2021-12-24 05:26:52 +01:00
|
|
|
if err != nil && !models.IsErrNoPendingTransfer(err) {
|
|
|
|
log.Warn("GetPendingRepositoryTransfer: %v", err)
|
|
|
|
} else {
|
2022-12-10 03:46:31 +01:00
|
|
|
if err := t.LoadAttributes(ctx); err != nil {
|
2021-12-24 05:26:52 +01: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 14:37:34 +01:00
|
|
|
transfer = ToRepoTransfer(ctx, t)
|
2021-12-24 05:26:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 07:33:40 +01:00
|
|
|
var language string
|
|
|
|
if repo.PrimaryLanguage != nil {
|
|
|
|
language = repo.PrimaryLanguage.Language
|
|
|
|
}
|
|
|
|
|
|
|
|
repoAPIURL := repo.APIURL()
|
|
|
|
|
2020-12-02 22:38:30 +01:00
|
|
|
return &api.Repository{
|
2022-07-15 10:00:01 +02:00
|
|
|
ID: repo.ID,
|
2023-06-22 15:08:08 +02:00
|
|
|
Owner: ToUserWithAccessMode(ctx, repo.Owner, permissionInRepo.AccessMode),
|
2022-07-15 10:00:01 +02: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 21:02:47 +02:00
|
|
|
URL: repoAPIURL,
|
2022-07-15 10:00:01 +02: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 16:46:26 +02:00
|
|
|
ArchivedAt: repo.ArchivedUnix.AsTime(),
|
2022-07-15 10:00:01 +02:00
|
|
|
Permissions: permission,
|
|
|
|
HasIssues: hasIssues,
|
|
|
|
ExternalTracker: externalTracker,
|
|
|
|
InternalTracker: internalTracker,
|
|
|
|
HasWiki: hasWiki,
|
|
|
|
HasProjects: hasProjects,
|
2024-03-04 03:56:52 +01:00
|
|
|
ProjectsMode: string(projectsMode),
|
2023-03-16 18:30:42 +01:00
|
|
|
HasReleases: hasReleases,
|
|
|
|
HasPackages: hasPackages,
|
|
|
|
HasActions: hasActions,
|
2022-07-15 10:00:01 +02:00
|
|
|
ExternalWiki: externalWiki,
|
|
|
|
HasPullRequests: hasPullRequests,
|
|
|
|
IgnoreWhitespaceConflicts: ignoreWhitespaceConflicts,
|
|
|
|
AllowMerge: allowMerge,
|
|
|
|
AllowRebase: allowRebase,
|
|
|
|
AllowRebaseMerge: allowRebaseMerge,
|
|
|
|
AllowSquash: allowSquash,
|
2024-02-12 23:37:23 +01:00
|
|
|
AllowFastForwardOnly: allowFastForwardOnly,
|
2022-07-15 10:00:01 +02:00
|
|
|
AllowRebaseUpdate: allowRebaseUpdate,
|
|
|
|
DefaultDeleteBranchAfterMerge: defaultDeleteBranchAfterMerge,
|
|
|
|
DefaultMergeStyle: string(defaultMergeStyle),
|
2023-02-13 07:09:52 +01: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 14:37:34 +01:00
|
|
|
AvatarURL: repo.AvatarLink(ctx),
|
2022-07-15 10:00:01 +02:00
|
|
|
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
|
|
|
|
MirrorInterval: mirrorInterval,
|
|
|
|
MirrorUpdated: mirrorUpdated,
|
|
|
|
RepoTransfer: transfer,
|
2021-12-24 05:26:52 +01: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 14:37:34 +01:00
|
|
|
func ToRepoTransfer(ctx context.Context, t *models.RepoTransfer) *api.RepoTransfer {
|
|
|
|
teams, _ := ToTeams(ctx, t.Teams, false)
|
2021-12-24 05:26:52 +01: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 14:37:34 +01:00
|
|
|
Doer: ToUser(ctx, t.Doer, nil),
|
|
|
|
Recipient: ToUser(ctx, t.Recipient, nil),
|
2021-12-24 05:26:52 +01:00
|
|
|
Teams: teams,
|
2020-12-02 22:38:30 +01:00
|
|
|
}
|
|
|
|
}
|