Skip to content

Commit e0853ab

Browse files
Added batch process method to the pipeline (#3510)
* Added batch process method to the pipeline * Added Process and BatchProcess tests * Fix test matching
1 parent 65e1c22 commit e0853ab

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

pipeline.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ type Pipeliner interface {
3030
// If a certain Redis command is not yet supported, you can use Do to execute it.
3131
Do(ctx context.Context, args ...interface{}) *Cmd
3232

33-
// Process puts the commands to be executed into the pipeline buffer.
33+
// Process queues the cmd for later execution.
3434
Process(ctx context.Context, cmd Cmder) error
3535

36+
// BatchProcess adds multiple commands to be executed into the pipeline buffer.
37+
BatchProcess(ctx context.Context, cmd ...Cmder) error
38+
3639
// Discard discards all commands in the pipeline buffer that have not yet been executed.
3740
Discard()
3841

@@ -79,7 +82,12 @@ func (c *Pipeline) Do(ctx context.Context, args ...interface{}) *Cmd {
7982

8083
// Process queues the cmd for later execution.
8184
func (c *Pipeline) Process(ctx context.Context, cmd Cmder) error {
82-
c.cmds = append(c.cmds, cmd)
85+
return c.BatchProcess(ctx, cmd)
86+
}
87+
88+
// BatchProcess queues multiple cmds for later execution.
89+
func (c *Pipeline) BatchProcess(ctx context.Context, cmd ...Cmder) error {
90+
c.cmds = append(c.cmds, cmd...)
8391
return nil
8492
}
8593

pipeline_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,25 @@ var _ = Describe("pipelining", func() {
114114
err := pipe.Do(ctx).Err()
115115
Expect(err).To(Equal(errors.New("redis: please enter the command to be executed")))
116116
})
117+
118+
It("should process", func() {
119+
err := pipe.Process(ctx, redis.NewCmd(ctx, "asking"))
120+
Expect(err).To(BeNil())
121+
Expect(pipe.Cmds()).To(HaveLen(1))
122+
})
123+
124+
It("should batchProcess", func() {
125+
err := pipe.BatchProcess(ctx, redis.NewCmd(ctx, "asking"))
126+
Expect(err).To(BeNil())
127+
Expect(pipe.Cmds()).To(HaveLen(1))
128+
129+
pipe.Discard()
130+
Expect(pipe.Cmds()).To(HaveLen(0))
131+
132+
err = pipe.BatchProcess(ctx, redis.NewCmd(ctx, "asking"), redis.NewCmd(ctx, "set", "key", "value"))
133+
Expect(err).To(BeNil())
134+
Expect(pipe.Cmds()).To(HaveLen(2))
135+
})
117136
}
118137

119138
Describe("Pipeline", func() {

0 commit comments

Comments
 (0)