Skip to content

Commit

Permalink
feat: add redis cluster mode in option (#1178)
Browse files Browse the repository at this point in the history
Signed-off-by: rfyiamcool <[email protected]>
  • Loading branch information
rfyiamcool authored Oct 12, 2023
1 parent d8dbcbb commit 8b649a5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ config/config.yaml 文件为存储组件提供了详细的配置说明。
```
redis:
clusterMode: false #是否为 redis cluster 模式
address: [ 127.0.0.1:16379 ] #地址
username: #用户名
password: openIM123 #密码
Expand Down
7 changes: 4 additions & 3 deletions pkg/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ type configStruct struct {
} `yaml:"mongo"`

Redis struct {
Address []string `yaml:"address"`
Username string `yaml:"username"`
Password string `yaml:"password"`
ClusterMode bool `yaml:"clusterMode"`
Address []string `yaml:"address"`
Username string `yaml:"username"`
Password string `yaml:"password"`
} `yaml:"redis"`

Kafka struct {
Expand Down
5 changes: 3 additions & 2 deletions pkg/common/db/cache/init_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewRedis() (redis.UniversalClient, error) {
}
specialerror.AddReplace(redis.Nil, errs.ErrRecordNotFound)
var rdb redis.UniversalClient
if len(config.Config.Redis.Address) > 1 {
if len(config.Config.Redis.Address) > 1 || config.Config.Redis.ClusterMode {
rdb = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: config.Config.Redis.Address,
Username: config.Config.Redis.Username,
Expand All @@ -58,12 +58,13 @@ func NewRedis() (redis.UniversalClient, error) {
})
}

var err error = nil
var err error
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
err = rdb.Ping(ctx).Err()
if err != nil {
return nil, fmt.Errorf("redis ping %w", err)
}

return rdb, err
}

0 comments on commit 8b649a5

Please sign in to comment.