summaryrefslogtreecommitdiffstats
path: root/modules/git/commit_info.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2020-02-02 03:11:32 +0800
committerGitHub <noreply@github.com>2020-02-01 19:11:32 +0000
commitce7062a422777c00aadf43ad67a90cc8aae689a5 (patch)
tree1648064ddb7f8d9e5c6b889bed9147db295cb658 /modules/git/commit_info.go
parent046bb05979b2476d4eef85f2d156ac42310f1a3f (diff)
downloadgitea-ce7062a422777c00aadf43ad67a90cc8aae689a5.tar.gz
gitea-ce7062a422777c00aadf43ad67a90cc8aae689a5.zip
Cache last commit to accelerate the repository directory page visit (#10069)
* Cache last commit to accelerate the repository directory page visit * Default use default cache configuration * add tests for last commit cache * Simplify last commit cache * Revert Enabled back * Change the last commit cache default ttl to 8760h * Fix test
Diffstat (limited to 'modules/git/commit_info.go')
-rw-r--r--modules/git/commit_info.go45
1 files changed, 44 insertions, 1 deletions
diff --git a/modules/git/commit_info.go b/modules/git/commit_info.go
index e74ddbfb05..69fd7f3563 100644
--- a/modules/git/commit_info.go
+++ b/modules/git/commit_info.go
@@ -5,6 +5,8 @@
package git
import (
+ "path"
+
"github.com/emirpasic/gods/trees/binaryheap"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
@@ -30,7 +32,29 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache LastCom
return nil, nil, err
}
- revs, err := getLastCommitForPaths(c, treePath, entryPaths)
+ var revs map[string]*object.Commit
+ if cache != nil {
+ var unHitPaths []string
+ revs, unHitPaths, err = getLastCommitForPathsByCache(commit.ID.String(), treePath, entryPaths, cache)
+ if err != nil {
+ return nil, nil, err
+ }
+ if len(unHitPaths) > 0 {
+ revs2, err := getLastCommitForPaths(c, treePath, unHitPaths)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ for k, v := range revs2 {
+ if err := cache.Put(commit.ID.String(), path.Join(treePath, k), v.ID().String()); err != nil {
+ return nil, nil, err
+ }
+ revs[k] = v
+ }
+ }
+ } else {
+ revs, err = getLastCommitForPaths(c, treePath, entryPaths)
+ }
if err != nil {
return nil, nil, err
}
@@ -127,6 +151,25 @@ func getFileHashes(c cgobject.CommitNode, treePath string, paths []string) (map[
return hashes, nil
}
+func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cache LastCommitCache) (map[string]*object.Commit, []string, error) {
+ var unHitEntryPaths []string
+ var results = make(map[string]*object.Commit)
+ for _, p := range paths {
+ lastCommit, err := cache.Get(commitID, path.Join(treePath, p))
+ if err != nil {
+ return nil, nil, err
+ }
+ if lastCommit != nil {
+ results[p] = lastCommit
+ continue
+ }
+
+ unHitEntryPaths = append(unHitEntryPaths, p)
+ }
+
+ return results, unHitEntryPaths, nil
+}
+
func getLastCommitForPaths(c cgobject.CommitNode, treePath string, paths []string) (map[string]*object.Commit, error) {
// We do a tree traversal with nodes sorted by commit time
heap := binaryheap.NewWith(func(a, b interface{}) int {