rejonson

package module
v0.0.83 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2020 License: Apache-2.0 Imports: 2 Imported by: 0

README

Rejonson

Redis rejson extension built upon go-redis

Build Status Coverage Status

Table of Contents

  1. Quick start
  2. API
  3. Dependencies
  4. Testing
  5. License
  6. Contact

Quick start

Install
go get github.com/dxtrleague/rejonson
Import
import "github.com/dxtrleague/rejonson"
Extend Client

Extends go-redis client with all ReJSON abilities, so you can use directly the rejson client for all redis usage and commands.

// go redis client
goRedisClient := redis.NewClient(&redis.Options{
  Addr: "localhost:6379",
})

client := rejonson.ExtendClient(goRedisClient)
defer client.Close()

arr := []interface{}{"hello", "world", 1, map[string]interface{}{"key": 12}}
js, err := json.Marshal(arr)
if err != nil {
  // handle
}
// redis "native" command
client.Set("go-redis-cmd", "hello", time.Second)
// rejson command
client.JsonSet("rejson-cmd", ".", string(js))

// int command
arrLen, err := client.JsonArrLen("rejson-cmd", ".").Result()
if err != nil {
  // handle
}

fmt.Printf("Array length: %d", arrLen)
// Output: Array length: 4
Pipeline

Client will also return extended Pipeline and TXPipeline

goRedisClient := redis.NewClient(&redis.Options{
  Addr: "localhost:6379",
})

client := rejonson.ExtendClient(goRedisClient)

pipeline := client.Pipeline()
pipeline.JsonSet("rejson-cmd-pipeline", ".", "[10]")
pipeline.JsonNumMultBy("rejson-cmd-pipeline", "[0]", 10)
pipeline.Set("go-redis-pipeline-command", "hello from go-redis", time.Second)

_, err := pipeline.Exec()
if err != nil {
  // handle error
}
jsonString, err := client.JsonGet("rejson-cmd-pipeline").Result()
if err != nil {
  // handle error
}

fmt.Printf("Array %s", jsonString)

// Output: Array [100]

API

Rejonson implements all the methods as described at ReJson Commands except for JSON.DEBUG and JSON.RESP.

The args will be serialized to redis directly so make sure to read ReJSON command docs

All the rejson methods starts with the prefix of Json e.g JsonDel, JsonArrIndex, JsonMGet.
Each command returns specific go-redis.Cmder by the specific request.


Due to some ReJSON bug - #issue-76, some empty strings will be ignored.

Dependencies

Rejonson depends only on go-redis. The testing also depends on assert library.

Test

Rejonson tests must use real redis with ReJson to run

It is recommended to run the unit tests when using rejonson.
The unit tests will make sure your go-redis version is compatible and your rejson plugin supports all the methods and working as expected.

The testing library depends on assert library

License

Apache 2.0

Contact

For any question or contribution, feel free to open an issue.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*redis.Client
	// contains filtered or unexported fields
}

Client is an extended redis.Client, stores a pointer to the original redis.Client

func ExtendClient

func ExtendClient(client *redis.Client) *Client
Example
ctx := context.Background()
goRedisClient := redis.NewClient(&redis.Options{
	Addr: "localhost:6379",
})

client := rejonson.ExtendClient(goRedisClient)
defer client.Close()

arr := []interface{}{"hello", "world", 1, map[string]interface{}{"key": 12}}
js, err := json.Marshal(arr)
if err != nil {
	// handle
}
// redis "native" command
client.Set(ctx, "go-redis-cmd", "hello", time.Second)
client.JsonSet(ctx, "rejson-cmd", ".", string(js))

// int command
arrLen, err := client.JsonArrLen(ctx, "rejson-cmd", ".").Result()
if err != nil {
	// handle
}

fmt.Printf("Array length: %d", arrLen)
Output:

Array length: 4

func (Client) JsonArrAppend

func (cl Client) JsonArrAppend(c context.Context, key, path string, jsons ...interface{}) *redis.IntCmd

func (Client) JsonArrIndex

func (cl Client) JsonArrIndex(c context.Context, key, path string, jsonScalar interface{}, startAndStop ...interface{}) *redis.IntCmd

func (Client) JsonArrInsert

func (cl Client) JsonArrInsert(c context.Context, key, path string, index int, jsons ...interface{}) *redis.IntCmd

func (Client) JsonArrLen

func (cl Client) JsonArrLen(c context.Context, key, path string) *redis.IntCmd

func (Client) JsonArrPop

func (cl Client) JsonArrPop(c context.Context, key, path string, index int) *redis.StringCmd

func (Client) JsonArrTrim

func (cl Client) JsonArrTrim(c context.Context, key, path string, start, stop int) *redis.IntCmd

func (Client) JsonDel

func (cl Client) JsonDel(c context.Context, key, path string) *redis.IntCmd

JsonDel

returns intCmd -> deleted 1 or 0 read more: https://oss.redislabs.com/rejson/commands/#jsondel

func (Client) JsonGet

func (cl Client) JsonGet(c context.Context, key string, args ...interface{}) *redis.StringCmd

JsonGet

Possible args:

(Optional) INDENT + indent-string (Optional) NEWLINE + line-break-string (Optional) SPACE + space-string (Optional) NOESCAPE (Optional) path ...string

returns stringCmd -> the JSON string read more: https://oss.redislabs.com/rejson/commands/#jsonget

func (Client) JsonMGet

func (cl Client) JsonMGet(c context.Context, key string, args ...interface{}) *redis.StringSliceCmd

func (Client) JsonNumIncrBy

func (cl Client) JsonNumIncrBy(c context.Context, key, path string, num int) *redis.StringCmd

func (Client) JsonNumMultBy

func (cl Client) JsonNumMultBy(c context.Context, key, path string, num int) *redis.StringCmd

func (Client) JsonObjKeys

func (cl Client) JsonObjKeys(c context.Context, key, path string) *redis.StringSliceCmd

func (Client) JsonObjLen

func (cl Client) JsonObjLen(c context.Context, key, path string) *redis.IntCmd

func (Client) JsonSet

func (cl Client) JsonSet(c context.Context, key, path, json string, args ...interface{}) *redis.StatusCmd

jsonSet

Possible args: (Optional)

func (Client) JsonStrAppend

func (cl Client) JsonStrAppend(c context.Context, key, path, appendString string) *redis.IntCmd

func (Client) JsonStrLen

func (cl Client) JsonStrLen(c context.Context, key, path string) *redis.IntCmd

func (Client) JsonType

func (cl Client) JsonType(c context.Context, key, path string) *redis.StringCmd

func (*Client) Pipeline

func (cl *Client) Pipeline() *Pipeline

func (*Client) TXPipeline

func (cl *Client) TXPipeline() *Pipeline

type Pipeline

type Pipeline struct {
	redis.Pipeliner
	// contains filtered or unexported fields
}

Pipeline is an extended redis.Pipeline, stores a pointer to the original redis.Pipeliner

func ExtendPipeline

func ExtendPipeline(pipeline redis.Pipeliner) *Pipeline
Example
ctx := context.Background()
goRedisClient := redis.NewClient(&redis.Options{
	Addr: "localhost:6379",
})

client := rejonson.ExtendClient(goRedisClient)

pipeline := client.Pipeline()
pipeline.JsonSet(ctx, "rejson-cmd-pipeline", ".", "[10]")
pipeline.JsonNumMultBy(ctx, "rejson-cmd-pipeline", "[0]", 10)
pipeline.Set(ctx, "go-redis-pipeline-command", "hello from go-redis", time.Second)

_, err := pipeline.Exec()
if err != nil {
	// handle error
}
jsonString, err := client.JsonGet(ctx, "rejson-cmd-pipeline").Result()
if err != nil {
	// handle error
}

fmt.Printf("Array %s", jsonString)
Output:

Array [100]

func (Pipeline) JsonArrAppend

func (cl Pipeline) JsonArrAppend(c context.Context, key, path string, jsons ...interface{}) *redis.IntCmd

func (Pipeline) JsonArrIndex

func (cl Pipeline) JsonArrIndex(c context.Context, key, path string, jsonScalar interface{}, startAndStop ...interface{}) *redis.IntCmd

func (Pipeline) JsonArrInsert

func (cl Pipeline) JsonArrInsert(c context.Context, key, path string, index int, jsons ...interface{}) *redis.IntCmd

func (Pipeline) JsonArrLen

func (cl Pipeline) JsonArrLen(c context.Context, key, path string) *redis.IntCmd

func (Pipeline) JsonArrPop

func (cl Pipeline) JsonArrPop(c context.Context, key, path string, index int) *redis.StringCmd

func (Pipeline) JsonArrTrim

func (cl Pipeline) JsonArrTrim(c context.Context, key, path string, start, stop int) *redis.IntCmd

func (Pipeline) JsonDel

func (cl Pipeline) JsonDel(c context.Context, key, path string) *redis.IntCmd

JsonDel

returns intCmd -> deleted 1 or 0 read more: https://oss.redislabs.com/rejson/commands/#jsondel

func (Pipeline) JsonGet

func (cl Pipeline) JsonGet(c context.Context, key string, args ...interface{}) *redis.StringCmd

JsonGet

Possible args:

(Optional) INDENT + indent-string (Optional) NEWLINE + line-break-string (Optional) SPACE + space-string (Optional) NOESCAPE (Optional) path ...string

returns stringCmd -> the JSON string read more: https://oss.redislabs.com/rejson/commands/#jsonget

func (Pipeline) JsonMGet

func (cl Pipeline) JsonMGet(c context.Context, key string, args ...interface{}) *redis.StringSliceCmd

func (Pipeline) JsonNumIncrBy

func (cl Pipeline) JsonNumIncrBy(c context.Context, key, path string, num int) *redis.StringCmd

func (Pipeline) JsonNumMultBy

func (cl Pipeline) JsonNumMultBy(c context.Context, key, path string, num int) *redis.StringCmd

func (Pipeline) JsonObjKeys

func (cl Pipeline) JsonObjKeys(c context.Context, key, path string) *redis.StringSliceCmd

func (Pipeline) JsonObjLen

func (cl Pipeline) JsonObjLen(c context.Context, key, path string) *redis.IntCmd

func (Pipeline) JsonSet

func (cl Pipeline) JsonSet(c context.Context, key, path, json string, args ...interface{}) *redis.StatusCmd

jsonSet

Possible args: (Optional)

func (Pipeline) JsonStrAppend

func (cl Pipeline) JsonStrAppend(c context.Context, key, path, appendString string) *redis.IntCmd

func (Pipeline) JsonStrLen

func (cl Pipeline) JsonStrLen(c context.Context, key, path string) *redis.IntCmd

func (Pipeline) JsonType

func (cl Pipeline) JsonType(c context.Context, key, path string) *redis.StringCmd

func (*Pipeline) Pipeline

func (pl *Pipeline) Pipeline() *Pipeline

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL