33 lines
708 B
Go
33 lines
708 B
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
goredis "github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
func New(ctx context.Context, log *slog.Logger, url string) (*goredis.Client, error) {
|
|
opts, err := goredis.ParseURL(url)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse redis url: %w", err)
|
|
}
|
|
|
|
opts.DialTimeout = 1 * time.Second
|
|
opts.ReadTimeout = 200 * time.Millisecond
|
|
opts.WriteTimeout = 200 * time.Millisecond
|
|
opts.MaxRetries = -1
|
|
|
|
client := goredis.NewClient(opts)
|
|
|
|
pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
if err := client.Ping(pingCtx).Err(); err != nil {
|
|
log.Warn("redis unavailable, running without cache", "error", err)
|
|
}
|
|
|
|
return client, nil
|
|
}
|