mirror of
https://github.com/gofiber/storage.git
synced 2025-09-27 04:46:08 +08:00
Add support for ParseURL to rueidis driver
This commit is contained in:
@@ -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"},
|
||||
@@ -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
|
||||
|
@@ -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
|
||||
@@ -100,6 +106,7 @@ var ConfigDefault = Config{
|
||||
Username: "",
|
||||
Password: "",
|
||||
ClientName: "",
|
||||
URL: "",
|
||||
SelectDB: 0,
|
||||
InitAddress: []string{"127.0.0.1:6379"},
|
||||
TLSConfig: nil,
|
||||
@@ -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
|
||||
}
|
||||
|
@@ -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,
|
||||
|
@@ -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{
|
||||
|
Reference in New Issue
Block a user