45 lines
807 B
Go
45 lines
807 B
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"leafdev.top/Ecosystem/recommender/internal/base/conf"
|
|
|
|
"github.com/bsm/redislock"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type Redis struct {
|
|
Client *redis.Client
|
|
Locker *redislock.Client
|
|
config *conf.Config
|
|
}
|
|
|
|
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,
|
|
config: c,
|
|
}
|
|
|
|
return r
|
|
}
|
|
|
|
func (r *Redis) Prefix(key string) string {
|
|
return fmt.Sprintf("%s:%s", r.config.Redis.Prefix, key)
|
|
}
|