add redis client provider
This commit is contained in:
24
providers/database/redis/config.go
Normal file
24
providers/database/redis/config.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
const DefaultPrefix = "Redis"
|
||||
|
||||
type Config struct {
|
||||
Host string
|
||||
Port uint
|
||||
Password string
|
||||
DB uint
|
||||
}
|
||||
|
||||
func (c *Config) ToRedisOptions() *redis.Options {
|
||||
return &redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%d", c.Host, c.Port),
|
||||
Password: c.Password,
|
||||
DB: int(c.DB),
|
||||
}
|
||||
}
|
||||
29
providers/database/redis/redis.go
Normal file
29
providers/database/redis/redis.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rogeecn/atom/container"
|
||||
"github.com/rogeecn/atom/utils/opt"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
o := opt.New(opts...)
|
||||
var conf Config
|
||||
if err := o.UnmarshalConfig(&conf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return container.Container.Provide(func() (*redis.Client, error) {
|
||||
client := redis.NewClient(conf.ToRedisOptions())
|
||||
|
||||
_, err := client.Ping(context.Background()).Result()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to ping")
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}, o.DiOptions()...)
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package redis
|
||||
Reference in New Issue
Block a user