add redis-config Auth Username (#2684)

This commit is contained in:
wanov
2023-07-06 20:58:31 +08:00
committed by GitHub
parent 0dc47609b5
commit 39810a520c
3 changed files with 38 additions and 0 deletions

View File

@ -44,6 +44,7 @@ func New(config *gredis.Config) *Redis {
fillWithDefaultConfiguration(config)
opts := &redis.UniversalOptions{
Addrs: gstr.SplitAndTrim(config.Address, ","),
Username: config.User,
Password: config.Pass,
DB: config.Db,
MaxRetries: defaultMaxRetries,

View File

@ -7,6 +7,7 @@
package redis_test
import (
"github.com/gogf/gf/v2/container/gvar"
"testing"
"time"
@ -34,3 +35,38 @@ func Test_ConfigFromMap(t *testing.T) {
t.Assert(c.ReadTimeout, 10*time.Second)
})
}
func Test_ConfigAddUser(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
c *gredis.Redis
err error
r *gvar.Var
)
c, err = gredis.New(&gredis.Config{
Address: `127.0.0.1`,
Db: 1,
User: "root",
Pass: "",
})
t.AssertNil(err)
_, err = c.Conn(ctx)
t.AssertNil(err)
_, err = redis.Do(ctx, "SET", "k", "v")
t.AssertNil(err)
r, err = redis.Do(ctx, "GET", "k")
t.AssertNil(err)
t.Assert(r, []byte("v"))
_, err = redis.Do(ctx, "DEL", "k")
t.AssertNil(err)
r, err = redis.Do(ctx, "GET", "k")
t.AssertNil(err)
t.Assert(r, nil)
})
}

View File

@ -23,6 +23,7 @@ type Config struct {
// Address It supports single and cluster redis server. Multiple addresses joined with char ','. Eg: 192.168.1.1:6379, 192.168.1.2:6379.
Address string `json:"address"`
Db int `json:"db"` // Redis db.
User string `json:"user"` // Username for AUTH.
Pass string `json:"pass"` // Password for AUTH.
MinIdle int `json:"minIdle"` // Minimum number of connections allowed to be idle (default is 0)
MaxIdle int `json:"maxIdle"` // Maximum number of connections allowed to be idle (default is 10)