redis.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package options
  2. import (
  3. "github.com/spf13/pflag"
  4. )
  5. // RedisOptions redis配置
  6. type RedisOptions struct {
  7. Host string `json:"host" mapstructure:"host" description:"Redis service host address"`
  8. Port int `json:"port"`
  9. Addrs []string `json:"addrs" mapstructure:"addrs"`
  10. Username string `json:"username" mapstructure:"username"`
  11. Password string `json:"password" mapstructure:"password"`
  12. Database int `json:"database" mapstructure:"database"`
  13. MasterName string `json:"master-name" mapstructure:"master-name"`
  14. MaxIdle int `json:"optimisation-max-idle" mapstructure:"optimisation-max-idle"`
  15. MaxActive int `json:"optimisation-max-active" mapstructure:"optimisation-max-active"`
  16. Timeout int `json:"timeout" mapstructure:"timeout"`
  17. EnableCluster bool `json:"enable-cluster" mapstructure:"enable-cluster"`
  18. UseSSL bool `json:"use-ssl" mapstructure:"use-ssl"`
  19. SSLInsecureSkipVerify bool `json:"ssl-insecure-skip-verify" mapstructure:"ssl-insecure-skip-verify"`
  20. }
  21. func NewRedisOptions() *RedisOptions {
  22. return &RedisOptions{
  23. Host: "127.0.0.1",
  24. Port: 6379,
  25. Addrs: []string{},
  26. Username: "",
  27. Password: "",
  28. Database: 0,
  29. MasterName: "",
  30. MaxIdle: 2000,
  31. MaxActive: 4000,
  32. Timeout: 0,
  33. EnableCluster: false,
  34. UseSSL: false,
  35. SSLInsecureSkipVerify: false,
  36. }
  37. }
  38. func (o *RedisOptions) Validate() []error {
  39. errs := []error{}
  40. return errs
  41. }
  42. func (o *RedisOptions) AddFlags(fs *pflag.FlagSet) {
  43. fs.StringVar(&o.Host, "redis.host", o.Host, "Hostname of your Redis server.")
  44. fs.IntVar(&o.Port, "redis.port", o.Port, "The port the Redis server is listening on.")
  45. fs.StringSliceVar(&o.Addrs, "redis.addrs", o.Addrs, "A set of redis address(format: 127.0.0.1:6379).")
  46. fs.StringVar(&o.Username, "redis.username", o.Username, "Username for access to redis service.")
  47. fs.StringVar(&o.Password, "redis.password", o.Password, "Optional auth password for Redis db.")
  48. fs.IntVar(&o.Database, "redis.database", o.Database, ""+
  49. "By default, the database is 0. Setting the database is not supported with redis cluster. "+
  50. "As such, if you have --redis.enable-cluster=true, then this value should be omitted or explicitly set to 0.")
  51. fs.StringVar(&o.MasterName, "redis.master-name", o.MasterName, "The name of master redis instance.")
  52. fs.IntVar(&o.MaxIdle, "redis.optimisation-max-idle", o.MaxIdle, ""+
  53. "This setting will configure how many connections are maintained in the pool when idle (no traffic). "+
  54. "Set the --redis.optimisation-max-active to something large, we usually leave it at around 2000 for "+
  55. "HA deployments.")
  56. fs.IntVar(&o.MaxActive, "redis.optimisation-max-active", o.MaxActive, ""+
  57. "In order to not over commit connections to the Redis server, we may limit the total "+
  58. "number of active connections to Redis. We recommend for production use to set this to around 4000.")
  59. fs.IntVar(&o.Timeout, "redis.timeout", o.Timeout, "Timeout (in seconds) when connecting to redis service.")
  60. fs.BoolVar(&o.EnableCluster, "redis.enable-cluster", o.EnableCluster, ""+
  61. "If you are using Redis cluster, enable it here to enable the slots mode.")
  62. fs.BoolVar(&o.UseSSL, "redis.use-ssl", o.UseSSL, ""+
  63. "If set, IAM will assume the connection to Redis is encrypted. "+
  64. "(use with Redis providers that support in-transit encryption).")
  65. fs.BoolVar(&o.SSLInsecureSkipVerify, "redis.ssl-insecure-skip-verify", o.SSLInsecureSkipVerify, ""+
  66. "Allows usage of self-signed certificates when connecting to an encrypted Redis database.")
  67. }