Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

redis_config.go 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package subconfigs
  2. import "fmt"
  3. // RedisConfig Redis配置
  4. type RedisConfig struct {
  5. BaseConfig
  6. Host string `yaml:"host"`
  7. Port int `yaml:"port"`
  8. Password string `yaml:"password"`
  9. DB int `yaml:"db"`
  10. PoolSize int `yaml:"pool_size"`
  11. DialTimeout int `yaml:"dial_timeout"`
  12. ReadTimeout int `yaml:"read_timeout"`
  13. WriteTimeout int `yaml:"write_timeout"`
  14. IdleTimeout int `yaml:"idle_timeout"`
  15. MaxConnAge int `yaml:"max_conn_age"`
  16. }
  17. func NewRedisConfig() *RedisConfig {
  18. return &RedisConfig{}
  19. }
  20. func (c *RedisConfig) SetDefaults() {
  21. c.Port = 6379
  22. c.DB = 0
  23. c.PoolSize = 10
  24. c.DialTimeout = 5
  25. c.ReadTimeout = 3
  26. c.WriteTimeout = 3
  27. c.IdleTimeout = 300
  28. }
  29. func (c *RedisConfig) Load(data map[string]interface{}) error {
  30. return c.LoadFromYAML(data, c)
  31. }
  32. func (c *RedisConfig) Validate() error {
  33. if c.Host == "" {
  34. return fmt.Errorf("redis host is required")
  35. }
  36. return nil
  37. }
  38. func (c *RedisConfig) IsConfigured() bool {
  39. return c.Host != ""
  40. }
  41. // 自动注册
  42. func init() {
  43. Register("redis", &RedisConfig{})
  44. }