Skip to content

Commit

Permalink
Merge pull request #49 from AlvinQinwen/master
Browse files Browse the repository at this point in the history
初始化redis客户端时支持IdleCheckFrequency字段可自定义,与官方设定一分钟保持一致
  • Loading branch information
Matrix-X authored Sep 13, 2022
2 parents 82ea1de + 03e0d4f commit 24ba851
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,34 @@ const SYSTEM_CACHE_TIMEOUT_SEASON = 60 * 60 * 24 * 30 * 3
const SYSTEM_CACHE_TIMEOUT_YEAR = 60 * 60 * 24 * 30 * 3 * 12

const (
defaultMaxIdle = 5
defaultMaxActive = 0
defaultTimeoutIdle = 240
defaultTimeoutConnect = 10000
defaultTimeoutRead = 5000
defaultTimeoutWrite = 5000
defaultAddr = "localhost:6379"
defaultProtocol = "tcp"
defaultRetryThreshold = 5
defaultMaxIdle = 5
defaultMaxActive = 0
defaultTimeoutIdle = 240
defaultTimeoutConnect = 10000
defaultTimeoutRead = 5000
defaultTimeoutWrite = 5000
defaultAddr = "localhost:6379"
defaultProtocol = "tcp"
defaultRetryThreshold = 5
defaultIdleCheckFrequency = time.Minute
)

const SCRIPT_SETEX = `return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])`

type RedisOptions struct {
MaxIdle int
MaxActive int
Protocol string
Addr string
Password string
DB int
SSLEnabled bool
Expiration time.Duration
TimeoutConnect int
TimeoutRead int
TimeoutWrite int
TimeoutIdle int
MaxIdle int
MaxActive int
Protocol string
Addr string
Password string
DB int
SSLEnabled bool
Expiration time.Duration
TimeoutConnect int
TimeoutRead int
TimeoutWrite int
TimeoutIdle int
IdleCheckFrequency time.Duration
}

var CTXRedis = context.Background()
Expand All @@ -77,7 +79,7 @@ func NewGRedis(opts interface{}) (gr *GRedis) {
PoolTimeout: 30 * time.Second,
IdleTimeout: toI,
Password: options.Password,
IdleCheckFrequency: 500 * time.Millisecond,
IdleCheckFrequency: options.IdleCheckFrequency,
}

if options.SSLEnabled {
Expand Down Expand Up @@ -132,6 +134,10 @@ func (r *RedisOptions) initDefaults() *RedisOptions {
r.Protocol = defaultProtocol
}

if r.IdleCheckFrequency == 0 {
r.IdleCheckFrequency = time.Minute
}

return r
}

Expand Down

0 comments on commit 24ba851

Please sign in to comment.