Support for pipeline with redis-json in Golang

Viewed 41

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 :)

2 Answers

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")
        }
    }
}
Related