package redis
import (
"context"
"crypto/tls"
"net"
"time"
)
type UniversalOptions struct {
Addrs []string
ClientName string
DB int
Dialer func (ctx context .Context , network, addr string ) (net .Conn , error )
OnConnect func (ctx context .Context , cn *Conn ) error
Username string
Password string
SentinelUsername string
SentinelPassword string
MaxRetries int
MinRetryBackoff time .Duration
MaxRetryBackoff time .Duration
DialTimeout time .Duration
ReadTimeout time .Duration
WriteTimeout time .Duration
ContextTimeoutEnabled bool
PoolFIFO bool
PoolSize int
PoolTimeout time .Duration
MinIdleConns int
MaxIdleConns int
ConnMaxIdleTime time .Duration
ConnMaxLifetime time .Duration
TLSConfig *tls .Config
MaxRedirects int
ReadOnly bool
RouteByLatency bool
RouteRandomly bool
MasterName string
}
func (o *UniversalOptions ) Cluster () *ClusterOptions {
if len (o .Addrs ) == 0 {
o .Addrs = []string {"127.0.0.1:6379" }
}
return &ClusterOptions {
Addrs : o .Addrs ,
ClientName : o .ClientName ,
Dialer : o .Dialer ,
OnConnect : o .OnConnect ,
Username : o .Username ,
Password : o .Password ,
MaxRedirects : o .MaxRedirects ,
ReadOnly : o .ReadOnly ,
RouteByLatency : o .RouteByLatency ,
RouteRandomly : o .RouteRandomly ,
MaxRetries : o .MaxRetries ,
MinRetryBackoff : o .MinRetryBackoff ,
MaxRetryBackoff : o .MaxRetryBackoff ,
DialTimeout : o .DialTimeout ,
ReadTimeout : o .ReadTimeout ,
WriteTimeout : o .WriteTimeout ,
ContextTimeoutEnabled : o .ContextTimeoutEnabled ,
PoolFIFO : o .PoolFIFO ,
PoolSize : o .PoolSize ,
PoolTimeout : o .PoolTimeout ,
MinIdleConns : o .MinIdleConns ,
MaxIdleConns : o .MaxIdleConns ,
ConnMaxIdleTime : o .ConnMaxIdleTime ,
ConnMaxLifetime : o .ConnMaxLifetime ,
TLSConfig : o .TLSConfig ,
}
}
func (o *UniversalOptions ) Failover () *FailoverOptions {
if len (o .Addrs ) == 0 {
o .Addrs = []string {"127.0.0.1:26379" }
}
return &FailoverOptions {
SentinelAddrs : o .Addrs ,
MasterName : o .MasterName ,
ClientName : o .ClientName ,
Dialer : o .Dialer ,
OnConnect : o .OnConnect ,
DB : o .DB ,
Username : o .Username ,
Password : o .Password ,
SentinelUsername : o .SentinelUsername ,
SentinelPassword : o .SentinelPassword ,
MaxRetries : o .MaxRetries ,
MinRetryBackoff : o .MinRetryBackoff ,
MaxRetryBackoff : o .MaxRetryBackoff ,
DialTimeout : o .DialTimeout ,
ReadTimeout : o .ReadTimeout ,
WriteTimeout : o .WriteTimeout ,
ContextTimeoutEnabled : o .ContextTimeoutEnabled ,
PoolFIFO : o .PoolFIFO ,
PoolSize : o .PoolSize ,
PoolTimeout : o .PoolTimeout ,
MinIdleConns : o .MinIdleConns ,
MaxIdleConns : o .MaxIdleConns ,
ConnMaxIdleTime : o .ConnMaxIdleTime ,
ConnMaxLifetime : o .ConnMaxLifetime ,
TLSConfig : o .TLSConfig ,
}
}
func (o *UniversalOptions ) Simple () *Options {
addr := "127.0.0.1:6379"
if len (o .Addrs ) > 0 {
addr = o .Addrs [0 ]
}
return &Options {
Addr : addr ,
ClientName : o .ClientName ,
Dialer : o .Dialer ,
OnConnect : o .OnConnect ,
DB : o .DB ,
Username : o .Username ,
Password : o .Password ,
MaxRetries : o .MaxRetries ,
MinRetryBackoff : o .MinRetryBackoff ,
MaxRetryBackoff : o .MaxRetryBackoff ,
DialTimeout : o .DialTimeout ,
ReadTimeout : o .ReadTimeout ,
WriteTimeout : o .WriteTimeout ,
ContextTimeoutEnabled : o .ContextTimeoutEnabled ,
PoolFIFO : o .PoolFIFO ,
PoolSize : o .PoolSize ,
PoolTimeout : o .PoolTimeout ,
MinIdleConns : o .MinIdleConns ,
MaxIdleConns : o .MaxIdleConns ,
ConnMaxIdleTime : o .ConnMaxIdleTime ,
ConnMaxLifetime : o .ConnMaxLifetime ,
TLSConfig : o .TLSConfig ,
}
}
type UniversalClient interface {
Cmdable
AddHook (Hook )
Watch (ctx context .Context , fn func (*Tx ) error , keys ...string ) error
Do (ctx context .Context , args ...interface {}) *Cmd
Process (ctx context .Context , cmd Cmder ) error
Subscribe (ctx context .Context , channels ...string ) *PubSub
PSubscribe (ctx context .Context , channels ...string ) *PubSub
SSubscribe (ctx context .Context , channels ...string ) *PubSub
Close () error
PoolStats () *PoolStats
}
var (
_ UniversalClient = (*Client )(nil )
_ UniversalClient = (*ClusterClient )(nil )
_ UniversalClient = (*Ring )(nil )
)
func NewUniversalClient (opts *UniversalOptions ) UniversalClient {
if opts .MasterName != "" {
return NewFailoverClient (opts .Failover ())
} else if len (opts .Addrs ) > 1 {
return NewClusterClient (opts .Cluster ())
}
return NewClient (opts .Simple ())
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .