package cache import ( "cockData/server/config" "cockData/server/log" "fmt" "github.com/go-redis/redis" ) var rdb *redis.Client // redis连接池 var rcdb *redis.ClusterClient // redis集群连接池 var _isCluster bool = false // 是否集群 func Init(config *config.RedisConfig) { if config.Cluster { _isCluster = true addrs := make([]string, 0) for _, clusterConfig := range *config.ClusterAddr { addr := fmt.Sprintf("%s:%d", clusterConfig.Host, clusterConfig.Port) addrs = append(addrs, addr) } opt := redis.ClusterOptions{ Addrs: addrs, Password: config.Password, PoolSize: config.PoolSize, } rcdb = redis.NewClusterClient(&opt) } else { opt := redis.Options{ Addr: fmt.Sprintf("%s:%d", config.Host, config.Port), Password: config.Password, DB: config.DatabaseIndex, PoolSize: config.PoolSize, } rdb = redis.NewClient(&opt) } if config.Cluster { if err := rcdb.Ping().Err(); err != nil { panic("Redis连接失败,请检查配置") } else { log.Info("Redis连接完毕...") } } else { if err := rdb.Ping().Err(); err != nil { panic("Redis连接失败,请检查配置") } else { log.Info("Redis连接完毕...") } } }