i_redis.go 702 B

1234567891011121314151617181920212223242526272829
  1. package infra
  2. import (
  3. "github.com/go-redis/redis"
  4. "sync"
  5. )
  6. var (
  7. once sync.Once
  8. GlobalRedisClient *redis.Client
  9. )
  10. // 获取全局的Redis客户端
  11. func InitRedisClient(addr string, password string, db int) {
  12. once.Do(func() {
  13. // 创建Redis连接
  14. GlobalRedisClient = redis.NewClient(&redis.Options{
  15. Addr: addr, // Redis地址
  16. Password: password, // Redis密码,如果没有密码则留空
  17. DB: db, // Redis数据库索引
  18. })
  19. })
  20. // 检查连接是否成功
  21. _, err := GlobalRedisClient.Ping().Result()
  22. if err != nil {
  23. GlobalLogger.Error("初始化 Redis 客户端报错:", err)
  24. }
  25. GlobalLogger.Error("初始化 Redis 客户端成功。")
  26. }