aboutsummaryrefslogtreecommitdiffstats
path: root/modules/context
diff options
context:
space:
mode:
authorLauris BH <lauris@nix.lv>2017-10-26 04:37:33 +0300
committerLunny Xiao <xiaolunwen@gmail.com>2017-10-26 09:37:33 +0800
commiteca05b09aa269dda1309ee77ac750e29e71c3fd3 (patch)
tree8f5a4aa5c7da0de3e6c2a16f8a567b0b1b0c758b /modules/context
parent3ab580c8d6b8a2c063d848f8e3002347c9e5cebb (diff)
downloadgitea-eca05b09aa269dda1309ee77ac750e29e71c3fd3.tar.gz
gitea-eca05b09aa269dda1309ee77ac750e29e71c3fd3.zip
Add commit count caching (#2774)
* Add commit count caching * Small refactoring * Add different key prefix for refs and commits * Add configuratuion option to allow to change caching time or disable it
Diffstat (limited to 'modules/context')
-rw-r--r--modules/context/repo.go21
1 files changed, 19 insertions, 2 deletions
diff --git a/modules/context/repo.go b/modules/context/repo.go
index c33396c0f9..3aaf1ce64a 100644
--- a/modules/context/repo.go
+++ b/modules/context/repo.go
@@ -13,7 +13,9 @@ import (
"code.gitea.io/git"
"code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/setting"
+
"github.com/Unknwon/com"
"gopkg.in/editorconfig/editorconfig-core-go.v1"
"gopkg.in/macaron.v1"
@@ -100,6 +102,21 @@ func (r *Repository) CanUseTimetracker(issue *models.Issue, user *models.User) b
r.IsWriter() || issue.IsPoster(user.ID) || issue.AssigneeID == user.ID)
}
+// GetCommitsCount returns cached commit count for current view
+func (r *Repository) GetCommitsCount() (int64, error) {
+ var contextName string
+ if r.IsViewBranch {
+ contextName = r.BranchName
+ } else if r.IsViewTag {
+ contextName = r.TagName
+ } else {
+ contextName = r.CommitID
+ }
+ return cache.GetInt64(r.Repository.GetCommitsCountCacheKey(contextName, r.IsViewBranch || r.IsViewTag), func() (int64, error) {
+ return r.Commit.CommitsCount()
+ })
+}
+
// GetEditorconfig returns the .editorconfig definition if found in the
// HEAD of the default repo branch.
func (r *Repository) GetEditorconfig() (*editorconfig.Editorconfig, error) {
@@ -535,9 +552,9 @@ func RepoRef() macaron.Handler {
ctx.Data["IsViewCommit"] = ctx.Repo.IsViewCommit
ctx.Data["CanCreateBranch"] = ctx.Repo.CanCreateBranch()
- ctx.Repo.CommitsCount, err = ctx.Repo.Commit.CommitsCount()
+ ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount()
if err != nil {
- ctx.Handle(500, "CommitsCount", err)
+ ctx.Handle(500, "GetCommitsCount", err)
return
}
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount