Skip to content

Commit

Permalink
Since Redis version 6.0, Redis has introduced access control based on…
Browse files Browse the repository at this point in the history
… username and password. Therefore, a new User configuration item has been added to the cache module.
  • Loading branch information
xujb977644703 authored and kevwan committed Jan 22, 2025
1 parent 00e0db5 commit 1e63f91
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/stores/redis/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type (
RedisConf struct {
Host string
Type string `json:",default=node,options=node|cluster"`
User string `json:",optional"`
Pass string `json:",optional"`
Tls bool `json:",optional"`
NonBlock bool `json:",default=true"`
Expand All @@ -40,6 +41,9 @@ func (rc RedisConf) NewRedis() *Redis {
if rc.Type == ClusterType {
opts = append(opts, Cluster())
}
if len(rc.User) > 0 {
opts = append(opts, WithUser(rc.User))
}

Check warning on line 46 in core/stores/redis/conf.go

View check run for this annotation

Codecov / codecov/patch

core/stores/redis/conf.go#L45-L46

Added lines #L45 - L46 were not covered by tests
if len(rc.Pass) > 0 {
opts = append(opts, WithPass(rc.Pass))
}
Expand Down
11 changes: 11 additions & 0 deletions core/stores/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type (
Redis struct {
Addr string
Type string
User string
Pass string
tls bool
brk breaker.Breaker
Expand Down Expand Up @@ -126,6 +127,9 @@ func NewRedis(conf RedisConf, opts ...Option) (*Redis, error) {
if conf.Type == ClusterType {
opts = append([]Option{Cluster()}, opts...)
}
if len(conf.User) > 0 {
opts = append([]Option{WithUser(conf.User)}, opts...)
}

Check warning on line 132 in core/stores/redis/redis.go

View check run for this annotation

Codecov / codecov/patch

core/stores/redis/redis.go#L131-L132

Added lines #L131 - L132 were not covered by tests
if len(conf.Pass) > 0 {
opts = append([]Option{WithPass(conf.Pass)}, opts...)
}
Expand Down Expand Up @@ -2405,6 +2409,13 @@ func SetSlowThreshold(threshold time.Duration) {
slowThreshold.Set(threshold)
}

// WithPass customizes the given Redis with given password.
func WithUser(user string) Option {
return func(r *Redis) {
r.User = user
}

Check warning on line 2416 in core/stores/redis/redis.go

View check run for this annotation

Codecov / codecov/patch

core/stores/redis/redis.go#L2413-L2416

Added lines #L2413 - L2416 were not covered by tests
}

// WithPass customizes the given Redis with given password.
func WithPass(pass string) Option {
return func(r *Redis) {
Expand Down

0 comments on commit 1e63f91

Please sign in to comment.