Skip to content

Commit

Permalink
Add support for ParseURL to rueidis driver
Browse files Browse the repository at this point in the history
  • Loading branch information
gaby committed Sep 10, 2023
1 parent 0e55883 commit 0f22927
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
11 changes: 11 additions & 0 deletions rueidis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ store := rueidis.New(rueidis.Config{
TLSConfig: nil,
})

// Initialize using Rueidis URL
store := rueidis.New(rueidis.Config{
URL: "redis://localhost:6379",
})

// Initialize Rueidis Cluster Client
store := rueidis.New(rueidis.Config{
InitAddress: []string{":6379", ":6380"},
Expand Down Expand Up @@ -105,6 +110,12 @@ type Config struct {
// Optional. Default is ""
ClientName string

// URL standard format Redis URL. If this is set all other config options, InitAddress, Username, Password, ClientName, and SelectDB have no effect.
//
// Example: redis://<user>:<pass>@localhost:6379/<db>
// Optional. Default is ""
URL string

// SelectDB to be selected after connecting to the server.
//
// Optional. Default is 0
Expand Down
10 changes: 10 additions & 0 deletions rueidis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ type Config struct {
// Optional. Default is ""
ClientName string

// URL standard format Redis URL. If this is set all other config options, InitAddress, Username, Password, ClientName, and SelectDB have no effect.
//
// Example: redis://<user>:<pass>@localhost:6379/<db>
// Optional. Default is ""
URL string

// SelectDB to be selected after connecting to the server.
//
// Optional. Default is 0
Expand Down Expand Up @@ -100,6 +106,7 @@ var ConfigDefault = Config{
Username: "",
Password: "",
ClientName: "",
URL: "",
SelectDB: 0,
InitAddress: []string{"127.0.0.1:6379"},
TLSConfig: nil,
Expand Down Expand Up @@ -138,6 +145,9 @@ func configDefault(config ...Config) Config {
if userConfig.ClientName != "" {
cfg.ClientName = userConfig.ClientName
}
if userConfig.URL != "" {
cfg.URL = userConfig.URL
}
if userConfig.SelectDB != 0 {
cfg.SelectDB = userConfig.SelectDB
}
Expand Down
22 changes: 22 additions & 0 deletions rueidis/rueidis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ func New(config ...Config) *Storage {
var db rueidis.Client
cacheTTL = cfg.CacheTTL

// Parse the URL and update config values accordingly
if cfg.URL != "" {
// This will panic if parsing URL fails
options := rueidis.MustParseURL(cfg.URL)

// Update the config values with the parsed URL values
cfg.InitAddress = options.InitAddress
cfg.Username = options.Username
cfg.Password = options.Password
cfg.SelectDB = options.SelectDB

// Update ClientName if returned
if cfg.ClientName == "" && options.ClientName != "" {
cfg.ClientName = options.ClientName
}

// Update TLSConfig if returned
if cfg.TLSConfig == nil && options.TLSConfig != nil {
cfg.TLSConfig = options.TLSConfig
}
}

// Update config values accordingly and start new Client
db, err := rueidis.NewClient(rueidis.ClientOption{
Username: cfg.Username,
Expand Down
22 changes: 22 additions & 0 deletions rueidis/rueidis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,28 @@ func Test_Rueidis_With_HostPort(t *testing.T) {
require.Nil(t, store.Close())
}

func Test_Rueidis_With_URL(t *testing.T) {
store := New(Config{
URL: "redis://localhost:6379",
})

var (
key = "bruce"
val = []byte("wayne")
)

err := store.Set(key, val, 0)
require.NoError(t, err)

result, err := store.Get(key)
require.NoError(t, err)
require.Equal(t, val, result)

err = store.Delete(key)
require.NoError(t, err)
require.Nil(t, store.Close())
}

func Test_Rueidis_Cluster(t *testing.T) {
store := New(Config{
InitAddress: []string{
Expand Down

0 comments on commit 0f22927

Please sign in to comment.