summaryrefslogtreecommitdiffstats
path: root/modules/cache
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/cache
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/cache')
-rw-r--r--modules/cache/cache.go72
1 files changed, 72 insertions, 0 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)
+}