]> source.dussan.org Git - gitea.git/commitdiff
Fix memcache support when value is returned as string always (#2924) (#2950)
authorLauris BH <lauris@nix.lv>
Tue, 21 Nov 2017 03:46:44 +0000 (05:46 +0200)
committerLunny Xiao <xiaolunwen@gmail.com>
Tue, 21 Nov 2017 03:46:44 +0000 (11:46 +0800)
modules/cache/cache.go

index 0a73ae8ae98c6ae778d16eadc3ef40cd91598623..a2d6c724c851a9492bff2930ccad451b6b00b160 100644 (file)
@@ -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