Skip to content

Commit d3bb548

Browse files
committed
Add commit count caching
1 parent 3af5b67 commit d3bb548

File tree

8 files changed

+138
-25
lines changed

8 files changed

+138
-25
lines changed

models/update.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"strings"
1212

1313
"code.gitea.io/git"
14-
14+
"code.gitea.io/gitea/modules/cache"
1515
"code.gitea.io/gitea/modules/log"
1616
)
1717

@@ -205,19 +205,26 @@ func pushUpdate(opts PushUpdateOptions) (repo *Repository, err error) {
205205
var commits = &PushCommits{}
206206
if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
207207
// If is tag reference
208+
tagName := opts.RefFullName[len(git.TagPrefix):]
208209
if isDelRef {
209-
err = pushUpdateDeleteTag(repo, gitRepo, opts.RefFullName[len(git.TagPrefix):])
210+
err = pushUpdateDeleteTag(repo, gitRepo, tagName)
210211
if err != nil {
211212
return nil, fmt.Errorf("pushUpdateDeleteTag: %v", err)
212213
}
213214
} else {
214-
err = pushUpdateAddTag(repo, gitRepo, opts.RefFullName[len(git.TagPrefix):])
215+
// Clear cache for tag commit count
216+
cache.Remove(fmt.Sprintf("tag-commits-count-%d-%s", repo.ID, tagName))
217+
err = pushUpdateAddTag(repo, gitRepo, tagName)
215218
if err != nil {
216219
return nil, fmt.Errorf("pushUpdateAddTag: %v", err)
217220
}
218221
}
219222
} else if !isDelRef {
220223
// If is branch reference
224+
225+
// Clear cache for branch commit counr
226+
cache.Remove(fmt.Sprintf("branch-commits-count-%d-%s", repo.ID, opts.RefFullName[len(git.BranchPrefix):]))
227+
221228
newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
222229
if err != nil {
223230
return nil, fmt.Errorf("gitRepo.GetCommit: %v", err)

modules/cache/cache.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package cache
6+
7+
import (
8+
"time"
9+
10+
"code.gitea.io/gitea/modules/setting"
11+
12+
mc "github.com/go-macaron/cache"
13+
)
14+
15+
var conn mc.Cache
16+
17+
// NewContext start cache service
18+
func NewContext() error {
19+
if setting.CacheService == nil || conn != nil {
20+
return nil
21+
}
22+
23+
var err error
24+
conn, err = mc.NewCacher(setting.CacheService.Adapter, mc.Options{
25+
Adapter: setting.CacheService.Adapter,
26+
AdapterConfig: setting.CacheService.Conn,
27+
Interval: setting.CacheService.Interval,
28+
})
29+
return err
30+
}
31+
32+
// GetInt returns key value from cache with callback when no key exists in cache
33+
func GetInt(key string, getFunc func() (int, error)) (int, error) {
34+
if conn == nil {
35+
return getFunc()
36+
}
37+
if !conn.IsExist(key) {
38+
var (
39+
value int
40+
err error
41+
)
42+
if value, err = getFunc(); err != nil {
43+
return value, err
44+
}
45+
conn.Put(key, value, int64(time.Hour.Seconds()))
46+
}
47+
return conn.Get(key).(int), nil
48+
}
49+
50+
// GetInt64 returns key value from cache with callback when no key exists in cache
51+
func GetInt64(key string, getFunc func() (int64, error)) (int64, error) {
52+
if conn == nil {
53+
return getFunc()
54+
}
55+
if !conn.IsExist(key) {
56+
var (
57+
value int64
58+
err error
59+
)
60+
if value, err = getFunc(); err != nil {
61+
return value, err
62+
}
63+
conn.Put(key, value, int64(time.Hour.Seconds()))
64+
}
65+
return conn.Get(key).(int64), nil
66+
}
67+
68+
// Remove key from cache
69+
func Remove(key string) {
70+
if conn == nil {
71+
return
72+
}
73+
conn.Delete(key)
74+
}

modules/context/repo.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313

1414
"code.gitea.io/git"
1515
"code.gitea.io/gitea/models"
16+
"code.gitea.io/gitea/modules/cache"
1617
"code.gitea.io/gitea/modules/setting"
18+
1719
"github.com/Unknwon/com"
1820
"gopkg.in/editorconfig/editorconfig-core-go.v1"
1921
"gopkg.in/macaron.v1"
@@ -100,6 +102,23 @@ func (r *Repository) CanUseTimetracker(issue *models.Issue, user *models.User) b
100102
r.IsWriter() || issue.IsPoster(user.ID) || issue.AssigneeID == user.ID)
101103
}
102104

