32 lines
559 B
Go
32 lines
559 B
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type RedisDB struct {
|
|
Client *redis.Client
|
|
}
|
|
|
|
func ConnectRedis(url string) (*RedisDB, error) {
|
|
opt, err := redis.ParseURL(url)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid Redis URL: %w", err)
|
|
}
|
|
|
|
rdb := redis.NewClient(opt)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
if err := rdb.Ping(ctx).Err(); err != nil {
|
|
return nil, fmt.Errorf("failed to connect to Redis: %w", err)
|
|
}
|
|
|
|
return &RedisDB{Client: rdb}, nil
|
|
}
|