Example #1
0
func NewRedisClient(config *Config) (*RedisClient, error) {
	var client *redis.Client

	if config.RedisSentinel {
		client = redis.NewFailoverClient(&redis.FailoverOptions{
			MasterName:    config.RedisMasterName,
			SentinelAddrs: []string{config.RedisAddr},
			Password:      config.RedisPassword,
			DB:            config.RedisDB,
		})
	} else {
		client = redis.NewClient(&redis.Options{
			Addr:     config.RedisAddr,
			Password: config.RedisPassword,
			DB:       config.RedisDB,
		})
	}
	ps, err := client.Subscribe(channelName)

	if err != nil {
		return nil, err
	}

	rc := &RedisClient{
		client: client,
		pubsub: ps,
	}
	go rc.Receive()
	return rc, nil
}
Example #2
0
func redisNewClient() RedisClient {
	var client RedisClient

	switch Config.Redis.Method {
	case "standalone":
		client = redis.NewClient(&redis.Options{
			Addr:     Config.Redis.Host[0],
			Password: "",
			DB:       0,
		})
	case "sentinel":
		client = redis.NewFailoverClient(&redis.FailoverOptions{
			MasterName:    "master",
			SentinelAddrs: Config.Redis.Host,
		})
	default:
		Log.Fatal("Unknown redis connection method specified")
	}

	return client
}
Example #3
0
func NewRedisClient(config *Config) *RedisClient {
	var client *redis.Client
	if config.RedisSentinel {
		client = redis.NewFailoverClient(&redis.FailoverOptions{
			MasterName:    config.RedisMasterName,
			SentinelAddrs: []string{config.RedisAddr},
			Password:      config.RedisPassword,
			DB:            int64(config.RedisDB),
		})
	} else {
		client = redis.NewClient(&redis.Options{
			Addr:     config.RedisAddr,
			Password: config.RedisPassword,
			DB:       int64(config.RedisDB),
		})
	}

	logrus.Infof("Redis client created for addr='%s'", config.RedisAddr)
	return &RedisClient{
		client: client,
	}
}