2020-12-03 00:38:30 +03:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-03 00:38:30 +03:00
|
|
|
|
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2022-12-03 05:48:26 +03:00
|
|
|
"context"
|
2022-01-18 16:18:30 +03:00
|
|
|
"time"
|
|
|
|
|
2020-12-03 00:38:30 +03:00
|
|
|
"code.gitea.io/gitea/models"
|
2024-01-15 05:19:25 +03:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-28 14:58:28 +03:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2023-06-22 16:08:08 +03:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 04:27:50 +03:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-09 22:57:58 +03:00
|
|
|
unit_model "code.gitea.io/gitea/models/unit"
|
2021-12-24 07:26:52 +03:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-12-03 00:38:30 +03:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToRepo converts a Repository to api.Repository
|
2023-06-22 16:08:08 +03:00
|
|
|
func ToRepo(ctx context.Context, repo *repo_model.Repository, permissionInRepo access_model.Permission) *api.Repository {
|
|
|
|
return innerToRepo(ctx, repo, permissionInRepo, false)
|
2020-12-03 00:38:30 +03:00
|
|
|
}
|
|
|
|
|
2023-06-22 16:08:08 +03:00
|
|
|
func innerToRepo(ctx context.Context, repo *repo_model.Repository, permissionInRepo access_model.Permission, isParent bool) *api.Repository {
|
2020-12-03 00:38:30 +03:00
|
|
|
var parent *api.Repository
|
|
|
|
|
2023-06-22 16:08:08 +03: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-03 00:38:30 +03:00
|
|
|
cloneLink := repo.CloneLink()
|
|
|
|
permission := &api.Permission{
|
2023-06-22 16:08:08 +03:00
|
|
|
Admin: permissionInRepo.AccessMode >= perm.AccessModeAdmin,
|
|
|
|
Push: permissionInRepo.UnitAccessMode(unit_model.TypeCode) >= perm.AccessModeWrite,
|
|
|
|
Pull: permissionInRepo.UnitAccessMode(unit_model.TypeCode) >= perm.AccessModeRead,
|
2020-12-03 00:38:30 +03:00
|
|
|
}
|
|
|
|
if !isParent {
|
2022-12-03 05:48:26 +03:00
|
|
|
err := repo.GetBaseRepo(ctx)
|
2020-12-03 00:38:30 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if repo.BaseRepo != nil {
|
2023-06-22 16:08:08 +03: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-03 00:38:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 20:46:10 +03:00
|
|
|
// check enabled/disabled units
|
2020-12-03 00:38:30 +03:00
|
|
|
hasIssues := false
|
|
|
|
var externalTracker *api.ExternalTracker
|
|
|
|
var internalTracker *api.InternalTracker
|
2022-12-10 05:46:31 +03:00
|
|
|
if unit, err := repo.GetUnit(ctx, unit_model.TypeIssues); err == nil {
|
2020-12-03 00:38:30 +03:00
|
|
|
config := unit.IssuesConfig()
|
|
|
|
hasIssues = true
|
|
|
|
internalTracker = &api.InternalTracker{
|
|
|
|
EnableTimeTracker: config.EnableTimetracker,
|
|
|
|
AllowOnlyContributorsToTrackTime: config.AllowOnlyContributorsToTrackTime,
|
|
|
|
EnableIssueDependencies: config.EnableDependencies,
|
|
|
|
}
|
2022-12-10 05:46:31 +03:00
|
|
|
} else if unit, err := repo.GetUnit(ctx, unit_model.TypeExternalTracker); err == nil {
|
2020-12-03 00:38:30 +03:00
|
|
|
config := unit.ExternalTrackerConfig()
|
|
|
|
hasIssues = true
|
|
|
|
externalTracker = &api.ExternalTracker{
|
2022-10-07 15:49:30 +03:00
|
|
|
ExternalTrackerURL: config.ExternalTrackerURL,
|
|
|
|
ExternalTrackerFormat: config.ExternalTrackerFormat,
|
|
|
|
ExternalTrackerStyle: config.ExternalTrackerStyle,
|
|
|
|
ExternalTrackerRegexpPattern: config.ExternalTrackerRegexpPattern,
|
2020-12-03 00:38:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hasWiki := false
|
2024-04-16 23:51:36 +03:00
|
|
|
globallyEditableWiki := false
|
2020-12-03 00:38:30 +03:00
|
|
|
var externalWiki *api.ExternalWiki
|
2024-04-16 23:51:36 +03:00
|
|
|
if wikiUnit, err := repo.GetUnit(ctx, unit_model.TypeWiki); err == nil {
|
2020-12-03 00:38:30 +03:00
|
|
|
hasWiki = true
|
2024-04-16 23:51:36 +03:00
|
|
|
if wikiUnit.DefaultPermissions == repo_model.UnitAccessModeWrite {
|
|
|
|
globallyEditableWiki = true
|
|
|
|
}
|
2022-12-10 05:46:31 +03:00
|
|
|
} else if unit, err := repo.GetUnit(ctx, unit_model.TypeExternalWiki); err == nil {
|
2020-12-03 00:38:30 +03: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-13 01:37:23 +03:00
|
|
|
allowFastForwardOnly := false
|
2022-07-15 11:00:01 +03:00
|
|
|
allowRebaseUpdate := false
|
|
|
|
defaultDeleteBranchAfterMerge := false
|
2021-12-10 04:27:50 +03:00
|
|
|
defaultMergeStyle := repo_model.MergeStyleMerge
|
2023-02-13 09:09:52 +03:00
|
|
|
defaultAllowMaintainerEdit := false
|
2022-12-10 05:46:31 +03:00
|
|
|
if unit, err := repo.GetUnit(ctx, unit_model.TypePullRequests); err == nil {
|
2020-12-03 00:38:30 +03:00
|
|
|
config := unit.PullRequestsConfig()
|
|
|
|
hasPullRequests = true
|
|
|
|
ignoreWhitespaceConflicts = config.IgnoreWhitespaceConflicts
|
|
|
|
allowMerge = config.AllowMerge
|
|
|
|
allowRebase = config.AllowRebase
|
|
|
|
allowRebaseMerge = config.AllowRebaseMerge
|
|
|
|
allowSquash = config.AllowSquash
|
2024-02-13 01:37:23 +03:00
|
|
|
allowFastForwardOnly = config.AllowFastForwardOnly
|
2022-07-15 11:00:01 +03:00
|
|
|
allowRebaseUpdate = config.AllowRebaseUpdate
|
|
|
|
defaultDeleteBranchAfterMerge = config.DefaultDeleteBranchAfterMerge
|
2021-03-27 17:55:40 +03:00
|
|
|
defaultMergeStyle = config.GetDefaultMergeStyle()
|
2023-02-13 09:09:52 +03:00
|
|
|
defaultAllowMaintainerEdit = config.DefaultAllowMaintainerEdit
|
2020-12-03 00:38:30 +03:00
|
|
|
}
|
|
|
|
hasProjects := false
|
2022-12-10 05:46:31 +03:00
|
|
|
if _, err := repo.GetUnit(ctx, unit_model.TypeProjects); err == nil {
|
2020-12-03 00:38:30 +03:00
|
|
|
hasProjects = true
|
|
|
|
}
|
|
|
|
|
2023-03-16 20:30:42 +03: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 15:11:03 +03:00
|
|
|
if err := repo.LoadOwner(ctx); err != nil {
|
2020-12-03 00:38:30 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-15 05:19:25 +03:00
|
|
|
numReleases, _ := db.Count[repo_model.Release](ctx, repo_model.FindReleasesOptions{
|
|
|
|
IncludeDrafts: false,
|
|
|
|
IncludeTags: false,
|
|
|
|
RepoID: repo.ID,
|
|
|
|
})
|
2020-12-03 00:38:30 +03:00
|
|
|
|
2021-01-03 02:47:47 +03:00
|
|
|
mirrorInterval := ""
|
2022-01-18 16:18:30 +03:00
|
|
|
var mirrorUpdated time.Time
|
2021-01-03 02:47:47 +03:00
|
|
|
if repo.IsMirror {
|
2023-05-15 22:02:10 +03:00
|
|
|
pullMirror, err := repo_model.GetMirrorByRepoID(ctx, repo.ID)
|
2021-12-10 04:27:50 +03:00
|
|
|
if err == nil {
|
2023-05-15 22:02:10 +03:00
|
|
|
mirrorInterval = pullMirror.Interval.String()
|
|
|
|
mirrorUpdated = pullMirror.UpdatedUnix.AsTime()
|
2021-01-03 02:47:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 07:26:52 +03:00
|
|
|
var transfer *api.RepoTransfer
|
|
|
|
if repo.Status == repo_model.RepositoryPendingTransfer {
|
2022-12-10 05:46:31 +03:00
|
|
|
t, err := models.GetPendingRepositoryTransfer(ctx, repo)
|
2021-12-24 07:26:52 +03:00
|
|
|
if err != nil && !models.IsErrNoPendingTransfer(err) {
|
|
|
|
log.Warn("GetPendingRepositoryTransfer: %v", err)
|
|
|
|
} else {
|
2022-12-10 05:46:31 +03:00
|
|
|
if err := t.LoadAttributes(ctx); err != nil {
|
2021-12-24 07:26:52 +03: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 16:37:34 +03:00
|
|
|
transfer = ToRepoTransfer(ctx, t)
|
2021-12-24 07:26:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 09:33:40 +03:00
|
|
|
var language string
|
|
|
|
if repo.PrimaryLanguage != nil {
|
|
|
|
language = repo.PrimaryLanguage.Language
|
|
|
|
}
|
|
|
|
|
|
|
|
repoAPIURL := repo.APIURL()
|
|
|
|
|
2020-12-03 00:38:30 +03:00
|
|
|
return &api.Repository{
|
2022-07-15 11:00:01 +03:00
|
|
|
ID: repo.ID,
|
2023-06-22 16:08:08 +03:00
|
|
|
Owner: ToUserWithAccessMode(ctx, repo.Owner, permissionInRepo.AccessMode),
|
2022-07-15 11:00:01 +03: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 22:02:47 +03:00
|
|
|
URL: repoAPIURL,
|
2022-07-15 11:00:01 +03: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 17:46:26 +03:00
|
|
|
ArchivedAt: repo.ArchivedUnix.AsTime(),
|
2022-07-15 11:00:01 +03:00
|
|
|
Permissions: permission,
|
|
|
|
HasIssues: hasIssues,
|
|
|
|
ExternalTracker: externalTracker,
|
|
|
|
InternalTracker: internalTracker,
|
|
|
|
HasWiki: hasWiki,
|
[GITEA] Allow changing the repo Wiki branch to main
Previously, the repo wiki was hardcoded to use `master` as its branch,
this change makes it possible to use `main` (or something else, governed
by `[repository].DEFAULT_BRANCH`, a setting that already exists and
defaults to `main`).
The way it is done is that a new column is added to the `repository`
table: `wiki_branch`. The migration will make existing repositories
default to `master`, for compatibility's sake, even if they don't have a
Wiki (because it's easier to do that). Newly created repositories will
default to `[repository].DEFAULT_BRANCH` instead.
The Wiki service was updated to use the branch name stored in the
database, and fall back to the default if it is empty.
Old repositories with Wikis using the older `master` branch will have
the option to do a one-time transition to `main`, available via the
repository settings in the "Danger Zone". This option will only be
available for repositories that have the internal wiki enabled, it is
not empty, and the wiki branch is not `[repository].DEFAULT_BRANCH`.
When migrating a repository with a Wiki, Forgejo will use the same
branch name for the wiki as the source repository did. If that's not the
same as the default, the option to normalize it will be available after
the migration's done.
Additionally, the `/api/v1/{owner}/{repo}` endpoint was updated: it will
now include the wiki branch name in `GET` requests, and allow changing
the wiki branch via `PATCH`.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit d87c526d2a313fa45093ab49b78bb30322b33298)
2024-01-30 14:18:53 +03:00
|
|
|
WikiBranch: repo.WikiBranch,
|
2024-04-16 23:51:36 +03:00
|
|
|
GloballyEditableWiki: globallyEditableWiki,
|
2022-07-15 11:00:01 +03:00
|
|
|
HasProjects: hasProjects,
|
2023-03-16 20:30:42 +03:00
|
|
|
HasReleases: hasReleases,
|
|
|
|
HasPackages: hasPackages,
|
|
|
|
HasActions: hasActions,
|
2022-07-15 11:00:01 +03:00
|
|
|
ExternalWiki: externalWiki,
|
|
|
|
HasPullRequests: hasPullRequests,
|
|
|
|
IgnoreWhitespaceConflicts: ignoreWhitespaceConflicts,
|
|
|
|
AllowMerge: allowMerge,
|
|
|
|
AllowRebase: allowRebase,
|
|
|
|
AllowRebaseMerge: allowRebaseMerge,
|
|
|
|
AllowSquash: allowSquash,
|
2024-02-13 01:37:23 +03:00
|
|
|
AllowFastForwardOnly: allowFastForwardOnly,
|
2022-07-15 11:00:01 +03:00
|
|
|
AllowRebaseUpdate: allowRebaseUpdate,
|
|
|
|
DefaultDeleteBranchAfterMerge: defaultDeleteBranchAfterMerge,
|
|
|
|
DefaultMergeStyle: string(defaultMergeStyle),
|
2023-02-13 09:09:52 +03: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 16:37:34 +03:00
|
|
|
AvatarURL: repo.AvatarLink(ctx),
|
2022-07-15 11:00:01 +03:00
|
|
|
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
|
|
|
|
MirrorInterval: mirrorInterval,
|
|
|
|
MirrorUpdated: mirrorUpdated,
|
|
|
|
RepoTransfer: transfer,
|
2021-12-24 07:26:52 +03: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 16:37:34 +03:00
|
|
|
func ToRepoTransfer(ctx context.Context, t *models.RepoTransfer) *api.RepoTransfer {
|
|
|
|
teams, _ := ToTeams(ctx, t.Teams, false)
|
2021-12-24 07:26:52 +03: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 16:37:34 +03:00
|
|
|
Doer: ToUser(ctx, t.Doer, nil),
|
|
|
|
Recipient: ToUser(ctx, t.Recipient, nil),
|
2021-12-24 07:26:52 +03:00
|
|
|
Teams: teams,
|
2020-12-03 00:38:30 +03:00
|
|
|
}
|
|
|
|
}
|