diff options
author | Lauris BH <lauris@nix.lv> | 2017-10-26 04:37:33 +0300 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-10-26 09:37:33 +0800 |
commit | eca05b09aa269dda1309ee77ac750e29e71c3fd3 (patch) | |
tree | 8f5a4aa5c7da0de3e6c2a16f8a567b0b1b0c758b /models | |
parent | 3ab580c8d6b8a2c063d848f8e3002347c9e5cebb (diff) | |
download | gitea-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 'models')
-rw-r--r-- | models/repo.go | 11 | ||||
-rw-r--r-- | models/update.go | 13 |
2 files changed, 21 insertions, 3 deletions
diff --git a/models/repo.go b/models/repo.go index 1b1be62f87..eca71568ee 100644 --- a/models/repo.go +++ b/models/repo.go @@ -258,6 +258,17 @@ func (repo *Repository) APIFormat(mode AccessMode) *api.Repository { return repo.innerAPIFormat(mode, false) } +// GetCommitsCountCacheKey returns cache key used for commits count caching. +func (repo *Repository) GetCommitsCountCacheKey(contextName string, isRef bool) string { + var prefix string + if isRef { + prefix = "ref" + } else { + prefix = "commit" + } + return fmt.Sprintf("commits-count-%d-%s-%s", repo.ID, prefix, contextName) +} + func (repo *Repository) innerAPIFormat(mode AccessMode, isParent bool) *api.Repository { var parent *api.Repository diff --git a/models/update.go b/models/update.go index 62d13ce209..82369bf636 100644 --- a/models/update.go +++ b/models/update.go @@ -11,7 +11,7 @@ import ( "strings" "code.gitea.io/git" - + "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/log" ) @@ -205,19 +205,26 @@ func pushUpdate(opts PushUpdateOptions) (repo *Repository, err error) { var commits = &PushCommits{} if strings.HasPrefix(opts.RefFullName, git.TagPrefix) { // If is tag reference + tagName := opts.RefFullName[len(git.TagPrefix):] if isDelRef { - err = pushUpdateDeleteTag(repo, gitRepo, opts.RefFullName[len(git.TagPrefix):]) + err = pushUpdateDeleteTag(repo, gitRepo, tagName) if err != nil { return nil, fmt.Errorf("pushUpdateDeleteTag: %v", err) } } else { - err = pushUpdateAddTag(repo, gitRepo, opts.RefFullName[len(git.TagPrefix):]) + // Clear cache for tag commit count + cache.Remove(repo.GetCommitsCountCacheKey(tagName, true)) + err = pushUpdateAddTag(repo, gitRepo, tagName) if err != nil { return nil, fmt.Errorf("pushUpdateAddTag: %v", err) } } } else if !isDelRef { // If is branch reference + + // Clear cache for branch commit count + cache.Remove(repo.GetCommitsCountCacheKey(opts.RefFullName[len(git.BranchPrefix):], true)) + newCommit, err := gitRepo.GetCommit(opts.NewCommitID) if err != nil { return nil, fmt.Errorf("gitRepo.GetCommit: %v", err) |