在Golang中支持使用redis-json的管道。

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

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包?

谢谢 在Golang中支持使用redis-json的管道。

英文:

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 在Golang中支持使用redis-json的管道。

答案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 (
	&quot;context&quot;
	&quot;strconv&quot;

	&quot;github.com/rueian/rueidis&quot;
)

func main() {
	client, err := rueidis.NewClient(rueidis.ClientOption{InitAddress: []string{&quot;127.0.0.1:6379&quot;}})
	if err != nil {
		panic(err)
	}
	cmds := make(rueidis.Commands, 10000)
	for i := 0; i &lt; len(cmds); i++ {
		cmds[i] = client.B().JsonSet().Key(strconv.Itoa(i)).Path(&quot;$&quot;).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 &lt; 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(&quot;unexpected response&quot;)
		}
	}
}

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

huangapple
  • 本文由 发表于 2022年8月31日 23:07:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/73558001.html
匿名

发表评论

匿名网友

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

确定