2025-02-24 16:48:20 +08:00
|
|
|
package redisaccessor
|
|
|
|
|
|
|
|
|
|
import (
|
2025-04-23 19:09:24 +08:00
|
|
|
"crypto/tls"
|
2025-03-06 13:30:24 +08:00
|
|
|
"fmt"
|
2025-03-05 21:21:44 +08:00
|
|
|
"legalgo-BE-go/config"
|
2025-02-24 16:48:20 +08:00
|
|
|
|
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var redisClient *redis.Client
|
|
|
|
|
|
|
|
|
|
func Get() *redis.Client {
|
|
|
|
|
return redisClient
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-06 13:30:24 +08:00
|
|
|
func New(cfg *config.Config) *redis.Client {
|
|
|
|
|
addr := fmt.Sprintf("%s:%s", cfg.Redis.Host, cfg.Redis.Port)
|
|
|
|
|
|
2025-04-23 19:09:24 +08:00
|
|
|
options := &redis.Options{
|
2025-03-06 13:30:24 +08:00
|
|
|
Addr: addr,
|
|
|
|
|
Password: cfg.Redis.Password,
|
|
|
|
|
DB: cfg.Redis.DB,
|
2025-04-23 19:09:24 +08:00
|
|
|
}
|
2025-02-24 16:48:20 +08:00
|
|
|
|
2025-04-23 19:09:24 +08:00
|
|
|
if cfg.Redis.Username != "" {
|
|
|
|
|
options.Username = cfg.Redis.Username
|
|
|
|
|
}
|
|
|
|
|
if cfg.Redis.SSL {
|
|
|
|
|
options.TLSConfig = &tls.Config{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
redisClient = redis.NewClient(options)
|
2025-02-24 16:48:20 +08:00
|
|
|
return redisClient
|
|
|
|
|
}
|