同时插入到Redis

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

Concurrently inserting to Redis

问题

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

package main

import (
    "log"
    "strconv"
    "time"

    "gopkg.in/redis.v5"
)

func main() {
    oneMillion := makeRange(1, 1000000)
    twoMillion := makeRange(1000001, 3000000)
    threeMillion := makeRange(3000001, 6000000)
    elements := [][]int{oneMillion, twoMillion, threeMillion}
    client := redis.NewClient(&redis.Options{
        Addr:         "localhost:6379",
        Password:     "",
        DB:           0,
        DialTimeout:  60 * time.Second,
        WriteTimeout: 60 * time.Second,
        ReadTimeout:  60 * time.Second,
        PoolTimeout:  60 * time.Second,
    })
    pipeline := client.Pipeline()
    for _, elem := range elements {
        for i := 0; i < len(elem); i++ {
            key := "KEY:" + strconv.Itoa(elem[i])
            val := "VAL:" + strconv.Itoa(elem[i])
            cmd := pipeline.Set(key, val, 0)
            if cmd.Err() != nil {
                log.Fatal("cmd error: ", cmd.Err())
            }
        }
        _, err := pipeline.Exec()
        if err != nil {
            log.Fatal("error: ", err)
        }
    }
}

func makeRange(min, max int) []int {
    a := make([]int, max-min+1)
    for i := range a {
        a[i] = min + i
    }
    return a
}

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

英文:

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.

package main
import (
&quot;log&quot;
&quot;strconv&quot;
&quot;time&quot;
&quot;gopkg.in/redis.v5&quot;
)
func main() {
oneMillion := makeRange(1, 1000000)
twoMillion := makeRange(1000001, 3000000)
threeMillion := makeRange(3000001, 6000000)
elements := [][]int{oneMillion, twoMillion, threeMillion}
client := redis.NewClient(&amp;redis.Options{
Addr:         &quot;localhost:6379&quot;,
Password:     &quot;&quot;,
DB:           0,
DialTimeout:  60 * time.Second,
WriteTimeout: 60 * time.Second,
ReadTimeout:  60 * time.Second,
PoolTimeout:  60 * time.Second,
})
pipeline := client.Pipeline()
for _, elem := range elements {
for i := 0; i &lt; len(elem); i++ {
key := &quot;KEY:&quot; + strconv.Itoa(elem[i])
val := &quot;VAL:&quot; + strconv.Itoa(elem[i])
cmd := pipeline.Set(key, val, 0)
if cmd.Err() != nil {
log.Fatal(&quot;cmd error: &quot;, cmd.Err())
}
}
_, err := pipeline.Exec()
if err != nil {
log.Fatal(&quot;error: &quot;, err)
}
}
}
func makeRange(min, max int) []int {
a := make([]int, max-min+1)
for i := range a {
a[i] = min + i
}
return a
}

答案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:

确定