Skip to content

Commit

Permalink
make redis client idle timeout configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
jianzhiyao committed Jun 28, 2020
1 parent 6b6a0e8 commit 5c9cc80
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion cache/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
}
}
Expand Down

0 comments on commit 5c9cc80

Please sign in to comment.