You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/stores/redis/conf.go

80 lines
1.7 KiB
Go

4 years ago
package redis
import (
"errors"
"time"
"github.com/tal-tech/go-zero/core/conf"
)
4 years ago
var (
// ErrEmptyHost is an error that indicates no redis host is set.
4 years ago
ErrEmptyHost = errors.New("empty redis host")
// ErrEmptyType is an error that indicates no redis type is set.
4 years ago
ErrEmptyType = errors.New("empty redis type")
// ErrEmptyKey is an error that indicates no redis key is set.
ErrEmptyKey = errors.New("empty redis key")
4 years ago
)
type (
// A RedisConf is a redis config.
4 years ago
RedisConf struct {
Host string
Type string `json:",default=node,options=node|cluster"`
Pass string `json:",optional"`
Tls bool `json:",default=false,options=true|false"`
SlowThreshold time.Duration `json:",default=100ms"`
4 years ago
}
// A RedisKeyConf is a redis config with key.
4 years ago
RedisKeyConf struct {
RedisConf
Key string `json:",optional"`
}
)
// NewRedis returns a Redis.
4 years ago
func (rc RedisConf) NewRedis() *Redis {
var opts []Option
if rc.Type == ClusterType {
opts = append(opts, Cluster())
}
if len(rc.Pass) > 0 {
opts = append(opts, WithPass(rc.Pass))
}
if rc.SlowThreshold > 0 {
opts = append(opts, WithSlowThreshold(conf.CheckedDuration(rc.SlowThreshold)))
}
if rc.Tls {
opts = append(opts, WithTLS())
}
return New(rc.Host, opts...)
4 years ago
}
// Validate validates the RedisConf.
4 years ago
func (rc RedisConf) Validate() error {
if len(rc.Host) == 0 {
return ErrEmptyHost
}
if len(rc.Type) == 0 {
return ErrEmptyType
}
return nil
}
// Validate validates the RedisKeyConf.
4 years ago
func (rkc RedisKeyConf) Validate() error {
if err := rkc.RedisConf.Validate(); err != nil {
return err
}
if len(rkc.Key) == 0 {
return ErrEmptyKey
}
return nil
}