37 lines
658 B
Go
37 lines
658 B
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
goredis "github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type Redis struct {
|
|
Client *goredis.Client
|
|
}
|
|
|
|
func New(redisURL string) (*Redis, error) {
|
|
opts, err := goredis.ParseURL(redisURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("redis: invalid URL: %w", err)
|
|
}
|
|
|
|
client := goredis.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: failed to connect: %w", err)
|
|
}
|
|
|
|
return &Redis{Client: client}, nil
|
|
}
|
|
|
|
func (r *Redis) Close() error {
|
|
return r.Client.Close()
|
|
}
|