Skip to content

Commit 2aa90de

Browse files
committed
Small refactoring
1 parent d3bb548 commit 2aa90de

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

models/repo.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ func (repo *Repository) APIFormat(mode AccessMode) *api.Repository {
258258
return repo.innerAPIFormat(mode, false)
259259
}
260260

261+
// GetCommitsCountCacheKey returns cache key used for commits count caching.
262+
func (repo *Repository) GetCommitsCountCacheKey(contextName string) string {
263+
return fmt.Sprintf("commits-count-%d-%s", repo.ID, contextName)
264+
}
265+
261266
func (repo *Repository) innerAPIFormat(mode AccessMode, isParent bool) *api.Repository {
262267
var parent *api.Repository
263268

models/update.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func pushUpdate(opts PushUpdateOptions) (repo *Repository, err error) {
213213
}
214214
} else {
215215
// Clear cache for tag commit count
216-
cache.Remove(fmt.Sprintf("tag-commits-count-%d-%s", repo.ID, tagName))
216+
cache.Remove(repo.GetCommitsCountCacheKey(tagName))
217217
err = pushUpdateAddTag(repo, gitRepo, tagName)
218218
if err != nil {
219219
return nil, fmt.Errorf("pushUpdateAddTag: %v", err)
@@ -222,8 +222,8 @@ func pushUpdate(opts PushUpdateOptions) (repo *Repository, err error) {
222222
} else if !isDelRef {
223223
// If is branch reference
224224

225-
// Clear cache for branch commit counr
226-
cache.Remove(fmt.Sprintf("branch-commits-count-%d-%s", repo.ID, opts.RefFullName[len(git.BranchPrefix):]))
225+
// Clear cache for branch commit count
226+
cache.Remove(repo.GetCommitsCountCacheKey(opts.RefFullName[len(git.BranchPrefix):]))
227227

228228
newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
229229
if err != nil {

modules/context/repo.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,15 @@ func (r *Repository) CanUseTimetracker(issue *models.Issue, user *models.User) b
104104

105105
// GetCommitsCount returns cached commit count for current view
106106
func (r *Repository) GetCommitsCount() (int64, error) {
107+
var contextName string
107108
if r.IsViewBranch {
108-
return cache.GetInt64(fmt.Sprintf("branch-commits-count-%d-%s", r.Repository.ID, r.BranchName), func() (int64, error) {
109-
return r.Commit.CommitsCount()
110-
})
111-
}
112-
if r.IsViewTag {
113-
return cache.GetInt64(fmt.Sprintf("tag-commits-count-%d-%s", r.Repository.ID, r.TagName), func() (int64, error) {
114-
return r.Commit.CommitsCount()
115-
})
109+
contextName = r.BranchName
110+
} else if r.IsViewTag {
111+
contextName = r.TagName
112+
} else {
113+
contextName = r.CommitID
116114
}
117-
return cache.GetInt64(fmt.Sprintf("commit-commits-count-%d-%s", r.Repository.ID, r.CommitID), func() (int64, error) {
115+
return cache.GetInt64(r.Repository.GetCommitsCountCacheKey(contextName), func() (int64, error) {
118116
return r.Commit.CommitsCount()
119117
})
120118
}

0 commit comments

Comments
 (0)