英文:
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 (
"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
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论