Skip to content

Instantly share code, notes, and snippets.

@knqyf263
Last active May 16, 2023 14:11
Show Gist options
  • Save knqyf263/9a67c7d2a11cd70034e1786b34daa3dc to your computer and use it in GitHub Desktop.
Save knqyf263/9a67c7d2a11cd70034e1786b34daa3dc to your computer and use it in GitHub Desktop.
go-redis pipeline
func TestRedis(t *testing.T) {
s, _ := testutil.PrepareTestRedis()
for i := 0; i < 10000; i++ {
s.Set("key"+strconv.Itoa(i), "hoge"+strconv.Itoa(i))
}
client := redis.NewClient(&redis.Options{Addr: s.Addr()})
// 普通にループ
result := map[string]string{}
for i := 0; i < 10000; i++ {
key := "key" + strconv.Itoa(i)
res, _ := client.Get(key).Result()
result[key] = res
}
// Pipelineを使ってループ
m := map[string]*redis.StringCmd{}
pipe := client.Pipeline()
for i := 0; i < 10000; i++ {
m["key"+strconv.Itoa(i)] = pipe.Get("key" + strconv.Itoa(i))
}
_, err := pipe.Exec()
if err != nil {
panic(err)
}
result2 := map[string]string{}
for k, v := range m {
res, _ := v.Result()
result2[k] = res
}
}
@alikarimii
Copy link

👍👍👍

@ryuryu08
Copy link

👍

@wrandowR
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment