mirror of
https://github.com/gofiber/storage.git
synced 2025-10-05 16:48:25 +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,
|
||||
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
|
||||
@@ -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://<user>:<pass>@localhost:6379/<db>
|
||||
// Optional. Default is ""
|
||||
URL string
|
||||
|
||||
// Reset clears any existing keys in existing Collection
|
||||
//
|
||||
// Optional. Default is false
|
||||
|
@@ -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://<user>:<pass>@localhost:6379/<db>
|
||||
// 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,
|
||||
}
|
||||
|
@@ -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 {
|
||||
|
@@ -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())
|
||||
}
|
||||
|
Reference in New Issue
Block a user