wynn/go/pkg/services/redis/component.go

48 lines
654 B
Go
Raw Normal View History

2025-03-02 08:38:31 +00:00
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
}