Created
December 15, 2019 04:20
-
-
Save wangrenjun/2f57d5ae15fdc6ce5df4cf740b0ba14b to your computer and use it in GitHub Desktop.
Redis pipeline example with Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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