英文:
Support for pipeline with redis-json in Golang
问题
我们最近开始在Golang中使用redis-json(github.com/nitishm/go-rejson/v4)和redis-search(github.com/RediSearch/redisearch-go/redisearch)客户端。
我们需要支持批量插入JSON对象,而且我们不想使用事务。是否有一种方法可以在redis-json中实现管道(我们想要将一系列的json.set操作进行管道化)?或者是否有人可以向我推荐一个支持这种类型管道的Golang包?
谢谢
英文:
We recently started to work with redis-json (github.com/nitishm/go-rejson/v4) and redis-search (github.com/RediSearch/redisearch-go/redisearch) clients in Golang.
We need to support bulk insert operations of json objects and we don't want to use transactions.
Is there a way to implement a pipeline with redis-json (we want to pipeline a bunch of json.set operations)? or if someone can refer me to a golang package which does support this kind of pipeline?
Thank you
答案1
得分: 2
我是https://github.com/rueian/rueidis的作者。
以下是使用rueidis进行批量插入和批量读取的示例代码:
package main
import (
"context"
"strconv"
"github.com/rueian/rueidis"
)
func main() {
client, err := rueidis.NewClient(rueidis.ClientOption{InitAddress: []string{"127.0.0.1:6379"}})
if err != nil {
panic(err)
}
cmds := make(rueidis.Commands, 10000)
for i := 0; i < len(cmds); i++ {
cmds[i] = client.B().JsonSet().Key(strconv.Itoa(i)).Path("$").Value(rueidis.JSON(i)).Build()
}
for _, resp := range client.DoMulti(context.Background(), cmds...) { // 批量插入
if err := resp.Error(); err != nil {
panic(err)
}
}
for i := 0; i < len(cmds); i++ {
cmds[i] = client.B().JsonGet().Key(strconv.Itoa(i)).Build()
}
for i, resp := range client.DoMulti(context.Background(), cmds...) { // 批量读取
if v, err := resp.AsInt64(); err != nil || v != int64(i) {
panic("unexpected response")
}
}
}
希望对你有帮助!
英文:
I am the author of https://github.com/rueian/rueidis.
Here is an example of how to use rueidis for bulk insert and bulk read:
package main
import (
"context"
"strconv"
"github.com/rueian/rueidis"
)
func main() {
client, err := rueidis.NewClient(rueidis.ClientOption{InitAddress: []string{"127.0.0.1:6379"}})
if err != nil {
panic(err)
}
cmds := make(rueidis.Commands, 10000)
for i := 0; i < len(cmds); i++ {
cmds[i] = client.B().JsonSet().Key(strconv.Itoa(i)).Path("$").Value(rueidis.JSON(i)).Build()
}
for _, resp := range client.DoMulti(context.Background(), cmds...) { // bulk insert
if err := resp.Error(); err != nil {
panic(err)
}
}
for i := 0; i < len(cmds); i++ {
cmds[i] = client.B().JsonGet().Key(strconv.Itoa(i)).Build()
}
for i, resp := range client.DoMulti(context.Background(), cmds...) { // bulk read
if v, err := resp.AsInt64(); err != nil || v != int64(i) {
panic("unexpected response")
}
}
}
答案2
得分: 0
在Redis文档中,针对每种编程语言都有推荐的客户端。
https://redis.io/docs/stack/json/clients/
rueian/rueidis支持流水线操作
https://github.com/rueian/rueidis/blob/master/pipe.go
编辑:
https://github.com/gomodule/redigo <-- star 数更多,似乎被推荐使用。
英文:
In the redis documentation itself there are recommended clients specific to each language.
https://redis.io/docs/stack/json/clients/
rueian/rueidis has the support for Pipelining
https://github.com/rueian/rueidis/blob/master/pipe.go
Edit :
https://github.com/gomodule/redigo <-- has more stars and seems to be recommended
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论