This commit is contained in:
@@ -2,32 +2,27 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"example.com/m/internal/config"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var Redis *redis.Client
|
||||
|
||||
func InitRedis(cfg *config.Config) (*redis.Client, error) {
|
||||
opt, err := redis.ParseURL(cfg.RedisURL)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("impossible to parse redis url: %v", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("failed to parse redis url: %w", err)
|
||||
}
|
||||
|
||||
Redis = redis.NewClient(opt)
|
||||
client := redis.NewClient(opt)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := Redis.Ping(ctx).Err(); err != nil {
|
||||
log.Fatalf("impossible to conenct to redis db: %v", err)
|
||||
return nil, err
|
||||
if err := client.Ping(ctx).Err(); err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to redis: %w", err)
|
||||
}
|
||||
|
||||
return Redis, nil
|
||||
return client, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user