Skip to content

Commit

Permalink
Fix memcache support when value is returned as string always (#2924) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lafriks authored and lunny committed Nov 21, 2017
1 parent 4683e54 commit c31e877
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions modules/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
package cache

import (
"fmt"
"strconv"

"code.gitea.io/gitea/modules/setting"

mc "github.com/go-macaron/cache"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c31e877

Please sign in to comment.