package redis import ( "github.com/gomodule/redigo/redis" "github.com/mna/redisc" "time" ) // redisc适配器 type RediscAdapter struct { cluster *redisc.Cluster } // 返回redisc适配器新实例 func NewRediscAdapter(nodes []string) (IRedisAdapter, error) { opts := []redis.DialOption{ redis.DialConnectTimeout(5 * time.Second), //redis.DialReadTimeout(5 * time.Second), //redis.DialWriteTimeout(5 * time.Second), } cluster := &redisc.Cluster{ StartupNodes: nodes, DialOptions: opts, CreatePool: createPool, } return &RediscAdapter{ cluster: cluster, }, nil } // 关闭Redis连接 func (a *RediscAdapter) Close() error { return a.cluster.Close() } // 执行Redis命令 func (a *RediscAdapter) Do(commandName string, args ...interface{}) (interface{}, error) { conn := a.cluster.Get() defer func(conn redis.Conn) { _ = conn.Close() }(conn) if nil == conn { return nil, ErrRedisConnNil } else { return conn.Do(commandName, args...) } } // 返回命令管道操作对象 func (a *RediscAdapter) Pipeline() IRedisPipeline { return &RedigoPipeline{ // redisc库兼容redigo库,返回RedigoPipeline即可 conn: a.cluster.Get(), } }