Skip to content

Commit

Permalink
Add an example in Readme's Disable Auto Pipelining section, make some…
Browse files Browse the repository at this point in the history
… improvement in coding style
  • Loading branch information
zhaohuiliu committed Feb 22, 2025
1 parent e1cfe0a commit 3b0a049
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ for _, resp := range client.DoMulti(ctx, cmds...) {
}
```

### Assign Commands to Auto Pipelining
You can also assign commands to auto pipelining with `ToPipe()`

For single command
``` golang
cmd := client.B().Get().Key("key").Build().ToPipe()
client.Do(ctx, cmd)
```

For multiple commands
``` golang
cmds := make(rueidis.Commands, 0, 10)
for i := 0; i < 10; i++ {
cmds = append(cmds, client.B().Set().Key("key").Value("value").Build())
}
cmds[0] = cmds[0].ToPipe() // All cmds will go to auto pipelining
client.DoMulti(ctx, cmds...)
```

## [Server-Assisted Client-Side Caching](https://redis.io/docs/manual/client-side-caching/)

The opt-in mode of [server-assisted client-side caching](https://redis.io/docs/manual/client-side-caching/) is enabled by default and can be used by calling `DoCache()` or `DoMultiCache()` with client-side TTLs specified.
Expand Down
6 changes: 3 additions & 3 deletions internal/cmds/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const (
mtGetTag = uint16(1<<11) | readonly // make mtGetTag can also be retried
scrRoTag = uint16(1<<10) | readonly // make scrRoTag can also be retried
unsubTag = uint16(1<<9) | noRetTag
pipeTag = uint16(1 << 8) // make blocking mode request can use auto pipelining
pipeTag = uint16(1<<8) // make blocking mode request can use auto pipelining
// InitSlot indicates that the command be sent to any redis node in cluster
InitSlot = uint16(1 << 14)
// NoSlot indicates that the command has no key slot specified
Expand Down Expand Up @@ -109,7 +109,7 @@ func (c Completed) Pin() Completed {
return c
}

// ToPipe marks the command with pipeTag
// ToPipe returns a new command with pipeTag
func (c Completed) ToPipe() Completed {
c.cf |= pipeTag
return c
Expand Down Expand Up @@ -152,7 +152,7 @@ func (c *Completed) IsWrite() bool {

// IsPipe checks if it is set pipeTag which prefers auto pipelining
func (c *Completed) IsPipe() bool {
return c.cf&pipeTag == pipeTag
return c.NoReply() || c.cf&pipeTag == pipeTag
}

// Commands returns the commands as []string.
Expand Down
4 changes: 2 additions & 2 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (m *mux) DoMultiStream(ctx context.Context, multi ...Completed) MultiRedisR
}

func (m *mux) Do(ctx context.Context, cmd Completed) (resp RedisResult) {
if m.usePool && !cmd.NoReply() && !cmd.IsPipe() {
if m.usePool && !cmd.IsPipe() {
resp = m.blocking(m.spool, ctx, cmd)
} else if cmd.IsBlock() {
resp = m.blocking(m.dpool, ctx, cmd)
Expand All @@ -222,7 +222,7 @@ func (m *mux) Do(ctx context.Context, cmd Completed) (resp RedisResult) {

func (m *mux) DoMulti(ctx context.Context, multi ...Completed) (resp *redisresults) {
for _, cmd := range multi {
if cmd.NoReply() || cmd.IsPipe() {
if cmd.IsPipe() {
return m.pipelineMulti(ctx, multi)
}
if cmd.IsBlock() {
Expand Down
2 changes: 1 addition & 1 deletion redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func testMultiSETGET(t *testing.T, client Client, csc bool) {
commands = append(commands, client.B().Get().Key(cmdkeys[len(cmdkeys)-1]).Build())
}
if i%10 == 0 {
commands[0].ToPipe()
commands[0] = commands[0].ToPipe()
}
jobs <- func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*10)
Expand Down

0 comments on commit 3b0a049

Please sign in to comment.