29 lines
564 B
Go
29 lines
564 B
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"example.com/m/internal/config"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
func InitRedis(cfg *config.Config) (*redis.Client, error) {
|
|
opt, err := redis.ParseURL(cfg.RedisURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse redis url: %w", err)
|
|
}
|
|
|
|
client := redis.NewClient(opt)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
if err := client.Ping(ctx).Err(); err != nil {
|
|
return nil, fmt.Errorf("failed to connect to redis: %w", err)
|
|
}
|
|
|
|
return client, nil
|
|
}
|