Skip to content

Commit 7c3e605

Browse files
lafrikslunny
authored andcommitted
Fix memcache support when value is returned as string always (#2924)
1 parent 222e7c3 commit 7c3e605

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

modules/cache/cache.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
package cache
66

77
import (
8+
"fmt"
9+
"strconv"
10+
811
"code.gitea.io/gitea/modules/setting"
912

1013
mc "github.com/go-macaron/cache"
@@ -42,7 +45,18 @@ func GetInt(key string, getFunc func() (int, error)) (int, error) {
4245
}
4346
conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
4447
}
45-
return conn.Get(key).(int), nil
48+
switch value := conn.Get(key).(type) {
49+
case int:
50+
return value, nil
51+
case string:
52+
v, err := strconv.Atoi(value)
53+
if err != nil {
54+
return 0, err
55+
}
56+
return v, nil
57+
default:
58+
return 0, fmt.Errorf("Unsupported cached value type: %v", value)
59+
}
4660
}
4761

4862
// 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) {
6074
}
6175
conn.Put(key, value, int64(setting.CacheService.TTL.Seconds()))
6276
}
63-
return conn.Get(key).(int64), nil
77+
switch value := conn.Get(key).(type) {
78+
case int64:
79+
return value, nil
80+
case string:
81+
v, err := strconv.ParseInt(value, 10, 64)
82+
if err != nil {
83+
return 0, err
84+
}
85+
return v, nil
86+
default:
87+
return 0, fmt.Errorf("Unsupported cached value type: %v", value)
88+
}
6489
}
6590

6691
// Remove key from cache

0 commit comments

Comments
 (0)