Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache for branch divergence on branch list page #29577

Merged
merged 21 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7c043cd
Add cache for branch divergence on branch list page
lunny Mar 4, 2024
6270922
Fix check
lunny Mar 4, 2024
a7a25e5
Merge branch 'main' into lunny/cache_branch_divergence
lunny Mar 4, 2024
de1c79f
Merge branch 'main' into lunny/cache_branch_divergence
lunny Mar 7, 2024
2413d20
Merge branch 'main' into lunny/cache_branch_divergence
GiteaBot Mar 8, 2024
6f11363
Merge branch 'main' into lunny/cache_branch_divergence
GiteaBot Mar 8, 2024
9060703
Follow wolfogre's suggestion
lunny Mar 8, 2024
434ad7c
Merge branch 'lunny/cache_branch_divergence' of github.com:lunny/gite…
lunny Mar 8, 2024
67d0944
More stable
lunny Mar 8, 2024
098aca2
Merge branch 'main' into lunny/cache_branch_divergence
GiteaBot Mar 8, 2024
5389406
Merge branch 'main' into lunny/cache_branch_divergence
GiteaBot Mar 8, 2024
098a38d
Merge branch 'main' into lunny/cache_branch_divergence
GiteaBot Mar 8, 2024
115888f
Merge branch 'main' into lunny/cache_branch_divergence
GiteaBot Mar 8, 2024
81f9a84
Set user's 24h preference from their current OS locale (#29651)
silverwind Mar 8, 2024
b191cdb
Merge branch 'main' into lunny/cache_branch_divergence
GiteaBot Mar 8, 2024
ddab9a8
Merge branch 'main' into lunny/cache_branch_divergence
lunny Mar 8, 2024
65f5ad0
Merge branch 'main' into lunny/cache_branch_divergence
GiteaBot Mar 8, 2024
734c6f6
Merge branch 'main' into lunny/cache_branch_divergence
GiteaBot Mar 8, 2024
ec086d4
Merge branch 'main' into lunny/cache_branch_divergence
GiteaBot Mar 8, 2024
d8ccb26
Merge branch 'main' into lunny/cache_branch_divergence
GiteaBot Mar 8, 2024
8296409
Merge branch 'main' into lunny/cache_branch_divergence
GiteaBot Mar 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 49 additions & 6 deletions services/repository/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import (
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/cache"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/optional"
"code.gitea.io/gitea/modules/queue"
Expand Down Expand Up @@ -99,7 +101,6 @@ func LoadBranches(ctx context.Context, repo *repo_model.Repository, gitRepo *git
if err != nil {
return nil, nil, 0, fmt.Errorf("loadOneBranch: %v", err)
}

branches = append(branches, branch)
}

Expand All @@ -109,10 +110,44 @@ func LoadBranches(ctx context.Context, repo *repo_model.Repository, gitRepo *git
if err != nil {
return nil, nil, 0, fmt.Errorf("loadOneBranch: %v", err)
}

return defaultBranch, branches, totalNumOfBranches, nil
}

func getDivergenceCacheKey(repoID int64, branchName string) string {
return fmt.Sprintf("%d-%s", repoID, branchName)
}

// getDivergenceFromCache gets the divergence from cache
func getDivergenceFromCache(repoID int64, branchName string) (*git.DivergeObject, bool) {
data := cache.GetCache().Get(getDivergenceCacheKey(repoID, branchName))
res := git.DivergeObject{
Ahead: -1,
Behind: -1,
}
s, ok := data.([]byte)
if !ok || len(s) == 0 {
return &res, false
}

if err := json.Unmarshal(s, &res); err != nil {
log.Error("json.UnMarshal failed: %v", err)
return &res, false
}
return &res, true
}

func putDivergenceFromCache(repoID int64, branchName string, divergence *git.DivergeObject) error {
bs, err := json.Marshal(divergence)
if err != nil {
return err
}
return cache.GetCache().Put(getDivergenceCacheKey(repoID, branchName), bs, 30*24*60*60)
}

func DelDivergenceFromCache(repoID int64, branchName string) error {
return cache.GetCache().Delete(getDivergenceCacheKey(repoID, branchName))
}

func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *git_model.Branch, protectedBranches *git_model.ProtectedBranchRules,
repoIDToRepo map[int64]*repo_model.Repository,
repoIDToGitRepo map[int64]*git.Repository,
Expand All @@ -130,10 +165,18 @@ func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *g

// it's not default branch
if repo.DefaultBranch != dbBranch.Name && !dbBranch.IsDeleted {
var err error
divergence, err = files_service.CountDivergingCommits(ctx, repo, git.BranchPrefix+branchName)
if err != nil {
log.Error("CountDivergingCommits: %v", err)
var cached bool
divergence, cached = getDivergenceFromCache(repo.ID, dbBranch.Name)
if !cached {
var err error
divergence, err = files_service.CountDivergingCommits(ctx, repo, git.BranchPrefix+branchName)
if err != nil {
log.Error("CountDivergingCommits: %v", err)
} else {
if err = putDivergenceFromCache(repo.ID, dbBranch.Name, divergence); err != nil {
log.Error("putDivergenceFromCache: %v", err)
}
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions services/repository/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
}
}

// delete cache for divergence
if err := DelDivergenceFromCache(repo.ID, branch); err != nil {
log.Error("DelDivergenceFromCache: %v", err)
}

commits := repo_module.GitToPushCommits(l)
commits.HeadCommit = repo_module.CommitToPushCommit(newCommit)

Expand Down
Loading