Files
ginimageApi/configs/redis.go
Beyhan Oğur e04ba85564 first commit
2026-04-26 21:40:14 +03:00

47 lines
987 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package configs
import (
"context"
"fmt"
"log"
"os"
"github.com/redis/go-redis/v9"
)
// RDB global Redis istemcisi
var RDB *redis.Client
// ConnectRedis .env'deki REDIS_URL ile Redis bağlantısını kurar
// Format: redis://<user>:<password>@<host>:<port>/<db>
func ConnectRedis() error {
redisURL := os.Getenv("REDIS_URL")
if redisURL == "" {
return fmt.Errorf("REDIS_URL ortam değişkeni tanımlı değil")
}
opts, err := redis.ParseURL(redisURL)
if err != nil {
return fmt.Errorf("REDIS_URL ayrıştırılamadı: %w", err)
}
RDB = redis.NewClient(opts)
// Bağlantıyı test et
ctx := context.Background()
if _, err := RDB.Ping(ctx).Result(); err != nil {
return fmt.Errorf("Redis'e bağlanılamadı: %w", err)
}
log.Printf("✅ Redis bağlantısı kuruldu: %s (db=%d)", opts.Addr, opts.DB)
return nil
}
// CloseRedis Redis bağlantısını güvenli şekilde kapatır
func CloseRedis() error {
if RDB != nil {
return RDB.Close()
}
return nil
}