Nessuna descrizione
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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package subconfigs
  2. import (
  3. "fmt"
  4. "git.x2erp.com/qdy/go-base/config/core"
  5. )
  6. // RedisConfig Redis配置
  7. type RedisConfig struct {
  8. core.BaseConfig
  9. Host string `yaml:"host" desc:"Redis服务器主机地址"`
  10. Port int `yaml:"port" desc:"Redis服务器端口"`
  11. Password string `yaml:"password" desc:"Redis密码"`
  12. DB int `yaml:"db" desc:"Redis数据库索引(0-15)"`
  13. PoolSize int `yaml:"pool_size" desc:"连接池大小"`
  14. DialTimeout int `yaml:"dial_timeout" desc:"连接超时时间(秒)"`
  15. ReadTimeout int `yaml:"read_timeout" desc:"读取超时时间(秒)"`
  16. WriteTimeout int `yaml:"write_timeout" desc:"写入超时时间(秒)"`
  17. IdleTimeout int `yaml:"idle_timeout" desc:"空闲连接超时时间(秒)"`
  18. MaxConnAge int `yaml:"max_conn_age" desc:"连接最大生命周期(秒)"`
  19. }
  20. func (c *RedisConfig) Description() string {
  21. return "Redis缓存数据库配置"
  22. }
  23. func NewRedisConfig() *RedisConfig {
  24. return &RedisConfig{}
  25. }
  26. func (c *RedisConfig) SetDefaults() {
  27. c.Port = 6379
  28. c.DB = 0
  29. c.PoolSize = 10
  30. c.DialTimeout = 5
  31. c.ReadTimeout = 3
  32. c.WriteTimeout = 3
  33. c.IdleTimeout = 300
  34. }
  35. func (c *RedisConfig) Load(data map[string]interface{}) error {
  36. return c.LoadFromYAML(data, c)
  37. }
  38. func (c *RedisConfig) Validate() error {
  39. if c.Host == "" {
  40. return fmt.Errorf("redis host is required")
  41. }
  42. return nil
  43. }
  44. func (c *RedisConfig) IsConfigured() bool {
  45. return c.Host != ""
  46. }
  47. // 自动注册
  48. func init() {
  49. core.Register("redis", &RedisConfig{})
  50. }