56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type Redis struct {
|
|
Host string `mapstructure:"host"`
|
|
Port int `mapstructure:"port"`
|
|
Password string `mapstructure:"password"`
|
|
DB int `mapstructure:"db"`
|
|
DialTimeout string `mapstructure:"dial_timeout"`
|
|
ReadTimeout string `mapstructure:"read_timeout"`
|
|
WriteTimeout string `mapstructure:"write_timeout"`
|
|
PoolSize int `mapstructure:"pool_size"`
|
|
MinIdleConnections int `mapstructure:"min_idle_connections"`
|
|
}
|
|
|
|
func (r Redis) Addr() string {
|
|
return fmt.Sprintf("%s:%d", r.Host, r.Port)
|
|
}
|
|
|
|
func (r Redis) ParseDialTimeout() time.Duration {
|
|
if r.DialTimeout == "" {
|
|
return 5 * time.Second
|
|
}
|
|
d, err := time.ParseDuration(r.DialTimeout)
|
|
if err != nil {
|
|
return 5 * time.Second
|
|
}
|
|
return d
|
|
}
|
|
|
|
func (r Redis) ParseReadTimeout() time.Duration {
|
|
if r.ReadTimeout == "" {
|
|
return 3 * time.Second
|
|
}
|
|
d, err := time.ParseDuration(r.ReadTimeout)
|
|
if err != nil {
|
|
return 3 * time.Second
|
|
}
|
|
return d
|
|
}
|
|
|
|
func (r Redis) ParseWriteTimeout() time.Duration {
|
|
if r.WriteTimeout == "" {
|
|
return 3 * time.Second
|
|
}
|
|
d, err := time.ParseDuration(r.WriteTimeout)
|
|
if err != nil {
|
|
return 3 * time.Second
|
|
}
|
|
return d
|
|
}
|