summaryrefslogtreecommitdiffstats
path: root/modules
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
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')
-rw-r--r--modules/cache/cache.go72
-rw-r--r--modules/context/repo.go21
-rw-r--r--modules/setting/setting.go32
3 files changed, 113 insertions, 12 deletions
diff --git a/modules/cache/cache.go b/modules/cache/cache.go
new file mode 100644
index 0000000000..0a73ae8ae9
--- /dev/null
+++ b/modules/cache/cache.go
@@ -0,0 +1,72 @@
+// Copyright 2017 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package cache
+
+import (
+ "code.gitea.io/gitea/modules/setting"
+
+ mc "github.com/go-macaron/cache"
+)
+
+var conn mc.Cache
+
+// NewContext start cache service
+func NewContext() error {
+ if setting.CacheService == nil || conn != nil {
+ return nil
+ }
+
+ var err error
+ conn, err = mc.NewCacher(setting.CacheService.Adapter, mc.Options{
+ Adapter: setting.CacheService.Adapter,
+ AdapterConfig: setting.CacheService.Conn,
+ Interval: setting.CacheService.Interval,
+ })
+ return err
+}
+
+// GetInt returns key value from cache with callback when no key exists in cache
+func GetInt(key string, getFunc func() (int, error)) (int, error) {
+ if conn == nil || setting.CacheService.TTL == 0 {
+ return getFunc()
+ }
+ if !conn.IsExist(key) {
+ var (
+ value int
+ err error
+ )
+ if value, err = getFunc(); err != nil {
+ return value, err
+ }
+ conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
+ }
+ return conn.Get(key).(int), nil
+}
+
+// GetInt64 returns key value from cache with callback when no key exists in cache
+func GetInt64(key string, getFunc func() (int64, error)) (int64, error) {
+ if conn == nil || setting.CacheService.TTL == 0 {
+ return getFunc()
+ }
+ if !conn.IsExist(key) {
+ var (
+ value int64
+ err error
+ )
+ if value, err = getFunc(); err != nil {
+ return value, err
+ }
+ conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
+ }
+ return conn.Get(key).(int64), nil
+}
+
+// Remove key from cache
+func Remove(key string) {
+ if conn == nil {
+ return
+ }
+ conn.Delete(key)
+}
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
diff --git a/modules/setting/setting.go b/modules/setting/setting.go
index 9787e09280..6c89381f3b 100644
--- a/modules/setting/setting.go
+++ b/modules/setting/setting.go
@@ -325,11 +325,6 @@ var (
// Time settings
TimeFormat string
- // Cache settings
- CacheAdapter string
- CacheInterval int
- CacheConn string
-
// Session settings
SessionConfig session.Options
CSRFCookieName = "_csrf"
@@ -1295,16 +1290,33 @@ func NewXORMLogService(disableConsole bool) {
}
}
+// Cache represents cache settings
+type Cache struct {
+ Adapter string
+ Interval int
+ Conn string
+ TTL time.Duration
+}
+
+var (
+ // CacheService the global cache
+ CacheService *Cache
+)
+
func newCacheService() {
- CacheAdapter = Cfg.Section("cache").Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache"})
- switch CacheAdapter {
+ sec := Cfg.Section("cache")
+ CacheService = &Cache{
+ Adapter: sec.Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache"}),
+ }
+ switch CacheService.Adapter {
case "memory":
- CacheInterval = Cfg.Section("cache").Key("INTERVAL").MustInt(60)
+ CacheService.Interval = sec.Key("INTERVAL").MustInt(60)
case "redis", "memcache":
- CacheConn = strings.Trim(Cfg.Section("cache").Key("HOST").String(), "\" ")
+ CacheService.Conn = strings.Trim(sec.Key("HOST").String(), "\" ")
default:
- log.Fatal(4, "Unknown cache adapter: %s", CacheAdapter)
+ log.Fatal(4, "Unknown cache adapter: %s", CacheService.Adapter)
}
+ CacheService.TTL = sec.Key("ITEM_TTL").MustDuration(16 * time.Hour)
log.Info("Cache Service Enabled")
}