mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-09 22:43:11 +01:00
bd820aa9c5
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.
175 lines
3.9 KiB
Go
175 lines
3.9 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package convert
|
|
|
|
import (
|
|
"context"
|
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/log"
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
)
|
|
|
|
// ToComment converts a issues_model.Comment to the api.Comment format
|
|
func ToComment(ctx context.Context, c *issues_model.Comment) *api.Comment {
|
|
return &api.Comment{
|
|
ID: c.ID,
|
|
Poster: ToUser(ctx, c.Poster, nil),
|
|
HTMLURL: c.HTMLURL(),
|
|
IssueURL: c.IssueURL(),
|
|
PRURL: c.PRURL(),
|
|
Body: c.Content,
|
|
Attachments: ToAttachments(c.Attachments),
|
|
Created: c.CreatedUnix.AsTime(),
|
|
Updated: c.UpdatedUnix.AsTime(),
|
|
}
|
|
}
|
|
|
|
// ToTimelineComment converts a issues_model.Comment to the api.TimelineComment format
|
|
func ToTimelineComment(ctx context.Context, c *issues_model.Comment, doer *user_model.User) *api.TimelineComment {
|
|
err := c.LoadMilestone(ctx)
|
|
if err != nil {
|
|
log.Error("LoadMilestone: %v", err)
|
|
return nil
|
|
}
|
|
|
|
err = c.LoadAssigneeUserAndTeam()
|
|
if err != nil {
|
|
log.Error("LoadAssigneeUserAndTeam: %v", err)
|
|
return nil
|
|
}
|
|
|
|
err = c.LoadResolveDoer()
|
|
if err != nil {
|
|
log.Error("LoadResolveDoer: %v", err)
|
|
return nil
|
|
}
|
|
|
|
err = c.LoadDepIssueDetails()
|
|
if err != nil {
|
|
log.Error("LoadDepIssueDetails: %v", err)
|
|
return nil
|
|
}
|
|
|
|
err = c.LoadTime()
|
|
if err != nil {
|
|
log.Error("LoadTime: %v", err)
|
|
return nil
|
|
}
|
|
|
|
err = c.LoadLabel()
|
|
if err != nil {
|
|
log.Error("LoadLabel: %v", err)
|
|
return nil
|
|
}
|
|
|
|
comment := &api.TimelineComment{
|
|
ID: c.ID,
|
|
Type: c.Type.String(),
|
|
Poster: ToUser(ctx, c.Poster, nil),
|
|
HTMLURL: c.HTMLURL(),
|
|
IssueURL: c.IssueURL(),
|
|
PRURL: c.PRURL(),
|
|
Body: c.Content,
|
|
Created: c.CreatedUnix.AsTime(),
|
|
Updated: c.UpdatedUnix.AsTime(),
|
|
|
|
OldProjectID: c.OldProjectID,
|
|
ProjectID: c.ProjectID,
|
|
|
|
OldTitle: c.OldTitle,
|
|
NewTitle: c.NewTitle,
|
|
|
|
OldRef: c.OldRef,
|
|
NewRef: c.NewRef,
|
|
|
|
RefAction: c.RefAction.String(),
|
|
RefCommitSHA: c.CommitSHA,
|
|
|
|
ReviewID: c.ReviewID,
|
|
|
|
RemovedAssignee: c.RemovedAssignee,
|
|
}
|
|
|
|
if c.OldMilestone != nil {
|
|
comment.OldMilestone = ToAPIMilestone(c.OldMilestone)
|
|
}
|
|
if c.Milestone != nil {
|
|
comment.Milestone = ToAPIMilestone(c.Milestone)
|
|
}
|
|
|
|
if c.Time != nil {
|
|
err = c.Time.LoadAttributes()
|
|
if err != nil {
|
|
log.Error("Time.LoadAttributes: %v", err)
|
|
return nil
|
|
}
|
|
|
|
comment.TrackedTime = ToTrackedTime(ctx, c.Time)
|
|
}
|
|
|
|
if c.RefIssueID != 0 {
|
|
issue, err := issues_model.GetIssueByID(ctx, c.RefIssueID)
|
|
if err != nil {
|
|
log.Error("GetIssueByID(%d): %v", c.RefIssueID, err)
|
|
return nil
|
|
}
|
|
comment.RefIssue = ToAPIIssue(ctx, issue)
|
|
}
|
|
|
|
if c.RefCommentID != 0 {
|
|
com, err := issues_model.GetCommentByID(ctx, c.RefCommentID)
|
|
if err != nil {
|
|
log.Error("GetCommentByID(%d): %v", c.RefCommentID, err)
|
|
return nil
|
|
}
|
|
err = com.LoadPoster(ctx)
|
|
if err != nil {
|
|
log.Error("LoadPoster: %v", err)
|
|
return nil
|
|
}
|
|
comment.RefComment = ToComment(ctx, com)
|
|
}
|
|
|
|
if c.Label != nil {
|
|
var org *user_model.User
|
|
var repo *repo_model.Repository
|
|
if c.Label.BelongsToOrg() {
|
|
var err error
|
|
org, err = user_model.GetUserByID(ctx, c.Label.OrgID)
|
|
if err != nil {
|
|
log.Error("GetUserByID(%d): %v", c.Label.OrgID, err)
|
|
return nil
|
|
}
|
|
}
|
|
if c.Label.BelongsToRepo() {
|
|
var err error
|
|
repo, err = repo_model.GetRepositoryByID(ctx, c.Label.RepoID)
|
|
if err != nil {
|
|
log.Error("GetRepositoryByID(%d): %v", c.Label.RepoID, err)
|
|
return nil
|
|
}
|
|
}
|
|
comment.Label = ToLabel(c.Label, repo, org)
|
|
}
|
|
|
|
if c.Assignee != nil {
|
|
comment.Assignee = ToUser(ctx, c.Assignee, nil)
|
|
}
|
|
if c.AssigneeTeam != nil {
|
|
comment.AssigneeTeam, _ = ToTeam(ctx, c.AssigneeTeam)
|
|
}
|
|
|
|
if c.ResolveDoer != nil {
|
|
comment.ResolveDoer = ToUser(ctx, c.ResolveDoer, nil)
|
|
}
|
|
|
|
if c.DependentIssue != nil {
|
|
comment.DependentIssue = ToAPIIssue(ctx, c.DependentIssue)
|
|
}
|
|
|
|
return comment
|
|
}
|