Replace RedisCluster to RedisUniversalClient
Some checks failed
autoapprove / autoapprove (pull_request) Failing after 1s
automerge / automerge (pull_request) Has been skipped
dependabot-automerge / automerge (pull_request) Has been skipped
codeql / analyze (go) (pull_request) Has been cancelled
prbuild / test (pull_request) Has been cancelled
prbuild / lint (pull_request) Has been cancelled

This commit is contained in:
Aleksandr Tolstikhin 2024-10-03 00:21:15 +07:00
parent 195c604a31
commit 8448680806
2 changed files with 18 additions and 31 deletions

View File

@ -11,8 +11,8 @@ func Config(c *redis.Options) options.Option {
return options.ContextOption(configKey{}, c) return options.ContextOption(configKey{}, c)
} }
type clusterConfigKey struct{} type universalConfigKey struct{}
func ClusterConfig(c *redis.ClusterOptions) options.Option { func UniversalConfig(c *redis.UniversalOptions) options.Option {
return options.ContextOption(clusterConfigKey{}, c) return options.ContextOption(universalConfigKey{}, c)
} }

View File

@ -14,21 +14,7 @@ import (
type Store struct { type Store struct {
opts store.Options opts store.Options
cli redisClient cli redis.UniversalClient
}
type redisClient interface {
Get(ctx context.Context, key string) *redis.StringCmd
Del(ctx context.Context, keys ...string) *redis.IntCmd
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
Keys(ctx context.Context, pattern string) *redis.StringSliceCmd
MGet(ctx context.Context, keys ...string) *redis.SliceCmd
MSet(ctx context.Context, kv ...interface{}) *redis.StatusCmd
Exists(ctx context.Context, keys ...string) *redis.IntCmd
Ping(ctx context.Context) *redis.StatusCmd
Pipeline() redis.Pipeliner
Pipelined(ctx context.Context, fn func(redis.Pipeliner) error) ([]redis.Cmder, error)
Close() error
} }
func (r *Store) Connect(ctx context.Context) error { func (r *Store) Connect(ctx context.Context) error {
@ -43,8 +29,8 @@ func (r *Store) Init(opts ...options.Option) error {
return r.configure() return r.configure()
} }
func (r *Store) Redis() *redis.Client { func (r *Store) Redis() redis.UniversalClient {
return r.cli.(*redis.Client) return r.cli.(redis.UniversalClient)
} }
func (r *Store) Disconnect(ctx context.Context) error { func (r *Store) Disconnect(ctx context.Context) error {
@ -351,7 +337,7 @@ func NewStore(opts ...options.Option) *Store {
func (r *Store) configure() error { func (r *Store) configure() error {
var redisOptions *redis.Options var redisOptions *redis.Options
var redisClusterOptions *redis.ClusterOptions var redisUniversalOptions *redis.UniversalOptions
var err error var err error
nodes := r.opts.Address nodes := r.opts.Address
@ -372,23 +358,23 @@ func (r *Store) configure() error {
} }
} }
if c, ok := r.opts.Context.Value(clusterConfigKey{}).(*redis.ClusterOptions); ok { if c, ok := r.opts.Context.Value(universalConfigKey{}).(*redis.UniversalOptions); ok {
redisClusterOptions = c redisUniversalOptions = c
if r.opts.TLSConfig != nil { if r.opts.TLSConfig != nil {
redisClusterOptions.TLSConfig = r.opts.TLSConfig redisUniversalOptions.TLSConfig = r.opts.TLSConfig
} }
} }
} }
if redisOptions != nil && redisClusterOptions != nil { if redisOptions != nil && redisUniversalOptions != nil {
return fmt.Errorf("must specify only one option Config or ClusterConfig") return fmt.Errorf("must specify only one option Config or ClusterConfig")
} }
if redisOptions == nil && redisClusterOptions == nil && r.cli != nil { if redisOptions == nil && redisUniversalOptions == nil && r.cli != nil {
return nil return nil
} }
if redisOptions == nil && redisClusterOptions == nil && len(nodes) == 1 { if redisOptions == nil && redisUniversalOptions == nil && len(nodes) == 1 {
redisOptions, err = redis.ParseURL(nodes[0]) redisOptions, err = redis.ParseURL(nodes[0])
if err != nil { if err != nil {
// Backwards compatibility // Backwards compatibility
@ -407,11 +393,12 @@ func (r *Store) configure() error {
TLSConfig: r.opts.TLSConfig, TLSConfig: r.opts.TLSConfig,
} }
} }
} else if redisOptions == nil && redisClusterOptions == nil && len(nodes) > 1 { } else if redisOptions == nil && redisUniversalOptions == nil && len(nodes) > 1 {
redisClusterOptions = &redis.ClusterOptions{ redisUniversalOptions = &redis.UniversalOptions{
Addrs: nodes, Addrs: nodes,
Username: "", Username: "",
Password: "", // no password set Password: "", // no password set
DB: 0, // use default DB
MaxRetries: 2, MaxRetries: 2,
MaxRetryBackoff: 256 * time.Millisecond, MaxRetryBackoff: 256 * time.Millisecond,
DialTimeout: 1 * time.Second, DialTimeout: 1 * time.Second,
@ -425,8 +412,8 @@ func (r *Store) configure() error {
if redisOptions != nil { if redisOptions != nil {
r.cli = redis.NewClient(redisOptions) r.cli = redis.NewClient(redisOptions)
} else if redisClusterOptions != nil { } else if redisUniversalOptions != nil {
r.cli = redis.NewClusterClient(redisClusterOptions) r.cli = redis.NewUniversalClient(redisUniversalOptions)
} }
return nil return nil