Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

Commit

Permalink
Redis连接释放BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
hb-chen committed Dec 29, 2018
1 parent 22a315e commit ed7eb42
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 6 additions & 3 deletions middleware/cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ func NewRedisCache(host string, password string, defaultExpiration time.Duration
}

func (c *RedisStore) Set(key string, value interface{}, expires time.Duration) error {
return c.invoke(c.pool.Get().Do, key, value, expires)
conn := c.pool.Get()
defer conn.Close()
return c.invoke(conn.Do, key, value, expires)
}

func (c *RedisStore) Add(key string, value interface{}, expires time.Duration) error {
conn := c.pool.Get()
defer conn.Close()
if exists(conn, key) {
return ErrNotStored
}
Expand All @@ -63,6 +66,7 @@ func (c *RedisStore) Add(key string, value interface{}, expires time.Duration) e

func (c *RedisStore) Replace(key string, value interface{}, expires time.Duration) error {
conn := c.pool.Get()
defer conn.Close()
if !exists(conn, key) {
return ErrNotStored
}
Expand Down Expand Up @@ -171,8 +175,7 @@ func (c *RedisStore) invoke(f func(string, ...interface{}) (interface{}, error),
if err != nil {
return err
}
conn := c.pool.Get()
defer conn.Close()

if expires > 0 {
_, err := f("SETEX", key, int32(expires/time.Second), b)
return err
Expand Down
4 changes: 3 additions & 1 deletion router/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func ApiHandler(c *Context) error {
cacheStore := cache.Default(c)
if id == 1 {
value = 0
cacheStore.Set("userId", 1, 5*time.Minute)
if err := cacheStore.Set("userId", 1, 5*time.Minute);err!= nil {
log.Errorf("cache error:%v",err)
}
} else {
if err := cacheStore.Get("userId", &value); err != nil {
log.Debugf("cache userId get err:%v", err)
Expand Down

0 comments on commit ed7eb42

Please sign in to comment.