48 lines
654 B
Go
48 lines
654 B
Go
|
package redis
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"log/slog"
|
||
|
|
||
|
"github.com/redis/rueidis"
|
||
|
"go.uber.org/config"
|
||
|
"go.uber.org/fx"
|
||
|
)
|
||
|
|
||
|
type Params struct {
|
||
|
fx.In
|
||
|
|
||
|
Config config.Provider
|
||
|
Ctx context.Context
|
||
|
Lc fx.Lifecycle
|
||
|
Log *slog.Logger
|
||
|
}
|
||
|
|
||
|
type Result struct {
|
||
|
fx.Out
|
||
|
|
||
|
client rueidis.Client
|
||
|
}
|
||
|
|
||
|
func New(p Params) (r Result, err error) {
|
||
|
|
||
|
var cfg struct {
|
||
|
Url string
|
||
|
}
|
||
|
|
||
|
err = p.Config.Get("redis").Populate(&cfg)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
redisConfig, err := rueidis.ParseURL(cfg.Url)
|
||
|
if err != nil {
|
||
|
return r, err
|
||
|
}
|
||
|
redisClient, err := rueidis.NewClient(redisConfig)
|
||
|
if err != nil {
|
||
|
return r, err
|
||
|
}
|
||
|
r.client = redisClient
|
||
|
return
|
||
|
}
|