first commit
This commit is contained in:
46
configs/redis.go
Normal file
46
configs/redis.go
Normal file
@@ -0,0 +1,46 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user