|
@@ -1,6 +1,7 @@
|
|
package redis
|
|
package redis
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "fmt"
|
|
redisc "github.com/chasex/redis-go-cluster"
|
|
redisc "github.com/chasex/redis-go-cluster"
|
|
"github.com/spf13/viper"
|
|
"github.com/spf13/viper"
|
|
"time"
|
|
"time"
|
|
@@ -40,3 +41,41 @@ func (a *GoRedisClusterAdapter) Close() error {
|
|
func (a *GoRedisClusterAdapter) Do(commandName string, args ...interface{}) (interface{}, error) {
|
|
func (a *GoRedisClusterAdapter) Do(commandName string, args ...interface{}) (interface{}, error) {
|
|
return a.cluster.Do(commandName, args...)
|
|
return a.cluster.Do(commandName, args...)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// 返回命令管道操作对象
|
|
|
|
+func (a *GoRedisClusterAdapter) Pipeline() IRedisPipeline {
|
|
|
|
+ return &GoRedisClusterPipeline{
|
|
|
|
+ cluster: a.cluster,
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Redigo命令管道操作对象
|
|
|
|
+type GoRedisClusterPipeline struct {
|
|
|
|
+ cluster *redisc.Cluster
|
|
|
|
+ commands []*redisCmd
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 向管道中添加命令
|
|
|
|
+func (p *GoRedisClusterPipeline) Send(commandName string, args ...interface{}) IRedisPipeline {
|
|
|
|
+ p.commands = append(p.commands, &redisCmd{commandName: commandName, args: args})
|
|
|
|
+ return p
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 执行管道中的命令
|
|
|
|
+func (p *GoRedisClusterPipeline) Execute() ([]interface{}, error) {
|
|
|
|
+ if nil == p.commands || len(p.commands) == 0 {
|
|
|
|
+ return nil, fmt.Errorf("no commands in pipeline")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ batch := p.cluster.NewBatch()
|
|
|
|
+
|
|
|
|
+ var err error
|
|
|
|
+ for i := 0; i < len(p.commands); i++ {
|
|
|
|
+ cmd := p.commands[i]
|
|
|
|
+ err = batch.Put(cmd.commandName, cmd.args...)
|
|
|
|
+ if nil != err {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return p.cluster.RunBatch(batch)
|
|
|
|
+}
|