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