mirror of
https://github.com/gofiber/storage.git
synced 2025-10-05 08:37:10 +08:00
URL parsing for redis (#64)
* URL parsing for redis Co-authored-by: TroyDota <49777269+TroyDota@users.noreply.github.com>
This commit is contained in:
@@ -48,6 +48,12 @@ store := redis.New(redis.Config{
|
|||||||
Database: 0,
|
Database: 0,
|
||||||
Reset: false,
|
Reset: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// or just the url with all information
|
||||||
|
store = redis.New(redis.Config{
|
||||||
|
URL: "redis://<user>:<pass>@127.0.0.1:6379/<db>",
|
||||||
|
Reset: false,
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
### Config
|
### Config
|
||||||
@@ -78,6 +84,12 @@ type Config struct {
|
|||||||
// Optional. Default is 0
|
// Optional. Default is 0
|
||||||
Database int
|
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://<user>:<pass>@localhost:6379/<db>
|
||||||
|
// Optional. Default is ""
|
||||||
|
URL string
|
||||||
|
|
||||||
// Reset clears any existing keys in existing Collection
|
// Reset clears any existing keys in existing Collection
|
||||||
//
|
//
|
||||||
// Optional. Default is false
|
// Optional. Default is false
|
||||||
|
@@ -27,6 +27,12 @@ type Config struct {
|
|||||||
// Optional. Default is 0
|
// Optional. Default is 0
|
||||||
Database int
|
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://<user>:<pass>@localhost:6379/<db>
|
||||||
|
// Optional. Default is ""
|
||||||
|
URL string
|
||||||
|
|
||||||
// Reset clears any existing keys in existing Collection
|
// Reset clears any existing keys in existing Collection
|
||||||
//
|
//
|
||||||
// Optional. Default is false
|
// Optional. Default is false
|
||||||
@@ -45,6 +51,7 @@ var ConfigDefault = Config{
|
|||||||
Port: 6379,
|
Port: 6379,
|
||||||
Username: "",
|
Username: "",
|
||||||
Password: "",
|
Password: "",
|
||||||
|
URL: "",
|
||||||
Database: 0,
|
Database: 0,
|
||||||
Reset: false,
|
Reset: false,
|
||||||
}
|
}
|
||||||
|
@@ -19,12 +19,25 @@ func New(config ...Config) *Storage {
|
|||||||
cfg := configDefault(config...)
|
cfg := configDefault(config...)
|
||||||
|
|
||||||
// Create new redis client
|
// Create new redis client
|
||||||
db := redis.NewClient(&redis.Options{
|
|
||||||
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
|
var options *redis.Options
|
||||||
DB: cfg.Database,
|
var err error
|
||||||
Username: cfg.Username,
|
|
||||||
Password: cfg.Password,
|
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
|
// Test connection
|
||||||
if err := db.Ping(context.Background()).Err(); err != nil {
|
if err := db.Ping(context.Background()).Err(); err != nil {
|
||||||
|
@@ -120,3 +120,25 @@ func Test_Redis_Reset(t *testing.T) {
|
|||||||
func Test_Redis_Close(t *testing.T) {
|
func Test_Redis_Close(t *testing.T) {
|
||||||
utils.AssertEqual(t, nil, testStore.Close())
|
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())
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user