package redis import ( redisc "github.com/chasex/redis-go-cluster" "github.com/spf13/viper" "time" ) // redis-go-cluster适配器 type GoRedisClusterAdapter struct { cluster *redisc.Cluster } // 返回redis-go-cluster适配器新实例 func NewGoRedisClusterAdapter(nodes []string) (IRedisAdapter, error) { maxActive := viper.GetInt("redis.max_active") idleTimeout := viper.GetDuration("redis.timeout") cluster, err := redisc.NewCluster(&redisc.Options{ StartNodes: nodes, ConnTimeout: 5 * time.Second, KeepAlive: maxActive, AliveTime: idleTimeout, }) if nil != err { return nil, err } return &GoRedisClusterAdapter{ cluster: cluster, }, nil } // 关闭Redis连接 func (a *GoRedisClusterAdapter) Close() error { a.cluster.Close() return nil } // 执行Redis命令 func (a *GoRedisClusterAdapter) Do(commandName string, args ...interface{}) (interface{}, error) { return a.cluster.Do(commandName, args...) }