Skip to content

Instantly share code, notes, and snippets.

@wangrenjun
Created December 15, 2019 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangrenjun/2f57d5ae15fdc6ce5df4cf740b0ba14b to your computer and use it in GitHub Desktop.
Save wangrenjun/2f57d5ae15fdc6ce5df4cf740b0ba14b to your computer and use it in GitHub Desktop.
Redis pipeline example with Go
package main
import (
"fmt"
"strconv"
"github.com/go-redis/redis/v7"
)
func main() {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
pipe := client.Pipeline()
for i := 0; i < 10; i++ {
pipe.Set("testkey:" + strconv.Itoa(i), strconv.Itoa(i), 0)
}
_, err := pipe.Exec()
if err != nil {
panic(err)
}
cmds := map[string]*redis.StringCmd{}
for i := 0; i < 10; i++ {
key := "testkey:" + strconv.Itoa(i)
cmds[key] = pipe.Get(key)
}
for i := 0; i < 10; i++ {
key := "testkey:" + strconv.Itoa(i)
err := pipe.Del(key).Err()
if err != nil {
panic(err)
}
}
_, err = pipe.Exec()
if err != nil {
panic(err)
}
for k, v := range cmds {
val, err := v.Result()
if err != nil {
panic(err)
}
fmt.Printf(" %s %s\n", k, val)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment