示例#1
0
func NewRedisClusterPool(forceReconnect bool, config RedisStorageConfig) *rediscluster.RedisCluster {
	if !forceReconnect {
		if redisClusterSingleton != nil {
			log.WithFields(logrus.Fields{
				"prefix": redisLogPrefix,
			}).Debug("Redis pool already INITIALISED")
			return redisClusterSingleton
		}
	} else {
		if redisClusterSingleton != nil {
			redisClusterSingleton.CloseConnection()
		}
	}

	log.WithFields(logrus.Fields{
		"prefix": redisLogPrefix,
	}).Debug("Creating new Redis connection pool")

	maxIdle := 100
	if config.MaxIdle > 0 {
		maxIdle = config.MaxIdle
	}

	maxActive := 500
	if config.MaxActive > 0 {
		maxActive = config.MaxActive
	}

	if config.EnableCluster {
		log.WithFields(logrus.Fields{
			"prefix": redisLogPrefix,
		}).Info("--> Using clustered mode")
	}

	thisPoolConf := rediscluster.PoolConfig{
		MaxIdle:     maxIdle,
		MaxActive:   maxActive,
		IdleTimeout: 240 * time.Second,
		Database:    config.Database,
		Password:    config.Password,
		IsCluster:   config.EnableCluster,
	}

	seed_redii := []map[string]string{}

	if len(config.Hosts) > 0 {
		for h, p := range config.Hosts {
			seed_redii = append(seed_redii, map[string]string{h: p})
		}
	} else {
		seed_redii = append(seed_redii, map[string]string{config.Host: strconv.Itoa(config.Port)})
	}

	thisInstance := rediscluster.NewRedisCluster(seed_redii, thisPoolConf, false)

	redisClusterSingleton = &thisInstance

	return &thisInstance
}
func NewRedisClusterPool() *rediscluster.RedisCluster {
	if redisClusterSingleton != nil {
		log.Debug("Redis pool already INITIALISED")
		return redisClusterSingleton
	}

	log.Info("Creating new Redis connection pool")

	maxIdle := 100
	if config.Storage.MaxIdle > 0 {
		maxIdle = config.Storage.MaxIdle
	}

	maxActive := 500
	if config.Storage.MaxActive > 0 {
		maxActive = config.Storage.MaxActive
	}

	if config.Storage.EnableCluster {
		log.Info("Using clustered mode")
	}

	thisPoolConf := rediscluster.PoolConfig{
		MaxIdle:     maxIdle,
		MaxActive:   maxActive,
		IdleTimeout: 240 * time.Second,
		Database:    config.Storage.Database,
		Password:    config.Storage.Password,
		IsCluster:   config.Storage.EnableCluster,
	}

	seed_redii := []map[string]string{}

	if len(config.Storage.Hosts) > 0 {
		for h, p := range config.Storage.Hosts {
			seed_redii = append(seed_redii, map[string]string{h: p})
		}
	} else {
		seed_redii = append(seed_redii, map[string]string{config.Storage.Host: strconv.Itoa(config.Storage.Port)})
	}

	thisInstance := rediscluster.NewRedisCluster(seed_redii, thisPoolConf, false)

	redisClusterSingleton = &thisInstance

	return &thisInstance
}
func NewRedisClusterPool(forceReconnect bool, isCache bool) *rediscluster.RedisCluster {
	redisPtr := redisClusterSingleton
	cfg := config.Storage
	if isCache {
		if config.EnableSeperateCacheStore {
			redisPtr = redisCacheClusterSingleton
			cfg = config.CacheStorage
		}

	}

	if !forceReconnect {
		if redisPtr != nil {
			log.Debug("Redis pool already INITIALISED")
			return redisPtr
		}
	} else {
		if redisPtr != nil {
			redisPtr.CloseConnection()
		}
	}

	log.Debug("Creating new Redis connection pool")

	maxIdle := 100
	if cfg.MaxIdle > 0 {
		maxIdle = cfg.MaxIdle
	}

	maxActive := 500
	if cfg.MaxActive > 0 {
		maxActive = cfg.MaxActive
	}

	if cfg.EnableCluster {
		log.Info("--> Using clustered mode")
	}

	thisPoolConf := rediscluster.PoolConfig{
		MaxIdle:     maxIdle,
		MaxActive:   maxActive,
		IdleTimeout: 240 * time.Second,
		Database:    cfg.Database,
		Password:    cfg.Password,
		IsCluster:   cfg.EnableCluster,
	}

	seed_redii := []map[string]string{}

	if len(cfg.Hosts) > 0 {
		for h, p := range cfg.Hosts {
			seed_redii = append(seed_redii, map[string]string{h: p})
		}
	} else {
		seed_redii = append(seed_redii, map[string]string{cfg.Host: strconv.Itoa(cfg.Port)})
	}

	thisInstance := rediscluster.NewRedisCluster(seed_redii, thisPoolConf, false)

	redisPtr = &thisInstance

	return &thisInstance
}