Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memcache support when value is returned as string always (#2924) #2950

Merged
merged 1 commit into from
Nov 21, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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