37 lines
625 B
Go
37 lines
625 B
Go
package cache
|
|
|
|
import (
|
|
"backend/config"
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type Redis struct {
|
|
client *redis.Client
|
|
}
|
|
|
|
func NewRedis(cfg config.Redis) (*Redis, error) {
|
|
rdb := redis.NewClient(&redis.Options{
|
|
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
|
|
Password: cfg.Pass,
|
|
DB: 0,
|
|
})
|
|
|
|
ctx := context.Background()
|
|
if err := rdb.Ping(ctx).Err(); err != nil {
|
|
return nil, fmt.Errorf("failed to connect to Redis: %w", err)
|
|
}
|
|
|
|
return &Redis{client: rdb}, nil
|
|
}
|
|
|
|
func (r *Redis) Client() *redis.Client {
|
|
return r.client
|
|
}
|
|
|
|
func (r *Redis) Close() error {
|
|
return r.client.Close()
|
|
}
|