105+
// GetCommitsCount returns cached commit count for current view
106+
func (r *Repository) GetCommitsCount() (int64, error) {
107+
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+
})
116+
}
117+
return cache.GetInt64(fmt.Sprintf("commit-commits-count-%d-%s", r.Repository.ID, r.CommitID), func() (int64, error) {
118+
return r.Commit.CommitsCount()
119+
})
120+
}
121+
103122
// GetEditorconfig returns the .editorconfig definition if found in the
104123
// HEAD of the default repo branch.
105124
func (r *Repository) GetEditorconfig() (*editorconfig.Editorconfig, error) {
@@ -535,9 +554,9 @@ func RepoRef() macaron.Handler {
535554
ctx.Data["IsViewCommit"] = ctx.Repo.IsViewCommit
536555
ctx.Data["CanCreateBranch"] = ctx.Repo.CanCreateBranch()
537556

538-
ctx.Repo.CommitsCount, err = ctx.Repo.Commit.CommitsCount()
557+
ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount()
539558
if err != nil {
540-
ctx.Handle(500, "CommitsCount", err)
559+
ctx.Handle(500, "GetCommitsCount", err)
541560
return
542561
}
543562
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount

modules/setting/setting.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,6 @@ var (
324324
// Time settings
325325
TimeFormat string
326326

327-
// Cache settings
328-
CacheAdapter string
329-
CacheInterval int
330-
CacheConn string
331-
332327
// Session settings
333328
SessionConfig session.Options
334329
CSRFCookieName = "_csrf"
@@ -1277,15 +1272,30 @@ func NewXORMLogService(disableConsole bool) {
12771272
}
12781273
}
12791274

1275+
// Cache represents cache settings
1276+
type Cache struct {
1277+
Adapter string
1278+
Interval int
1279+
Conn string
1280+
}
1281+
1282+
var (
1283+
// CacheService the global cache
1284+
CacheService *Cache
1285+
)
1286+
12801287
func newCacheService() {
1281-
CacheAdapter = Cfg.Section("cache").Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache"})
1282-
switch CacheAdapter {
1288+
sec := Cfg.Section("mailer")
1289+
CacheService = &Cache{
1290+
Adapter: sec.Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache"}),
1291+
}
1292+
switch CacheService.Adapter {
12831293
case "memory":
1284-
CacheInterval = Cfg.Section("cache").Key("INTERVAL").MustInt(60)
1294+
CacheService.Interval = sec.Key("INTERVAL").MustInt(60)
12851295
case "redis", "memcache":
1286-
CacheConn = strings.Trim(Cfg.Section("cache").Key("HOST").String(), "\" ")
1296+
CacheService.Conn = strings.Trim(sec.Key("HOST").String(), "\" ")
12871297
default:
1288-
log.Fatal(4, "Unknown cache adapter: %s", CacheAdapter)
1298+
log.Fatal(4, "Unknown cache adapter: %s", CacheService.Adapter)
12891299
}
12901300

12911301
log.Info("Cache Service Enabled")

routers/admin/admin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ func Config(ctx *context.Context) {
224224
ctx.Data["Mailer"] = setting.MailService
225225
}
226226

227-
ctx.Data["CacheAdapter"] = setting.CacheAdapter
228-
ctx.Data["CacheInterval"] = setting.CacheInterval
229-
ctx.Data["CacheConn"] = setting.CacheConn
227+
ctx.Data["CacheAdapter"] = setting.CacheService.Adapter
228+
ctx.Data["CacheInterval"] = setting.CacheService.Interval
229+
ctx.Data["CacheConn"] = setting.CacheService.Conn
230230

231231
ctx.Data["SessionConfig"] = setting.SessionConfig
232232

routers/init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import (
1111
"code.gitea.io/git"
1212
"code.gitea.io/gitea/models"
1313
"code.gitea.io/gitea/models/migrations"
14+
"code.gitea.io/gitea/modules/cache"
1415
"code.gitea.io/gitea/modules/cron"
1516
"code.gitea.io/gitea/modules/highlight"
1617
"code.gitea.io/gitea/modules/log"
1718
"code.gitea.io/gitea/modules/mailer"
1819
"code.gitea.io/gitea/modules/markup"
1920
"code.gitea.io/gitea/modules/setting"
2021
"code.gitea.io/gitea/modules/ssh"
22+
2123
macaron "gopkg.in/macaron.v1"
2224
)
2325

@@ -37,6 +39,7 @@ func checkRunMode() {
3739
func NewServices() {
3840
setting.NewServices()
3941
mailer.NewContext()
42+
cache.NewContext()
4043
}
4144

4245
// GlobalInit is for global configuration reload-able.

routers/repo/commit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func Commits(ctx *context.Context) {
5454
return
5555
}
5656

57-
commitsCount, err := ctx.Repo.Commit.CommitsCount()
57+
commitsCount, err := ctx.Repo.GetCommitsCount()
5858
if err != nil {
5959
ctx.Handle(500, "GetCommitsCount", err)
6060
return
@@ -89,7 +89,7 @@ func Commits(ctx *context.Context) {
8989
func Graph(ctx *context.Context) {
9090
ctx.Data["PageIsCommits"] = true
9191

92-
commitsCount, err := ctx.Repo.Commit.CommitsCount()
92+
commitsCount, err := ctx.Repo.GetCommitsCount()
9393
if err != nil {
9494
ctx.Handle(500, "GetCommitsCount", err)
9595
return

routers/routes/routes.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ func NewMacaron() *macaron.Macaron {
9999
Redirect: true,
100100
}))
101101
m.Use(cache.Cacher(cache.Options{
102-
Adapter: setting.CacheAdapter,
103-
AdapterConfig: setting.CacheConn,
104-
Interval: setting.CacheInterval,
102+
Adapter: setting.CacheService.Adapter,
103+
AdapterConfig: setting.CacheService.Conn,
104+
Interval: setting.CacheService.Interval,
105105
}))
106106
m.Use(captcha.Captchaer(captcha.Options{
107107
SubURL: setting.AppSubURL,
@@ -573,9 +573,9 @@ func RegisterRoutes(m *macaron.Macaron) {
573573
ctx.Handle(500, "GetBranchCommit", err)
574574
return
575575
}
576-
ctx.Repo.CommitsCount, err = ctx.Repo.Commit.CommitsCount()
576+
ctx.Repo.CommitsCount, err = ctx.Repo.GetCommitsCount()
577577
if err != nil {
578-
ctx.Handle(500, "CommitsCount", err)
578+
ctx.Handle(500, "GetCommitsCount", err)
579579
return
580580
}
581581
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount

0 commit comments

Comments
 (0)