//go:build redis package cache import ( "context" "fmt" "time" "github.com/redis/go-redis/v9" ) // NewRedisClient creates a Redis client from a DSN (e.g. "redis://localhost:6379/0") // and verifies connectivity with a PING. func NewRedisClient(dsn string) (*redis.Client, error) { opts, err := redis.ParseURL(dsn) if err != nil { return nil, fmt.Errorf("redis: invalid DSN: %w", err) } opts.PoolSize = 10 opts.MinIdleConns = 2 opts.DialTimeout = 5 * time.Second opts.ReadTimeout = 3 * time.Second opts.WriteTimeout = 3 * time.Second client := redis.NewClient(opts) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := client.Ping(ctx).Err(); err != nil { client.Close() return nil, fmt.Errorf("redis: ping failed: %w", err) } return client, nil }