Replace RedisCluster to RedisUniversalClient #114

Merged
vtolstov merged 2 commits from atolstikhin/micro-store-redis:universal_client_redis into master 2024-10-04 00:20:24 +03:00
2 changed files with 9 additions and 52 deletions
Showing only changes of commit 617f0f7811 - Show all commits

View File

@ -5,12 +5,6 @@ import (
"go.unistack.org/micro/v4/options"
)
type configKey struct{}
func Config(c *redis.Options) options.Option {
return options.ContextOption(configKey{}, c)
}
type universalConfigKey struct{}
func UniversalConfig(c *redis.UniversalOptions) options.Option {

View File

@ -30,7 +30,7 @@ func (r *Store) Init(opts ...options.Option) error {
}
func (r *Store) Redis() redis.UniversalClient {
return r.cli.(redis.UniversalClient)
return r.cli
}
func (r *Store) Disconnect(ctx context.Context) error {
@ -336,28 +336,13 @@ func NewStore(opts ...options.Option) *Store {
}
func (r *Store) configure() error {
var redisOptions *redis.Options
var redisUniversalOptions *redis.UniversalOptions
var err error
nodes := r.opts.Address
if len(nodes) == 0 {
nodes = []string{"redis://127.0.0.1:6379"}
}
if r.cli != nil && r.opts.Context == nil {
return nil
}
if r.opts.Context != nil {
if c, ok := r.opts.Context.Value(configKey{}).(*redis.Options); ok {
redisOptions = c
if r.opts.TLSConfig != nil {
redisOptions.TLSConfig = r.opts.TLSConfig
}
}
if c, ok := r.opts.Context.Value(universalConfigKey{}).(*redis.UniversalOptions); ok {
redisUniversalOptions = c
if r.opts.TLSConfig != nil {
@ -366,36 +351,12 @@ func (r *Store) configure() error {
}
}
if redisOptions != nil && redisUniversalOptions != nil {
return fmt.Errorf("must specify only one option Config or ClusterConfig")
}
if redisOptions == nil && redisUniversalOptions == nil && r.cli != nil {
if redisUniversalOptions == nil && r.cli != nil {
return nil
}
if redisOptions == nil && redisUniversalOptions == nil && len(nodes) == 1 {
redisOptions, err = redis.ParseURL(nodes[0])
if err != nil {
// Backwards compatibility
redisOptions = &redis.Options{
Addr: nodes[0],
Username: "",
Password: "", // no password set
DB: 0, // use default DB
MaxRetries: 2,
MaxRetryBackoff: 256 * time.Millisecond,
DialTimeout: 1 * time.Second,
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
PoolTimeout: 1 * time.Second,
MinIdleConns: 10,
TLSConfig: r.opts.TLSConfig,
}
}
} else if redisOptions == nil && redisUniversalOptions == nil && len(nodes) > 1 {
if redisUniversalOptions == nil {
redisUniversalOptions = &redis.UniversalOptions{
Addrs: nodes,
Username: "",
Password: "", // no password set
DB: 0, // use default DB
@ -410,11 +371,13 @@ func (r *Store) configure() error {
}
}
if redisOptions != nil {
r.cli = redis.NewClient(redisOptions)
} else if redisUniversalOptions != nil {
r.cli = redis.NewUniversalClient(redisUniversalOptions)
if len(r.opts.Address) > 0 {
redisUniversalOptions.Addrs = r.opts.Address
} else if len(redisUniversalOptions.Addrs) == 0 {
redisUniversalOptions.Addrs = []string{"redis://127.0.0.1:6379"}
}
r.cli = redis.NewUniversalClient(redisUniversalOptions)
return nil
}