diff --git a/redis/README.md b/redis/README.md index 034140f6..3e5e119f 100644 --- a/redis/README.md +++ b/redis/README.md @@ -48,6 +48,12 @@ store := redis.New(redis.Config{ Database: 0, Reset: false, }) + +// or just the url with all information +store = redis.New(redis.Config{ + URL: "redis://:@127.0.0.1:6379/", + Reset: false, +}) ``` ### Config @@ -78,6 +84,12 @@ type Config struct { // Optional. Default is 0 Database int + // URL the standard format redis url to parse all other options. If this is set all other config options, Host, Port, Username, Password, Database have no effect. + // + // Example: redis://:@localhost:6379/ + // Optional. Default is "" + URL string + // Reset clears any existing keys in existing Collection // // Optional. Default is false diff --git a/redis/config.go b/redis/config.go index e70f995d..3f0d9a83 100644 --- a/redis/config.go +++ b/redis/config.go @@ -27,6 +27,12 @@ type Config struct { // Optional. Default is 0 Database int + // URL the standard format redis url to parse all other options. If this is set all other config options, Host, Port, Username, Password, Database have no effect. + // + // Example: redis://:@localhost:6379/ + // Optional. Default is "" + URL string + // Reset clears any existing keys in existing Collection // // Optional. Default is false @@ -45,6 +51,7 @@ var ConfigDefault = Config{ Port: 6379, Username: "", Password: "", + URL: "", Database: 0, Reset: false, } diff --git a/redis/redis.go b/redis/redis.go index f9f9facb..d703cf06 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -19,12 +19,25 @@ func New(config ...Config) *Storage { cfg := configDefault(config...) // Create new redis client - db := redis.NewClient(&redis.Options{ - Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), - DB: cfg.Database, - Username: cfg.Username, - Password: cfg.Password, - }) + + var options *redis.Options + var err error + + if cfg.URL != "" { + options, err = redis.ParseURL(cfg.URL) + if err != nil { + panic(err) + } + } else { + options = &redis.Options{ + Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), + DB: cfg.Database, + Username: cfg.Username, + Password: cfg.Password, + } + } + + db := redis.NewClient(options) // Test connection if err := db.Ping(context.Background()).Err(); err != nil { diff --git a/redis/redis_test.go b/redis/redis_test.go index 21d0b25f..8e09d1a6 100644 --- a/redis/redis_test.go +++ b/redis/redis_test.go @@ -120,3 +120,25 @@ func Test_Redis_Reset(t *testing.T) { func Test_Redis_Close(t *testing.T) { utils.AssertEqual(t, nil, testStore.Close()) } + +func Test_Redis_Initalize_WithURL(t *testing.T) { + testStoreUrl := New(Config{ + URL: "redis://localhost:6379", + }) + var ( + key = "clark" + val = []byte("kent") + ) + + err := testStoreUrl.Set(key, val, 0) + utils.AssertEqual(t, nil, err) + + result, err := testStoreUrl.Get(key) + utils.AssertEqual(t, nil, err) + utils.AssertEqual(t, val, result) + + err = testStoreUrl.Delete(key) + utils.AssertEqual(t, nil, err) + + utils.AssertEqual(t, nil, testStoreUrl.Close()) +}