| 1 | // Package redisx opens the Redis client. |
| 2 | package redisx |
| 3 | |
| 4 | import ( |
| 5 | "context" |
| 6 | |
| 7 | "github.com/redis/go-redis/v9" |
| 8 | "github.com/redis/go-redis/v9/logging" |
| 9 | ) |
| 10 | |
| 11 | // Connect opens a client from a redis:// URL and checks that Redis answers. |
| 12 | func Connect(ctx context.Context, url string) (*redis.Client, error) { |
| 13 | opt, err := redis.ParseURL(url) |
| 14 | if err != nil { |
| 15 | return nil, err |
| 16 | } |
| 17 | // Without this, go-redis ignores a context's deadline. |
| 18 | opt.ContextTimeoutEnabled = true |
| 19 | logging.Disable() |
| 20 | rdb := redis.NewClient(opt) |
| 21 | if err := rdb.Ping(ctx).Err(); err != nil { |
| 22 | rdb.Close() |
| 23 | return nil, err |
| 24 | } |
| 25 | return rdb, nil |
| 26 | } |