From 5c9cc805a1df91cb7dd2d4e5cca06c06f687387c Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Sun, 28 Jun 2020 22:20:46 +0800 Subject: [PATCH] make redis client idle timeout configurable --- cache/redis/redis.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cache/redis/redis.go b/cache/redis/redis.go index 7a14b012..56faf211 100644 --- a/cache/redis/redis.go +++ b/cache/redis/redis.go @@ -55,6 +55,9 @@ type Cache struct { key string password string maxIdle int + + //the timeout to a value less than the redis server's timeout. + timeout time.Duration } // NewRedisCache create new redis cache with default collection name. @@ -211,12 +214,21 @@ func (rc *Cache) StartAndGC(config string) error { if _, ok := cf["maxIdle"]; !ok { cf["maxIdle"] = "3" } + if _, ok := cf["timeout"]; !ok { + cf["timeout"] = "180s" + } rc.key = cf["key"] rc.conninfo = cf["conn"] rc.dbNum, _ = strconv.Atoi(cf["dbNum"]) rc.password = cf["password"] rc.maxIdle, _ = strconv.Atoi(cf["maxIdle"]) + if v, err := time.ParseDuration(cf["timeout"]); err == nil { + rc.timeout = v + } else { + rc.timeout = 180 * time.Second + } + rc.connectInit() c := rc.p.Get() @@ -250,7 +262,7 @@ func (rc *Cache) connectInit() { // initialize a new pool rc.p = &redis.Pool{ MaxIdle: rc.maxIdle, - IdleTimeout: 180 * time.Second, + IdleTimeout: rc.timeout, Dial: dialFunc, } }