package app import ( "context" "fmt" "time" "github.com/gochat/gochat/internal/config" "github.com/gochat/gochat/pkg/logger" "github.com/redis/go-redis/v9" ) // NewRedisClient creates a Redis client for caching, Pub/Sub, and sessions. // Reference: Chatwoot config/cable.yml Redis adapter configuration func NewRedisClient(cfg *config.RedisConfig) (*redis.Client, error) { opts, err := redis.ParseURL(cfg.DSN) if err != nil { return nil, fmt.Errorf("failed to parse Redis DSN: %w", err) } poolSize := cfg.PoolSize if poolSize <= 0 { poolSize = 50 } opts.PoolSize = poolSize client := redis.NewClient(opts) // Verify connection ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := client.Ping(ctx).Err(); err != nil { return nil, fmt.Errorf("failed to connect to Redis: %w", err) } logger.L().Infof("Connected to Redis: %s (db=%d, pool=%d)", opts.Addr, opts.DB, poolSize) return client, nil }