aboutsummaryrefslogtreecommitdiffstats
path: root/modules/cache
diff options
context:
space:
mode:
authorLauris BH <lauris@nix.lv>2017-11-16 09:06:34 +0200
committerLunny Xiao <xiaolunwen@gmail.com>2017-11-16 15:06:34 +0800
commit7c3e605698aded0c1a877ad9e8e43bd6b70c6f18 (patch)
tree57763f36694bdc43afe0b6b361810d38f0d6ea73 /modules/cache
parent222e7c3f06dc5dd7780626d7146644b6730e613b (diff)
downloadgitea-7c3e605698aded0c1a877ad9e8e43bd6b70c6f18.tar.gz
gitea-7c3e605698aded0c1a877ad9e8e43bd6b70c6f18.zip
Fix memcache support when value is returned as string always (#2924)
Diffstat (limited to 'modules/cache')
-rw-r--r--modules/cache/cache.go29
1 files changed, 27 insertions, 2 deletions
diff --git a/modules/cache/cache.go b/modules/cache/cache.go
index 0a73ae8ae9..a2d6c724c8 100644
--- a/modules/cache/cache.go
+++ b/modules/cache/cache.go
@@ -5,6 +5,9 @@
package cache
import (
+ "fmt"
+ "strconv"
+
"code.gitea.io/gitea/modules/setting"
mc "github.com/go-macaron/cache"
@@ -42,7 +45,18 @@ func GetInt(key string, getFunc func() (int, error)) (int, error) {
}
conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
}
- return conn.Get(key).(int), nil
+ switch value := conn.Get(key).(type) {
+ case int:
+ return value, nil
+ case string:
+ v, err := strconv.Atoi(value)
+ if err != nil {
+ return 0, err
+ }
+ return v, nil
+ default:
+ return 0, fmt.Errorf("Unsupported cached value type: %v", value)
+ }
}
// GetInt64 returns key value from cache with callback when no key exists in cache
@@ -60,7 +74,18 @@ func GetInt64(key string, getFunc func() (int64, error)) (int64, error) {
}
conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
}
- return conn.Get(key).(int64), nil
+ switch value := conn.Get(key).(type) {
+ case int64:
+ return value, nil
+ case string:
+ v, err := strconv.ParseInt(value, 10, 64)
+ if err != nil {
+ return 0, err
+ }
+ return v, nil
+ default:
+ return 0, fmt.Errorf("Unsupported cached value type: %v", value)
+ }
}
// Remove key from cache