diff options
Diffstat (limited to 'modules/cache/cache_redis.go')
-rw-r--r-- | modules/cache/cache_redis.go | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/modules/cache/cache_redis.go b/modules/cache/cache_redis.go index e4b9a70f63..ff6c8d424c 100644 --- a/modules/cache/cache_redis.go +++ b/modules/cache/cache_redis.go @@ -6,11 +6,11 @@ package cache import ( "fmt" + "strconv" "time" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/nosql" - "code.gitea.io/gitea/modules/util" "gitea.com/go-chi/cache" "github.com/go-redis/redis/v8" @@ -24,20 +24,37 @@ type RedisCacher struct { occupyMode bool } -// Put puts value into cache with key and expire time. +// toStr convert string/int/int64 interface to string. it's only used by the RedisCacher.Put internally +func toStr(v interface{}) string { + if v == nil { + return "" + } + switch v := v.(type) { + case string: + return v + case []byte: + return string(v) + case int: + return strconv.FormatInt(int64(v), 10) + case int64: + return strconv.FormatInt(v, 10) + default: + return fmt.Sprint(v) // as what the old com.ToStr does in most cases + } +} + +// Put puts value (string type) into cache with key and expire time. // If expired is 0, it lives forever. func (c *RedisCacher) Put(key string, val interface{}, expire int64) error { + // this function is not well-designed, it only puts string values into cache key = c.prefix + key if expire == 0 { - if err := c.c.Set(graceful.GetManager().HammerContext(), key, util.ToStr(val), 0).Err(); err != nil { + if err := c.c.Set(graceful.GetManager().HammerContext(), key, toStr(val), 0).Err(); err != nil { return err } } else { - dur, err := time.ParseDuration(util.ToStr(expire) + "s") - if err != nil { - return err - } - if err = c.c.Set(graceful.GetManager().HammerContext(), key, util.ToStr(val), dur).Err(); err != nil { + dur := time.Duration(expire) * time.Second + if err := c.c.Set(graceful.GetManager().HammerContext(), key, toStr(val), dur).Err(); err != nil { return err } } |