增加自定义配置转换成redis包需要的配置格式的方法
This commit is contained in:
parent
417d4ee834
commit
9d2f6b8a67
47
config.go
47
config.go
@ -8,8 +8,11 @@
|
|||||||
package redis
|
package redis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/logger"
|
"git.zhangdeman.cn/zhangdeman/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -92,7 +95,7 @@ type Config struct {
|
|||||||
// Amount of time client waits for connection if all connections
|
// Amount of time client waits for connection if all connections
|
||||||
// are busy before returning an error.
|
// are busy before returning an error.
|
||||||
// Default is ReadTimeout + 1 second.
|
// Default is ReadTimeout + 1 second.
|
||||||
PoolTimeout time.Duration `json:"pool_timeout" yaml:"pool_timeout"` // 连接池无可用连接, 等待的时常, 默认值 ReadTimeout + 1
|
PoolTimeout int64 `json:"pool_timeout" yaml:"pool_timeout"` // 连接池无可用连接, 等待的时常, 默认值 ReadTimeout + 1
|
||||||
// Amount of time after which client closes idle connections.
|
// Amount of time after which client closes idle connections.
|
||||||
// Should be less than server's timeout.
|
// Should be less than server's timeout.
|
||||||
// Default is 5 minutes. -1 disables idle timeout check.
|
// Default is 5 minutes. -1 disables idle timeout check.
|
||||||
@ -104,13 +107,43 @@ type Config struct {
|
|||||||
IdleCheckFrequency int64 `json:"idle_check_frequency" yaml:"idle_check_frequency"` // 空闲连接检查频率, 默认 1 分钟, -1 禁用, 注意 : 即使禁用,超过 IdleTimeout 时常的连接也不可被读取到
|
IdleCheckFrequency int64 `json:"idle_check_frequency" yaml:"idle_check_frequency"` // 空闲连接检查频率, 默认 1 分钟, -1 禁用, 注意 : 即使禁用,超过 IdleTimeout 时常的连接也不可被读取到
|
||||||
}
|
}
|
||||||
|
|
||||||
// Timeout 超时配置
|
// Config2Options 将 自定义的配置文件转为 redis 包需要的配置格式
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 12:19 2022/6/15
|
// Date : 17:37 2022/6/15
|
||||||
type Timeout struct {
|
func Config2Options(cfg *Config) *redis.Options {
|
||||||
Connect int `yaml:"connect" json:"connect"` // 连接超时
|
option := &redis.Options{
|
||||||
Read int `yaml:"read" json:"read"` // 读取超时
|
Network: cfg.Network,
|
||||||
Write int `yaml:"write" json:"write"` // 写入超时
|
Addr: fmt.Sprintf("%v:%v", cfg.Host, cfg.Port),
|
||||||
|
Username: cfg.Username,
|
||||||
|
Password: cfg.Password,
|
||||||
|
DB: cfg.DB,
|
||||||
|
MaxRetries: cfg.MaxRetries,
|
||||||
|
MinRetryBackoff: time.Duration(cfg.MinRetryBackoff) * time.Millisecond,
|
||||||
|
MaxRetryBackoff: time.Duration(cfg.MaxRetryBackoff) * time.Millisecond,
|
||||||
|
DialTimeout: time.Duration(cfg.DialTimeout) * time.Millisecond,
|
||||||
|
ReadTimeout: time.Duration(cfg.ReadTimeout) * time.Millisecond,
|
||||||
|
WriteTimeout: time.Duration(cfg.WriteTimeout) * time.Millisecond,
|
||||||
|
PoolFIFO: cfg.PoolFIFO,
|
||||||
|
PoolSize: cfg.PoolSize,
|
||||||
|
MinIdleConns: cfg.MinIdleConns,
|
||||||
|
MaxConnAge: time.Duration(cfg.MaxConnAge) * time.Millisecond,
|
||||||
|
PoolTimeout: time.Duration(cfg.PoolTimeout) * time.Millisecond,
|
||||||
|
IdleTimeout: time.Duration(cfg.IdleTimeout) * time.Millisecond,
|
||||||
|
IdleCheckFrequency: time.Duration(cfg.IdleCheckFrequency) * time.Millisecond,
|
||||||
|
TLSConfig: nil,
|
||||||
|
Limiter: nil,
|
||||||
|
}
|
||||||
|
if len(option.Network) == 0 {
|
||||||
|
option.Network = NetworkTypeTCP
|
||||||
|
}
|
||||||
|
return option
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
// NetworkTypeTCP ...
|
||||||
|
NetworkTypeTCP = "tcp"
|
||||||
|
// NetworkTypeUnix ...
|
||||||
|
NetworkTypeUnix = "unix"
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user