Last active
May 16, 2023 14:11
-
-
Save knqyf263/9a67c7d2a11cd70034e1786b34daa3dc to your computer and use it in GitHub Desktop.
go-redis pipeline
This file contains 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
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 | |
} | |
} |
👍
👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍👍👍