recommender/internal/base/redis/provide.go

39 lines
669 B
Go
Raw Normal View History

2024-11-06 10:47:56 +00:00
package redis
import (
"context"
"fmt"
2024-11-06 12:35:16 +00:00
"leafdev.top/Ecosystem/recommender/internal/base/conf"
2024-11-06 10:47:56 +00:00
"github.com/bsm/redislock"
"github.com/redis/go-redis/v9"
)
type Redis struct {
Client *redis.Client
Locker *redislock.Client
}
func NewRedis(c *conf.Config) *Redis {
var client = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", c.Redis.Host, c.Redis.Port),
Password: c.Redis.Password,
DB: c.Redis.DB,
})
status := client.Ping(context.Background())
if status.Err() != nil {
panic(status.Err())
}
// Create a new lock client.
locker := redislock.New(client)
var r = &Redis{
Client: client,
Locker: locker,
}
return r
}