redis json 마이그레이션 완료
This commit is contained in:
@ -7,7 +7,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/nitishm/go-rejson/v4/rjs"
|
||||
"repositories.action2quare.com/ayo/gocommon"
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
"repositories.action2quare.com/ayo/gocommon/wshandler"
|
||||
@ -50,7 +49,7 @@ type groupDoc struct {
|
||||
Members map[string]*memberDoc `json:"members"`
|
||||
InCharge string `json:"incharge"`
|
||||
|
||||
rh *RejsonHandler
|
||||
rh *RedisonHandler
|
||||
id groupID
|
||||
idhex string
|
||||
}
|
||||
@ -134,7 +133,7 @@ func (gd *groupDoc) addMember(mid accountID, doc bson.M) (*memberDoc, error) {
|
||||
Body: doc,
|
||||
}
|
||||
|
||||
if _, err := gd.rh.JSONSet(gd.strid(), "$.members."+gd.tid(mid), memdoc, rjs.SetOptionXX); err != nil {
|
||||
if _, err := gd.rh.JSONSet(gd.strid(), "$.members."+gd.tid(mid), memdoc, SetOptionXX); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -151,7 +150,7 @@ type groupInMemory struct {
|
||||
sendUpstreamMessage func(*wshandler.UpstreamMessage)
|
||||
sendEnterRoomMessage func(groupID, accountID)
|
||||
sendLeaveRoomMessage func(groupID, accountID)
|
||||
rh *RejsonHandler
|
||||
rh *RedisonHandler
|
||||
}
|
||||
|
||||
func (gm *groupInMemory) createGroup(newid groupID, charge accountID, chargeDoc bson.M) (*groupDoc, error) {
|
||||
@ -172,7 +171,7 @@ func (gm *groupInMemory) createGroup(newid groupID, charge accountID, chargeDoc
|
||||
id: newid,
|
||||
}
|
||||
|
||||
_, err := gm.rh.JSONSet(gd.strid(), "$", gd, rjs.SetOptionNX)
|
||||
_, err := gm.rh.JSONSet(gd.strid(), "$", gd, SetOptionNX)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -367,7 +366,7 @@ func (gm *groupInMemory) UpdateMemberDocument(gid groupID, mid accountID, doc bs
|
||||
id: gid,
|
||||
rh: gm.rh,
|
||||
}
|
||||
_, err := gm.rh.JSONSet(gd.strid(), "$.members."+gd.tid(mid)+".body", doc, rjs.SetOptionXX)
|
||||
_, err := gm.rh.JSONSet(gd.strid(), "$.members."+gd.tid(mid)+".body", doc, SetOptionXX)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -392,7 +391,7 @@ func (gm *groupInMemory) UpdateGroupDocument(gid groupID, body []byte) error {
|
||||
id: gid,
|
||||
rh: gm.rh,
|
||||
}
|
||||
_, err := gm.rh.JSONSet(gd.strid(), "$.members.body", body, rjs.SetOptionXX)
|
||||
_, err := gm.rh.JSONSet(gd.strid(), "$.members.body", body, SetOptionXX)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -410,7 +409,7 @@ func (cfg *groupConfig) prepareInMemory(ctx context.Context, typename string, su
|
||||
// 각 함수에서는 publish
|
||||
gm := &groupInMemory{
|
||||
groupConfig: cfg,
|
||||
rh: NewReJSONHandler(ctx, redisClient),
|
||||
rh: NewRedisonHandler(ctx, redisClient),
|
||||
sendUpstreamMessage: func(msg *wshandler.UpstreamMessage) {
|
||||
wsh.SendUpstreamMessage(region, msg)
|
||||
},
|
||||
|
||||
361
core/redison_handler.go
Normal file
361
core/redison_handler.go
Normal file
@ -0,0 +1,361 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
type SetOption string
|
||||
type GetOption = [2]any
|
||||
|
||||
const (
|
||||
// JSONSET command Options
|
||||
SetOptionNX SetOption = "NX"
|
||||
SetOptionXX SetOption = "XX"
|
||||
)
|
||||
|
||||
var (
|
||||
GetOptionSPACE = GetOption{"SPACE", " "}
|
||||
GetOptionINDENT = GetOption{"INDENT", "\t"}
|
||||
GetOptionNEWLINE = GetOption{"NEWLINE", "\n"}
|
||||
GetOptionNOESCAPE = GetOption{"NOESCAPE", ""}
|
||||
)
|
||||
|
||||
// gocommon으로 옮길 거
|
||||
type RedisonHandler struct {
|
||||
*redis.Client
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewRedisonHandler(ctx context.Context, redisClient *redis.Client) *RedisonHandler {
|
||||
return &RedisonHandler{
|
||||
Client: redisClient,
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func respToArray[T any](resp any, err error) ([]T, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resArr := resp.([]any)
|
||||
v := make([]T, len(resArr))
|
||||
for i, e := range resArr {
|
||||
v[i] = e.(T)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func appendArgs[T any](args []any, ext ...T) []any {
|
||||
for _, e := range ext {
|
||||
args = append(args, e)
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONMSet(key string, kv map[string]any) error {
|
||||
pl := rh.Pipeline()
|
||||
for path, obj := range kv {
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pl.Do(rh.ctx, "JSON.SET", key, path, b)
|
||||
}
|
||||
|
||||
cmders, err := pl.Exec(rh.ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, cmder := range cmders {
|
||||
if cmder.Err() != nil {
|
||||
return cmder.Err()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONSet(key, path string, obj any, opts ...SetOption) (bool, error) {
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
args := []any{
|
||||
"JSON.SET",
|
||||
key,
|
||||
path,
|
||||
b,
|
||||
}
|
||||
if len(opts) > 0 {
|
||||
args = append(args, opts[0])
|
||||
}
|
||||
|
||||
res, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return res.(string) == "OK", nil
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONGet(key, path string, opts ...GetOption) (res any, err error) {
|
||||
args := appendArgs[string]([]any{
|
||||
"JSON.GET",
|
||||
key,
|
||||
}, strings.Split(path, " ")...)
|
||||
|
||||
for _, opt := range opts {
|
||||
args = append(args, opt[:]...)
|
||||
}
|
||||
|
||||
return rh.Do(rh.ctx, args...).Result()
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONGetString(key, path string) ([]string, error) {
|
||||
return respToArray[string](rh.JSONResp(key, path))
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONGetInt64(key, path string) ([]int64, error) {
|
||||
return respToArray[int64](rh.JSONResp(key, path))
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONMGet(path string, keys ...string) (res any, err error) {
|
||||
args := appendArgs[string]([]any{
|
||||
"JSON.MGET",
|
||||
path,
|
||||
}, keys...)
|
||||
return rh.Do(rh.ctx, args...).Result()
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONMDel(key string, paths []string) error {
|
||||
pl := rh.Pipeline()
|
||||
for _, path := range paths {
|
||||
args := []any{
|
||||
"JSON.DEL",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
pl.Do(rh.ctx, args...)
|
||||
}
|
||||
_, err := pl.Exec(rh.ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONDel(key, path string) (int64, error) {
|
||||
args := []any{
|
||||
"JSON.DEL",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return resp.(int64), nil
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONType(key, path string) ([]string, error) {
|
||||
args := []any{
|
||||
"JSON.TYPE",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
return respToArray[string](rh.Do(rh.ctx, args...).Result())
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONNumIncrBy(key, path string, number int) ([]any, error) {
|
||||
args := []any{
|
||||
"JSON.NUMINCRBY",
|
||||
key,
|
||||
path,
|
||||
number,
|
||||
}
|
||||
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp.([]any), nil
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONNumMultBy(key, path string, number int) (res any, err error) {
|
||||
args := []any{
|
||||
"JSON.NUMMULTBY",
|
||||
key,
|
||||
path,
|
||||
number,
|
||||
}
|
||||
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp.([]any), nil
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONStrAppend(key, path string, jsonstring string) ([]int64, error) {
|
||||
args := []any{
|
||||
"JSON.STRAPPEND",
|
||||
key,
|
||||
path,
|
||||
fmt.Sprintf(`'"%s"'`, jsonstring),
|
||||
}
|
||||
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONStrLen(key, path string) (res []int64, err error) {
|
||||
args := []any{
|
||||
"JSON.STRLEN",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONArrAppend(key, path string, values ...any) (int64, error) {
|
||||
args := []any{
|
||||
"JSON.ARRAPPEND",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return resp.(int64), nil
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONArrLen(key, path string) ([]int64, error) {
|
||||
args := []any{
|
||||
"JSON.ARRLEN",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONArrPop(key, path string, index int) (res any, err error) {
|
||||
args := []any{
|
||||
"JSON.ARRPOP",
|
||||
key,
|
||||
path,
|
||||
index,
|
||||
}
|
||||
resp, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.([]any)[0], nil
|
||||
}
|
||||
|
||||
func appendValues(args []any, values ...any) []any {
|
||||
for _, jsonValue := range values {
|
||||
switch jsonValue := jsonValue.(type) {
|
||||
case string:
|
||||
args = append(args, fmt.Sprintf(`'"%s"'`, jsonValue))
|
||||
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
|
||||
args = append(args, jsonValue)
|
||||
default:
|
||||
bt, _ := json.Marshal(jsonValue)
|
||||
args = append(args, bt)
|
||||
}
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONArrIndex(key, path string, jsonValue any, optionalRange ...int) ([]int64, error) {
|
||||
args := appendValues([]any{
|
||||
"JSON.ARRINDEX",
|
||||
key,
|
||||
path,
|
||||
}, jsonValue)
|
||||
|
||||
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONArrTrim(key, path string, start, end int) (res any, err error) {
|
||||
args := []any{
|
||||
"JSON.ARRTRIM",
|
||||
key,
|
||||
path,
|
||||
start,
|
||||
end,
|
||||
}
|
||||
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONArrInsert(key, path string, index int, values ...any) (res any, err error) {
|
||||
args := appendValues([]any{
|
||||
"JSON.ARRINSERT",
|
||||
key,
|
||||
path,
|
||||
index,
|
||||
}, values...)
|
||||
|
||||
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONObjKeys(key, path string) ([]string, error) {
|
||||
args := []any{
|
||||
"JSON.OBJKEYS",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
|
||||
res, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resArr := res.([]any)
|
||||
resArr = resArr[0].([]any)
|
||||
slc := make([]string, len(resArr))
|
||||
|
||||
for i, r := range resArr {
|
||||
slc[i] = r.(string)
|
||||
}
|
||||
|
||||
return slc, nil
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONObjLen(key, path string) ([]int64, error) {
|
||||
args := []any{
|
||||
"JSON.OBJLEN",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
|
||||
return respToArray[int64](rh.Do(rh.ctx, args...).Result())
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONDebug(key, path string) (res any, err error) {
|
||||
args := []any{
|
||||
"JSON.DEBUG",
|
||||
"MEMORY",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
return rh.Do(rh.ctx, args...).Result()
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONForget(key, path string) (int64, error) {
|
||||
return rh.JSONDel(key, path)
|
||||
}
|
||||
|
||||
func (rh *RedisonHandler) JSONResp(key, path string) (res any, err error) {
|
||||
args := []any{
|
||||
"JSON.RESP",
|
||||
key,
|
||||
path,
|
||||
}
|
||||
return rh.Do(rh.ctx, args...).Result()
|
||||
}
|
||||
@ -1,210 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/nitishm/go-rejson/v4"
|
||||
"github.com/nitishm/go-rejson/v4/rjs"
|
||||
)
|
||||
|
||||
// gocommon으로 옮길 거
|
||||
type RejsonHandler struct {
|
||||
*redis.Client
|
||||
inner *rejson.Handler
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewReJSONHandler(ctx context.Context, redisClient *redis.Client) *RejsonHandler {
|
||||
inner := rejson.NewReJSONHandler()
|
||||
inner.SetGoRedisClientWithContext(ctx, redisClient)
|
||||
return &RejsonHandler{
|
||||
Client: redisClient,
|
||||
inner: inner,
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func respToArray[T any](resp any) []T {
|
||||
resArr := resp.([]any)
|
||||
v := make([]T, len(resArr))
|
||||
for i, e := range resArr {
|
||||
v[i] = e.(T)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONMSet(key string, kv map[string]any) error {
|
||||
pl := rh.Pipeline()
|
||||
for path, obj := range kv {
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pl.Do(rh.ctx, []any{"JSON.SET"}, key, path, b)
|
||||
}
|
||||
|
||||
cmders, err := pl.Exec(rh.ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, cmder := range cmders {
|
||||
if cmder.Err() != nil {
|
||||
return cmder.Err()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONSet(key, path string, obj any, opts ...rjs.SetOption) (res interface{}, err error) {
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
args := []any{
|
||||
[]any{"JSON.SET"},
|
||||
key,
|
||||
path,
|
||||
b,
|
||||
}
|
||||
if len(opts) > 0 {
|
||||
args = append(args, opts[0])
|
||||
}
|
||||
|
||||
return rh.Do(rh.ctx, args...).Result()
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONGet(key, path string, opts ...rjs.GetOption) (res interface{}, err error) {
|
||||
return rh.inner.JSONGet(key, path, opts...)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONGetString(key, path string) ([]string, error) {
|
||||
res, err := rh.inner.JSONResp(key, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return respToArray[string](res), nil
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONGetInt64(key, path string) ([]int64, error) {
|
||||
res, err := rh.inner.JSONResp(key, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return respToArray[int64](res), nil
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONMGet(path string, keys ...string) (res interface{}, err error) {
|
||||
return rh.inner.JSONMGet(path, keys...)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONMDel(key string, paths []string) error {
|
||||
pl := rh.Pipeline()
|
||||
for _, path := range paths {
|
||||
name, args, err := rjs.CommandBuilder(rjs.ReJSONCommandDEL, key, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args = append([]interface{}{name}, args...)
|
||||
pl.Do(rh.ctx, args...)
|
||||
}
|
||||
_, err := pl.Exec(rh.ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONDel(key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONDel(key, path)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONType(key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONType(key, path)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONNumIncrBy(key, path string, number int) (res interface{}, err error) {
|
||||
return rh.inner.JSONNumIncrBy(key, path, number)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONNumMultBy(key, path string, number int) (res interface{}, err error) {
|
||||
return rh.inner.JSONNumMultBy(key, path, number)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONStrAppend(key, path string, jsonstring string) (res interface{}, err error) {
|
||||
return rh.inner.JSONStrAppend(key, path, jsonstring)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONStrLen(key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONStrLen(key, path)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONArrAppend(key, path string, values ...interface{}) (res interface{}, err error) {
|
||||
return rh.inner.JSONArrAppend(key, path, values...)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONArrLen(key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONArrLen(key, path)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONArrPop(key, path string, index int) (res interface{}, err error) {
|
||||
return rh.inner.JSONArrPop(key, path, index)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONArrIndex(key, path string, jsonValue interface{}, optionalRange ...int) (res interface{}, err error) {
|
||||
return rh.inner.JSONArrIndex(key, path, jsonValue, optionalRange...)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONArrTrim(key, path string, start, end int) (res interface{}, err error) {
|
||||
return rh.inner.JSONArrTrim(key, path, start, end)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONArrInsert(key, path string, index int, values ...interface{}) (res interface{}, err error) {
|
||||
return rh.inner.JSONArrInsert(key, path, index, values...)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONObjKeys(key, path string) ([]string, error) {
|
||||
name, args, err := rjs.CommandBuilder(rjs.ReJSONCommandOBJKEYS, key, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
args = append([]interface{}{name}, args...)
|
||||
res, err := rh.Do(rh.ctx, args...).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resArr := res.([]any)
|
||||
resArr = resArr[0].([]any)
|
||||
slc := make([]string, len(resArr))
|
||||
|
||||
for i, r := range resArr {
|
||||
slc[i] = r.(string)
|
||||
}
|
||||
|
||||
return slc, nil
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONObjLen(key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONObjLen(key, path)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONObjLenInt64(key, path string) (int64, error) {
|
||||
res, err := rh.inner.JSONObjLen(key, path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.([]any)[0].(int64), nil
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONDebug(subCmd rjs.DebugSubCommand, key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONDebug(subCmd, key, path)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONForget(key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONForget(key, path)
|
||||
}
|
||||
|
||||
func (rh *RejsonHandler) JSONResp(key, path string) (res interface{}, err error) {
|
||||
return rh.inner.JSONResp(key, path)
|
||||
}
|
||||
@ -12,7 +12,7 @@ import (
|
||||
|
||||
func TestReJSON(t *testing.T) {
|
||||
rc := redis.NewClient(&redis.Options{Addr: "192.168.8.94:6380"})
|
||||
rh := NewReJSONHandler(context.Background(), rc)
|
||||
rh := NewRedisonHandler(context.Background(), rc)
|
||||
|
||||
testDoc := map[string]any{
|
||||
"members": map[string]any{
|
||||
@ -36,39 +36,21 @@ func TestReJSON(t *testing.T) {
|
||||
midout := gd.mid(tid)
|
||||
logger.Println(midin, tid, midout)
|
||||
|
||||
res, err := rh.JSONSet("jsontest", "$", testDoc)
|
||||
logger.Println(res, err)
|
||||
logger.Println(rh.JSONSet("jsontest", "$", testDoc))
|
||||
logger.Println(rh.JSONGet("jsontest", "$"))
|
||||
logger.Println(rh.JSONResp("jsontest", "$.members"))
|
||||
logger.Println(rh.JSONGetString("jsontest", "$.members..key"))
|
||||
logger.Println(rh.JSONGetInt64("jsontest", "$.members..exp"))
|
||||
logger.Println(rh.JSONObjKeys("jsontest", "$.members"))
|
||||
|
||||
res, err = rh.JSONResp("jsontest", "$.members")
|
||||
vars := res.([]any)
|
||||
logger.Println(vars, err)
|
||||
|
||||
res, err = rh.JSONGetString("jsontest", "$.members..key")
|
||||
logger.Println(res, err)
|
||||
|
||||
res, err = rh.JSONGetInt64("jsontest", "$.members..exp")
|
||||
logger.Println(res, err)
|
||||
|
||||
res, err = rh.JSONObjKeys("jsontest", "$.members")
|
||||
logger.Println(res, err)
|
||||
|
||||
err = rh.JSONMSet("jsontest", map[string]any{
|
||||
err := rh.JSONMSet("jsontest", map[string]any{
|
||||
"$.members.mid1.key": "newval",
|
||||
"$.members.mid2.key": "newval",
|
||||
})
|
||||
logger.Println(err)
|
||||
|
||||
res, err = rh.JSONGet("jsontest", "$")
|
||||
logger.Println(string(res.([]byte)), err)
|
||||
|
||||
err = rh.JSONMDel("jsontest", []string{"$.members.mid1", "$.members.mid2"})
|
||||
logger.Println(err)
|
||||
|
||||
res, err = rh.JSONGet("jsontest", "$")
|
||||
logger.Println(string(res.([]byte)), err)
|
||||
|
||||
res, err = rh.JSONObjLen("jsontest", "$.members")
|
||||
count := res.([]any)[0].(int64)
|
||||
logger.Println(count, err)
|
||||
|
||||
logger.Println(rh.JSONGet("jsontest", "$"))
|
||||
logger.Println(rh.JSONMDel("jsontest", []string{"$.members.mid1", "$.members.mid2"}))
|
||||
logger.Println(rh.JSONGet("jsontest", "$"))
|
||||
logger.Println(rh.JSONObjLen("jsontest", "$.members"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user