初始版本
This commit is contained in:
28
redisx/redisx.go
Normal file
28
redisx/redisx.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package redisx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
}
|
||||
|
||||
func Connect(ctx context.Context, cfg Config) (*redis.Client, error) {
|
||||
client := redis.NewClient(&redis.Options{Addr: fmt.Sprintf("%s:%d", cfg.Host, defaultInt(cfg.Port, 6379))})
|
||||
if err := client.Ping(ctx).Err(); err != nil {
|
||||
return client, err
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func defaultInt(v, fallback int) int {
|
||||
if v == 0 {
|
||||
return fallback
|
||||
}
|
||||
return v
|
||||
}
|
||||
Reference in New Issue
Block a user