同时插入到Redis

huangapple go评论109阅读模式
英文:

Concurrently inserting to Redis

问题

我有三个元素,分别由100万、200万和300万个整数组成。我想将它们同时插入到Redis中,以使它们的执行时间不超过300万个整数的执行时间。我尝试使用sync.WaitGroup,但它并没有加快执行速度。以下是我的基本代码。

  1. package main
  2. import (
  3. "log"
  4. "strconv"
  5. "time"
  6. "gopkg.in/redis.v5"
  7. )
  8. func main() {
  9. oneMillion := makeRange(1, 1000000)
  10. twoMillion := makeRange(1000001, 3000000)
  11. threeMillion := makeRange(3000001, 6000000)
  12. elements := [][]int{oneMillion, twoMillion, threeMillion}
  13. client := redis.NewClient(&redis.Options{
  14. Addr: "localhost:6379",
  15. Password: "",
  16. DB: 0,
  17. DialTimeout: 60 * time.Second,
  18. WriteTimeout: 60 * time.Second,
  19. ReadTimeout: 60 * time.Second,
  20. PoolTimeout: 60 * time.Second,
  21. })
  22. pipeline := client.Pipeline()
  23. for _, elem := range elements {
  24. for i := 0; i < len(elem); i++ {
  25. key := "KEY:" + strconv.Itoa(elem[i])
  26. val := "VAL:" + strconv.Itoa(elem[i])
  27. cmd := pipeline.Set(key, val, 0)
  28. if cmd.Err() != nil {
  29. log.Fatal("cmd error: ", cmd.Err())
  30. }
  31. }
  32. _, err := pipeline.Exec()
  33. if err != nil {
  34. log.Fatal("error: ", err)
  35. }
  36. }
  37. }
  38. func makeRange(min, max int) []int {
  39. a := make([]int, max-min+1)
  40. for i := range a {
  41. a[i] = min + i
  42. }
  43. return a
  44. }

请注意,这只是一个基本的代码示例,可能需要根据你的实际需求进行修改。

英文:

I have three elements consisting of 1 million, 2 million and 3 million ints respectively. I want to insert them to redis such that they all happen concurrently and the total execution time is not greater than the execution time of the 3 million ints. I tried using sync.Waitgroup but it doesn't speed up the execution. Here's my basic code.

  1. package main
  2. import (
  3. &quot;log&quot;
  4. &quot;strconv&quot;
  5. &quot;time&quot;
  6. &quot;gopkg.in/redis.v5&quot;
  7. )
  8. func main() {
  9. oneMillion := makeRange(1, 1000000)
  10. twoMillion := makeRange(1000001, 3000000)
  11. threeMillion := makeRange(3000001, 6000000)
  12. elements := [][]int{oneMillion, twoMillion, threeMillion}
  13. client := redis.NewClient(&amp;redis.Options{
  14. Addr: &quot;localhost:6379&quot;,
  15. Password: &quot;&quot;,
  16. DB: 0,
  17. DialTimeout: 60 * time.Second,
  18. WriteTimeout: 60 * time.Second,
  19. ReadTimeout: 60 * time.Second,
  20. PoolTimeout: 60 * time.Second,
  21. })
  22. pipeline := client.Pipeline()
  23. for _, elem := range elements {
  24. for i := 0; i &lt; len(elem); i++ {
  25. key := &quot;KEY:&quot; + strconv.Itoa(elem[i])
  26. val := &quot;VAL:&quot; + strconv.Itoa(elem[i])
  27. cmd := pipeline.Set(key, val, 0)
  28. if cmd.Err() != nil {
  29. log.Fatal(&quot;cmd error: &quot;, cmd.Err())
  30. }
  31. }
  32. _, err := pipeline.Exec()
  33. if err != nil {
  34. log.Fatal(&quot;error: &quot;, err)
  35. }
  36. }
  37. }
  38. func makeRange(min, max int) []int {
  39. a := make([]int, max-min+1)
  40. for i := range a {
  41. a[i] = min + i
  42. }
  43. return a
  44. }

答案1

得分: 0

大致上,Redis 上的每个操作都是原子的,也就是说,在任何给定时间只会有一个操作在 Redis 服务器上执行。因此,理论上,如果你想在 Redis 服务器上执行 300 万个设置操作,那么所有这些操作都将按顺序进行。

英文:

More or less every operation on redis is atomic , i.e only one operation will be executing on the redis server at any given time. So in theory if you want to do say 3 million set operations on a redis server all those operations will happen serially.

huangapple
  • 本文由 发表于 2017年5月14日 18:31:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/43962874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定